Basic stream segmenter.
This muxer outputs streams to a number of separate files of nearly fixed duration. Output filename pattern can be set in a fashion similar to image2, or by using a strftime
template if thestrftime option is enabled.
stream_segment
is a variant of the muxer used to write to streaming output formats, i.e. which do not require global headers, and is recommended for outputting e.g. to MPEG transport stream segments. ssegment
is a shorter alias for stream_segment
.
Every segment starts with a keyframe of the selected reference stream, which is set through the reference_stream option.
Note that if you want accurate splitting for a video file, you need to make the input key frames correspond to the exact splitting times expected by the segmenter, or the segment muxer will start the new segment with the key frame found next after the specified start time.
The segment muxer works best with a single constant frame rate video.
Optionally it can generate a list of the created segments, by setting the option segment_list. The list type is specified by the segment_list_type option. The entry filenames in the segment list are set by default to the basename of the corresponding segment files.
See also the hls muxer, which provides a more specific implementation for HLS segmentation.
4.22.1 Options
The segment muxer supports the following options:
- increment_tc 1|0
-
if set to
1
, increment timecode between each segment If this is selected, the input need to have a timecode in the first video stream. Default value is0
. - reference_stream specifier
-
Set the reference stream, as specified by the string specifier. If specifier is set to
auto
, the reference is chosen automatically. Otherwise it must be a stream specifier (see the “Stream specifiers” chapter in the ffmpeg manual) which specifies the reference stream. The default value isauto
. - segment_format format
-
Override the inner container format, by default it is guessed by the filename extension.
- segment_format_options options_list
-
Set output format options using a :-separated list of key=value parameters. Values containing the
:
special character must be escaped. - segment_list name
-
Generate also a listfile named name. If not specified no listfile is generated.
- segment_list_flags flags
-
Set flags affecting the segment list generation.
It currently supports the following flags:
- ‘ cache’
-
Allow caching (only affects M3U8 list files).
- ‘ live’
-
Allow live-friendly file generation.
- segment_list_size size
-
Update the list file so that it contains at most size segments. If 0 the list file will contain all the segments. Default value is 0.
- segment_list_entry_prefix prefix
-
Prepend prefix to each entry. Useful to generate absolute paths. By default no prefix is applied.
- segment_list_type type
-
Select the listing format.
The following values are recognized:
- ‘ flat’
-
Generate a flat list for the created segments, one segment per line.
- ‘ csv, ext’
-
Generate a list for the created segments, one segment per line, each line matching the format (comma-separated values):
segment_filename,segment_start_time,segment_end_time
segment_filename is the name of the output file generated by the muxer according to the provided pattern. CSV escaping (according to RFC4180) is applied if required.
segment_start_time and segment_end_time specify the segment start and end time expressed in seconds.
A list file with the suffix
".csv"
or".ext"
will auto-select this format.‘ext’ is deprecated in favor or ‘csv’.
- ‘ ffconcat’
-
Generate an ffconcat file for the created segments. The resulting file can be read using the FFmpeg concat demuxer.
A list file with the suffix
".ffcat"
or".ffconcat"
will auto-select this format. - ‘ m3u8’
-
Generate an extended M3U8 file, version 3, compliant with http://tools.ietf.org/id/draft-pantos-http-live-streaming.
A list file with the suffix
".m3u8"
will auto-select this format.
If not specified the type is guessed from the list file name suffix.
- segment_time time
-
Set segment duration to time, the value must be a duration specification. Default value is "2". See also the segment_times option.
Note that splitting may not be accurate, unless you force the reference stream key-frames at the given time. See the introductory notice and the examples below.
- segment_atclocktime 1|0
-
If set to "1" split at regular clock time intervals starting from 00:00 o’clock. The time value specified in segment_time is used for setting the length of the splitting interval.
For example with segment_time set to "900" this makes it possible to create files at 12:00 o’clock, 12:15, 12:30, etc.
Default value is "0".
- segment_clocktime_offset duration
-
Delay the segment splitting times with the specified duration when using segment_atclocktime.
For example with segment_time set to "900" and segment_clocktime_offset set to "300" this makes it possible to create files at 12:05, 12:20, 12:35, etc.
Default value is "0".
- segment_clocktime_wrap_duration duration
-
Force the segmenter to only start a new segment if a packet reaches the muxer within the specified duration after the segmenting clock time. This way you can make the segmenter more resilient to backward local time jumps, such as leap seconds or transition to standard time from daylight savings time.
Assuming that the delay between the packets of your source is less than 0.5 second you can detect a leap second by specifying 0.5 as the duration.
Default is the maximum possible duration which means starting a new segment regardless of the elapsed time since the last clock time.
- segment_time_delta delta
-
Specify the accuracy time when selecting the start time for a segment, expressed as a duration specification. Default value is "0".
When delta is specified a key-frame will start a new segment if its PTS satisfies the relation:
PTS >= start_time - time_delta
This option is useful when splitting video content, which is always split at GOP boundaries, in case a key frame is found just before the specified split time.
In particular may be used in combination with the ffmpeg option force_key_frames. The key frame times specified by force_key_frames may not be set accurately because of rounding issues, with the consequence that a key frame time may result set just before the specified time. For constant frame rate videos a value of 1/(2*frame_rate) should address the worst case mismatch between the specified time and the time set by force_key_frames.
- segment_times times
-
Specify a list of split points. times contains a list of comma separated duration specifications, in increasing order. See also the segment_time option.
- segment_frames frames
-
Specify a list of split video frame numbers. frames contains a list of comma separated integer numbers, in increasing order.
This option specifies to start a new segment whenever a reference stream key frame is found and the sequential number (starting from 0) of the frame is greater or equal to the next value in the list.
- segment_wrap limit
-
Wrap around segment index once it reaches limit.
- segment_start_number number
-
Set the sequence number of the first segment. Defaults to
0
. - strftime 1|0
-
Use the
strftime
function to define the name of the new segments to write. If this is selected, the output segment name must contain astrftime
function template. Default value is0
. - break_non_keyframes 1|0
-
If enabled, allow segments to start on frames other than keyframes. This improves behavior on some players when the time between keyframes is inconsistent, but may make things worse on others, and can cause some oddities during seeking. Defaults to
0
. - reset_timestamps 1|0
-
Reset timestamps at the begin of each segment, so that each segment will start with near-zero timestamps. It is meant to ease the playback of the generated segments. May not work with some combinations of muxers/codecs. It is set to
0
by default. - initial_offset offset
-
Specify timestamp offset to apply to the output packet timestamps. The argument must be a time duration specification, and defaults to 0.
- write_empty_segments 1|0
-
If enabled, write an empty segment if there are no packets during the period a segment would usually span. Otherwise, the segment will be filled with the next packet written. Defaults to
0
.
4.22.2 Examples
- Remux the content of file in.mkv to a list of segments out-000.nut, out-001.nut, etc., and write the list of generated segments to out.list:
ffmpeg -i in.mkv -codec copy -map 0 -f segment -segment_list out.list out%03d.nut
- Segment input and set output format options for the output segments:
ffmpeg -i in.mkv -f segment -segment_time 10 -segment_format_options movflags=+faststart out%03d.mp4
- Segment the input file according to the split points specified by the segment_times option:
ffmpeg -i in.mkv -codec copy -map 0 -f segment -segment_list out.csv -segment_times 1,2,3,5,8,13,21 out%03d.nut
- Use the
ffmpeg
force_key_frames option to force key frames in the input at the specified location, together with the segment option segment_time_delta to account for possible roundings operated when setting key frame times.ffmpeg -i in.mkv -force_key_frames 1,2,3,5,8,13,21 -codec:v mpeg4 -codec:a pcm_s16le -map 0 \ -f segment -segment_list out.csv -segment_times 1,2,3,5,8,13,21 -segment_time_delta 0.05 out%03d.nut
In order to force key frames on the input file, transcoding is required.
- Segment the input file by splitting the input file according to the frame numbers sequence specified with the segment_frames option:
ffmpeg -i in.mkv -codec copy -map 0 -f segment -segment_list out.csv -segment_frames 100,200,300,500,800 out%03d.nut
- Convert the in.mkv to TS segments using the
libx264
andlibfaac
encoders:ffmpeg -i in.mkv -map 0 -codec:v libx264 -codec:a libfaac -f ssegment -segment_list out.list out%03d.ts
- Segment the input file, and create an M3U8 live playlist (can be used as live HLS source):
ffmpeg -re -i in.mkv -codec copy -map 0 -f segment -segment_list playlist.m3u8 \ -segment_list_flags +live -segment_time 10 out%03d.mkv