Split Video and Generate Index File Using FFmpeg
This article provides a step-by-step guide on how to split a video file into smaller segments and automatically generate a custom index file using FFmpeg. You will learn how to use FFmpeg’s segment muxer to slice your videos by time intervals without losing quality, and how to output index formats like M3U8 playlists or standard text-based file lists.
Method 1: Split Video and Generate an M3U8 Index (HLS)
The most common way to split a video and create an index file is by
using the HTTP Live Streaming (HLS) format. This method outputs video
segments alongside an .m3u8 index file.
Run the following command in your terminal:
ffmpeg -i input.mp4 -c copy -map 0 -f segment -segment_time 10 -segment_list playlist.m3u8 output%03d.mp4Parameter Breakdown: * -i input.mp4:
Defines the source video file. * -c copy: Copies the video
and audio streams without re-encoding, which makes the process extremely
fast. * -map 0: Maps all streams from the input file to the
output. * -f segment: Specifies that the segment muxer
should be used. * -segment_time 10: Sets the target segment
duration to 10 seconds. Note that FFmpeg will split at the nearest
keyframe, so segment lengths may vary slightly. *
-segment_list playlist.m3u8: Generates the custom index
file containing the list of segments. * output%03d.mp4:
Names the output segments sequentially (e.g.,
output000.mp4, output001.mp4).
Method 2: Generate a Custom Text Index (FFConcat Format)
If you need a plain text index file that lists all the generated
segments—useful for later merging or record-keeping—you can generate an
ffconcat index.
Run this command:
ffmpeg -i input.mp4 -c copy -f segment -segment_time 30 -segment_list index.txt -segment_list_type ffconcat chunk%03d.mp4Parameter Breakdown: *
-segment_list index.txt: Specifies the name of the custom
text index. * -segment_list_type ffconcat: Instructs FFmpeg
to format the index file as an FFmpeg concatenation script, listing the
filename of each segment.
The resulting index.txt file will look like this:
file 'chunk000.mp4'
file 'chunk001.mp4'
file 'chunk002.mp4'
Method 3: Splitting with Frame-Accurate Precision
Because the -c copy command splits videos only at
existing keyframes (I-frames), the split times might not be perfectly
precise. If you require exact interval splitting, you must force
keyframes and re-encode the video.
Use this command to force keyframes every 10 seconds:
ffmpeg -i input.mp4 -force_key_frames "expr:gte(t,n_forced*10)" -f segment -segment_time 10 -segment_list precise_playlist.m3u8 output%03d.mp4Parameter Breakdown: *
-force_key_frames "expr:gte(t,n_forced*10)": Forces the
encoder to place a keyframe exactly every 10 seconds. This ensures that
the segment cuts occur precisely at the specified duration.