Download encrypted HLS content with ffmpeg

published on in category linux , Tags: 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.

I run this site without advertisement of any kind. All information is free and my only goal is to give back something to the amazing free software development community. If you find some value in this, please consider donating me a cup of coffee using PayPal. Thank you so much!