Search code examples
videoffmpegmp4http-live-streaming

ffmpeg segmentation with multiple mp4 files to HLS stream


I'm trying to segment multiple mp4 files from a .txt (just like concatenation works), but it doesn't seem to work for me.

This is my concat.txt:

file video1.mp4
file video2.mp4
file video3.mp4

etc...

and my ffmpeg commands are:

ffmpeg -i concat.txt -map 0 -codec:v libx264 -codec:a libfaac -f ssegment \
-segment_list playlist.m3u8 -segment_list_flags +live -segment_time 10 out%03d.ts

Unfortunately every mediaplayer throws an error on playback.

Can I use the concat file, or do I have to concat all the mp4s first into a single mp4 file, and segment that mp4 file to get the final m3u8?


Solution

  • The ffmpeg concat demuxer requires the same codecs for the input files, although the container can vary. You also need to make sure the video and audio streams have the same IDs across all files.

    ffmpeg -re -f concat -i concat.txt -c:v libx264 -vbsf h264_mp4toannexb -r 25 -g 75 -c:a libfdk_aac -hls_time 3 playlist.m3u8

    I used the h264_mp4toannexb bitstream filter to convert the H.264 stream to the Annex B format required by MPEG-TS, set the GOP size to 75 (3 seconds at 25 fps) and used a segment length of 3 seconds since each segment should start with a keyframe.