112

How Can I extract mp4 from http live streaming m3u8 file? I Tried this command below:

ffmpeg -i {input file} -f rawvideo -bsf h264_mp4toannexb -vcodec copy out.mp4

I took this error:

[NULL @ 0000000002f07060] Packet header is not contained in global extradata, corrupted stream or invalid MP4/AVCC bitstream Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: I

jaunt
  • 4,978
  • 4
  • 33
  • 54
thiago.adriano26
  • 1,491
  • 3
  • 14
  • 19
  • 1
    you can use this extension https://chrome.google.com/webstore/detail/hls-downloader/apomkbibleomoihlhhdbeghnfioffbej?hl=en – regisbsb Nov 26 '19 at 14:08

2 Answers2

271

Your command is completely incorrect. The output format is not rawvideo and you don't need the bitstream filter h264_mp4toannexb which is used when you want to convert the h264 contained in an mp4 to the Annex B format used by MPEG-TS for example. What you want to use instead is the aac_adtstoasc for the AAC streams.

ffmpeg -i http://.../playlist.m3u8 -c copy -bsf:a aac_adtstoasc output.mp4
Community
  • 1
  • 1
aergistal
  • 29,947
  • 5
  • 70
  • 92
  • Hello, This Answer helped me to record live stream from m3u8 url and video also gets saved in output.mp4. My question is can we specify time in seconds/minutes in this command so that the video saved is of the specified time. Currently it keeps on recording and stops until I press q in console. Thanks In advance – Mayuri Ruparel Dec 27 '16 at 13:00
  • 1
    @Mayuri `-t – aergistal Dec 28 '16 at 14:27
  • hey can any one help me to get download progress fore the same case – Shivam Parmar Dec 29 '20 at 11:51
  • with some m3u8 files that `ffmpeg` refuses to dump, it is still possible to do with vlc > open network > stream output: Settings > File. – ccpizza Feb 16 '22 at 21:29
  • It seems that the `http` part is required, `ffmpeg` doesn't do anything given an offline `.m3u8` file. Dunno why tho – Minh Nghĩa Mar 27 '22 at 16:42
  • @MinhNghĩa Check the console output, you probably need to enable `-protocol_whitelist file` to allow local M3U8 access. – aergistal Apr 04 '22 at 15:03
26

Aergistal's answer works, but I found that converting to mp4 can make some m3u8 videos broken. If you are stuck with this problem, try to convert them to mkv, and convert them to mp4 later.

blackmiaool
  • 5,234
  • 2
  • 22
  • 39
  • 64
    I've found that `ffmpeg -i http://...m3u8 -c copy live.mkv` works very well. Just if anyone comes around and reads this. – rwenz3l Jun 03 '17 at 22:01
  • @blackmiacool : How can do achieve this programatically ? – Vineesh TP Nov 03 '17 at 04:08
  • `.ts` also works quiet well for some streams – Honza R Apr 03 '18 at 21:13
  • 3
    If you have a https:// address change it to http:// otherwise you get an "HTTP error 403 forbidden" error ... For converting MKV to MP4 simply use `ffmpeg -i file.mkv file.mp4` but why should one do that? – Erich Kuester Sep 25 '19 at 19:45
  • 10
    In case of local `m3u8` file, I also had to add a white-list at front, like: `ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto -i 02.m3u8 -c copy 02.mkv` – Bill Kotsias Apr 22 '20 at 08:08
  • @ErichKuester - Because MKV is more resilient to potential corruption - I imagine? – Robert Perry Jun 16 '20 at 08:10
  • @robert-perry - Maybe, don't know exactly, just meaning, if you can play MKV with vlc, there is no reason for extra-converting to MP4 ... – Erich Kuester Jun 17 '20 at 10:05
  • @rwenz3l I found your comment true also when input is a stream, mkv could be played even if the stream is interupt or the ffmpeg process exit when pulling the video. mp4 is not even playable in such situations – Luk Aron Jan 01 '21 at 17:53
  • @ErichKuester But there's an "MP4" in the question. – blackmiaool Jul 14 '21 at 02:51