Use the
-f segment muxer with
-segment_time and
-reset_timestamps 1 to split recordings into manageable chunks. Append
-movflags +frag_keyframe+empty_moov to ensure each segment is a valid, playable fragmented MP4 file that can be concatenated later without re-encoding.
Understanding Fragmented MP4 Structure
Standard MP4 files store metadata in a single header at the beginning of the file. This structure works well for short clips but becomes problematic for long recordings because the entire file must be written before it is fully playable or seekable. Fragmented MP4 (fMP4) changes this by distributing metadata across multiple
moof (movie fragment) boxes throughout the stream. This allows players to start playback almost immediately after receiving the first few kilobytes of data, which is critical for live streaming and continuous recording scenarios. When you record in segments, you are essentially creating a series of these small, self-contained fragments rather than one monolithic container.
Essential FFmpeg Flags for Segmentation
To achieve this behavior, you need to combine several specific flags. The core mechanism is the segment muxer, which handles the splitting logic. You must specify the format as
-f segment. Then, use
-segment_time to define the duration of each chunk in seconds. For example, setting this to
60 creates a new file every minute. It is crucial to add
-reset_timestamps 1. Without this, timestamps accumulate across all segments, causing playback issues when individual files are opened in isolation. Each segment should appear as if it started at time zero.
The most important flag for MP4 compatibility is
-movflags. You should use
+frag_keyframe+empty_moov. The
frag_keyframe option ensures that each segment starts with a keyframe, making it independently decodable. The
empty_moov flag prevents the creation of a large initial index, keeping the file size minimal and allowing for faster writes. If you omit these, you might get standard MP4 segments that cannot be played until the recording stops, defeating the purpose of real-time segmentation.
For more information on stabilizing large video files during processing, see our guide on
stabilizing large videos with Linux Mint and ffmpeg. This technique often pairs well with segmented recording when dealing with high-motion content.
Constructing the Command Line
A typical command for recording a webcam or screen input looks like this:
ffmpeg -f v4l2 -i /dev/video0 \
-c:v libx264 -preset ultrafast -tune zerolatency \
-f segment -segment_time 300 -reset_timestamps 1 \
-movflags +frag_keyframe+empty_moov \
output_%03d.mp4
Here,
-c:v libx264 uses the H.264 encoder, which is widely supported. The
-preset ultrafast minimizes CPU load, which is vital for long-term recording stability. The output pattern
output_%03d.mp4 generates files named
output_001.mp4,
output_002.mp4, etc. Adjust the codec settings based on your hardware capabilities. If you are using hardware acceleration, such as NVENC, you may encounter errors related to version queries. Refer to the article on
solving failed to query nvenc max version error if you face driver-specific issues while trying to optimize encoding speed.
Managing Disk Space and File Growth
Continuous recording will eventually fill your storage. Since each segment is an independent file, you can implement simple shell scripts to monitor disk usage. For instance, you can set up a cron job that checks the total size of the
output_*.mp4 files and deletes the oldest ones when a threshold is reached. Because the files are fragmented MP4s, they do not require complex merging to be viewed individually. However, if you need a single consolidated file for archival, you can concatenate them using the concat demuxer. Note that concatenation requires all segments to have identical parameters; otherwise, you must remux them into a common format first.
Playback and Compatibility Considerations
Fragmented MP4 files are supported by modern web browsers and mobile devices, including iOS and Android. However, some older media players might struggle with the
empty_moov structure. If you need maximum compatibility for offline playback, consider converting the final segments back to standard MP4 containers after the recording session ends. This post-processing step adds overhead but ensures universal playability. For users looking to optimize their videos for specific devices like iPhones, check out
converting video to play on iPhone with ffmpeg on Linux for detailed profile adjustments.
Frequently Asked Questions
Can I merge fragmented MP4 segments without re-encoding?
Yes, you can use the FFmpeg concat demuxer to join them losslessly. Create a text file listing the filenames and run
ffmpeg -f concat -i list.txt -c copy merged.mp4. Ensure all segments were created with identical codecs and resolutions.
Why does my player freeze when opening a single segment?
This usually happens if you omitted
-movflags +frag_keyframe. Without a keyframe at the start, the decoder cannot locate the beginning of the frame data efficiently. Adding the flag ensures each file is self-contained and seekable from the first frame.
How do I handle audio synchronization across segments?
Always include
-reset_timestamps 1 for both video and audio streams. This resets the internal clock for each new file, preventing drift or silence gaps between segments. Verify that your audio codec matches the video codec's timing assumptions.
Is it better to use MKV or MP4 for long recordings?
MKV is generally more robust for live recording because it handles interruptions gracefully. However, MP4 is preferred for web distribution and mobile playback. Use MP4 if you need broad compatibility; choose MKV if you prioritize ease of recovery after crashes.
What happens if the recording process crashes?
Since each segment is written separately, partial corruption usually affects only the current chunk. The previous segments remain intact and playable. You can restart the recording process, and FFmpeg will continue generating new numbered files, leaving old data untouched.