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

1

2 Answers 2

272

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
6
  • 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 Dec 27, 2016 at 13:00
  • 1
    @Mayuri -t <time> after the input in s or hms.ms
    – aergistal
    Dec 28, 2016 at 14:27
  • hey can any one help me to get download progress fore the same case Dec 29, 2020 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, 2022 at 21:29
  • It seems that the http part is required, ffmpeg doesn't do anything given an offline .m3u8 file. Dunno why tho Mar 27, 2022 at 16:42
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.

9
  • 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 3, 2017 at 22:01
  • @blackmiacool : How can do achieve this programatically ?
    – Vineesh TP
    Nov 3, 2017 at 4:08
  • .ts also works quiet well for some streams
    – Honza R
    Apr 3, 2018 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? Sep 25, 2019 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 Apr 22, 2020 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.