Download encrypted HLS content with ffmpeg

published on on macOS, Linux, ffmpeg

I maintain an archive of videos, especially documentaries from public media libraries from tv channels etc. on my NAS. Whilst I can use youtube-dl for the most part, it's rather difficult for sites that use HTTP live streaming (HLS) to stream their content. In the most cases this is even AES-128 encrypted, which makes it difficult to download it. But since there's players that support playback, it's obviously possible to decode the stream on the client side. ffmpeg is the swiss army knife for video/audio encoding and muxing and therefore the perfect utility for downloading encrypted HLS content.

I use the following command to achieve this:


ffmpeg -i "$1" -c copy -bsf:a aac_adtstoasc $2

where $1 is the URL to the m3u8 playlist file. To find this, just open the Developer Tools of your favorite browser and inspect the network requests when loading the page. Just filter for m3u8, you'll probably find something. $2 is the target file, so something like history-of-computers.mp4.

I'm not sure if you need any plugins for ffmpeg to make this work, for me it works inside a Docker container with a standard Ubuntu 18.04 and the ffmpeg binary provided by the repository.