Search code examples
ffmpegmpeg-dash

How to set segment duration of a DASH stream using ffmpeg?


I am trying to convert a video to a live DASH stream using ffmpeg. The command looks like this:

ffmpeg -i input.mp4 -preset veryfast \
-c:v libx264 -pix_fmt yuv420p -map 0:v:0 \
-s:0 1280x720 -b:v:0 2M -maxrate:0 2.5M -bufsize:0 4M \
-use_template 1 -use_timeline 1 -seg_duration 1 -f dash foo/stream.mpd

I would like to have the segment duration be 1 second long. However the above command seems to produce chunk files about 8 seconds long instead. However if I change the command to the following (adding -g 5):

ffmpeg -i input.mp4 -preset veryfast \
-c:v libx264 -pix_fmt yuv420p -map 0:v:0 \
-s:0 1280x720 -b:v:0 2M -maxrate:0 2.5M -bufsize:0 4M \
-g 5 \
-use_template 1 -use_timeline 1 -seg_duration 1 -f dash foo/stream.mpd

I get chunk files that are much closer to 1 second long. Obviously this isn't ideal since that will produce a keyframe every 5 frames. So:

a) I don't understand the relationship between seg_duration and keyframe interval. Why is ffmpeg not automatically just putting a keyframe at the beginning of each chunk?

b) How do I get the chunk/segment length I want?


Solution

  • FFmpeg has a modular architecture. seg_duration is an option for the DASH muxer. The encoder has to be told separately about keyframe interval via -g or -force_key_frames.

    For segmented output, the KF interval should be a divisor of the segment duration. You can just set it equal here by -force_key_frames "expr:gte(t,n_forced*1)"