[FFmpeg-user] Generating HLS chunks on demand

German Geraskin german.geraskin at gmail.com
Wed Jan 25 10:37:58 EET 2023


Hi Vincent,

Do not use -ss and -t parameters simultaneously. FFmpeg cannot form an
output container well in that case.

If you just need to create HLS segments from some source file you can
use the "segment" muxer.

ffmpeg -i source.mp4 -vcodec copy -acodec copy -f ssegment
-segment_list chunklist.m3u8 -segment_time 4  media_%04d.ts

Chunk durations in such an approach may vary, because in your source
file the distance between key-frames may vary.

If you want to create HLS segments of exact length, then you must
transcode your source file and provide exact frame rate -r and exact
GOP -g .

For example, if your source file has a frame rate of 25 frames per
second, then to make HLS chunk duration of 4 second, you need to set
GOP to 100 (or 50, or 25).
To have 1 keyframe in each HLS chunk: -r 25 -g 100
To have 2 keyframes in each HLS chunk: -r 25 -g 50
To have 4 keyframes in each HLS chunk: -r 25 -g 25

So the final command may look like this:

ffmpeg -i source.mp4 -r 25 -g 100 -c:v libx264 -preset fast -crf 24
-c:a aac -b:a 128k -f ssegment -segment_list chunklist.m3u8
-segment_time 4  media_%04d.ts

German


More information about the ffmpeg-user mailing list