How to extract mp4 video from m3u8 file

15,298

m3u8 files are usually text files that contain links to the actual data files.

A solution I use, is to download the actual data files with curl in a Bash loop and then concatenate all the downloaded files of the playlist.

You can try a Bash function like the following [put it in your .bashrc or .bash_aliases file and edit it accordingly if you need to]:

function download-playlist() {
    if [[ -n "$1" ]]; then
        touch ./files.txt;
        counter=1;
        while read line; do
            if [[ "$line" == "http"* ]]; then
                curl --silent -o ${counter}.mp4 "$line";
                echo "file ${counter}.mp4" >> ./files.txt;
                ((counter++));
            fi;
        done < "$1";
        ffmpeg -f concat -safe 0 -i ./files.txt -codec copy output.mp4;
    else
        echo 'Usage: download-playlist <file.m3u8>';
    fi
}

Then you can use it like this: download-playlist master.m3u8

PS: Usually the master.m3u8 contains links to sub-playlists. The function possibly must be run on a sub-playlist and not on the actual master.m3u8 playlist.

Share:
15,298

Related videos on Youtube

RizwanTahir
Author by

RizwanTahir

BS (EE) from FAST NUCES (Lahore Campus), Former Internee at Pak Elektron Limited (PEL).

Updated on June 04, 2022

Comments

  • RizwanTahir
    RizwanTahir almost 2 years

    I have been downloading videos from a website recently using either Chrome extensions or a downloading site. However, recently it has started to give a m3u8 file download link. I have searched all over internet and tried numerous methods but am unable to download these videos. Does anyone have an idea of how to get the download links of such videos?

    Most of these videos are highlights of football matches that normally play in the browser but cannot be downloaded.

    This is the error I get when i use FFmpeg:

    http @ 0000018479631d00] Protocol 'http' not on whitelist 'file,crypto'!
    master.m3u8: Invalid argument
    
    • avolkmann
      avolkmann almost 6 years
      You should be able to play and download using VLC or FFMPEG.
  • avolkmann
    avolkmann almost 6 years
    VLC can handle AES encryption. If you're up for programming yourself, any language can solve the problem.
  • avolkmann
    avolkmann about 5 years
    @Enrico do you mean the vlc or the programming solution? IIRC vlc will automatically handle encryption. Regarding the DIY way, I wrote about how I solved a similar issue here: andreasvolkmann.github.io/posts/2018-04-27-m3u8-and-ts-segme‌​nts
  • Fulalas
    Fulalas over 3 years
    Nice answer! Do you mind fixing ffmpeg call? There's a missing 'f' :)
  • Bruce
    Bruce over 2 years
    Nice answer! The ffmpeg option -safe 0 is not necessary in my case.