How to Configure fMP4 Fragmentation in FFmpeg

Fragmented MP4 (fMP4) is a variation of the MP4 format that divides media into sequenceable chunks, making it ideal for adaptive bitrate streaming protocols like DASH and HLS. This article provides a direct, practical guide on how to configure fMP4 fragmentation criteria in FFmpeg using either duration-based limits or frame count (GOP-based) thresholds.

The Foundation of fMP4 Muxing in FFmpeg

To output an fMP4 file, you must instruct FFmpeg’s mov muxer to write fragmented data instead of a standard single-atom MP4. This is primarily done using the -movflags option combined with specific fragmentation parameters.

At a minimum, you must define how the initial movie atom (moov) and subsequent movie fragments (moof and mdat) are written:

ffmpeg -i input.mp4 -c:v libx264 -movflags +empty_moov+default_base_moof output.mp4

Method 1: Fragmenting by Duration

To fragment your video based on a specific duration, use the -frag_duration option. This option accepts a value in microseconds (1 second = 1,000,000 microseconds).

For example, to split the stream into fragments of exactly 4 seconds (4,000,000 microseconds), use the following command:

ffmpeg -i input.mp4 -c:v libx264 -movflags +empty_moov+default_base_moof -frag_duration 4000000 output.mp4

FFmpeg will attempt to cut fragments as close to this duration as possible. Note that a new fragment can only begin on a keyframe (I-frame). If your video does not have keyframes at the specified interval, the actual fragment duration may vary.


Method 2: Fragmenting by Frame Count (GOP Size)

FFmpeg does not have a direct -frag_frame_count option. Instead, fMP4 fragmentation is naturally tied to keyframes. To fragment a video by a precise frame count, you must force a keyframe interval (Group of Pictures or GOP size) in the video encoder and tell the muxer to fragment on every keyframe.

To segment a video precisely every 60 frames (e.g., every 2 seconds in a 30fps video), configure the encoder’s GOP size (-g) and disable scene change detection (-sc_threshold 0 for x264), then apply the frag_keyframe flag:

ffmpeg -i input.mp4 -c:v libx264 -g 60 -keyint_min 60 -sc_threshold 0 -movflags +empty_moov+default_base_moof+frag_keyframe output.mp4