Configure FFmpeg Segment Muxer to Split Live Streams
This guide explains how to configure the FFmpeg segment
muxer to split a continuous live stream into smaller, sequential video
chunks. You will learn the exact command-line arguments needed to split
streams by duration, generate a dynamic playlist, and ensure clean cuts
by managing keyframes.
The segment muxer in FFmpeg splits an input stream into
multiple files of a specified duration. This is highly useful for HTTP
Live Streaming (HLS) preparation, archiving, or processing live
feeds.
Basic Command Syntax
To split a live RTMP or RTSP stream into 10-second MPEG-TS segments, use the following basic command:
ffmpeg -i rtmp://source.com/live/stream \
-c copy \
-f segment \
-segment_time 10 \
-reset_timestamps 1 \
-segment_list playlist.m3u8 \
segment_%03d.tsKey Configuration Parameters
-f segment: Tells FFmpeg to use the segment muxer.-segment_time 10: Sets the target duration for each segment in seconds.-reset_timestamps 1: Resets the timestamps at the beginning of each segment to near zero. This is critical for ensuring individual segments can be played back reliably on standard media players.-segment_list playlist.m3u8: Generates a playlist file tracking all created segments. Common formats include.m3u8(for HLS) or.csv/.txtfor basic tracking.segment_%03d.ts: The output file pattern.%03dis a placeholder that outputs sequential three-digit numbers (e.g.,segment_001.ts,segment_002.ts).
Handling Keyframes for Accurate Splitting
FFmpeg can only split a video stream at an intra-frame (I-frame or
keyframe). If your source stream does not have frequent keyframes,
segments may be much longer than your defined -segment_time
because FFmpeg must wait for the next keyframe to cut the file.
If you are re-encoding the stream (instead of using
-c copy), you should force keyframes at precise intervals
to ensure accurate splitting:
ffmpeg -i rtmp://source.com/live/stream \
-c:v libx264 -g 60 -keyint_min 60 -sc_threshold 0 \
-f segment \
-segment_time 2 \
-reset_timestamps 1 \
-segment_list live.m3u8 \
output_%05d.tsIn this configuration: * -g 60 forces a
group of pictures (GOP) size of 60 frames. At 30fps, this creates a
keyframe exactly every 2 seconds. *
-sc_threshold 0 prevents scene-change
detection from inserting extra, unexpected keyframes that might disrupt
the division interval.