FFmpeg Split Video at Specific Timestamps
This article provides a straightforward, step-by-step guide on how to
split a single video file into multiple parts at precise, custom
timestamps using FFmpeg’s powerful segment muxer. You will
learn the exact command-line syntax, how to handle keyframes for clean
cuts, and how to format the output filenames.
The Basic Command Syntax
To split a video at specific timestamps using the
segment muxer, you use the -f segment option
along with the -segment_times option to define your split
points.
Here is the fundamental command:
ffmpeg -i input.mp4 -f segment -segment_times 60,120,180 -reset_timestamps 1 -c copy output_%03d.mp4Parameter Breakdown
-i input.mp4: Specifies your input video file.-f segment: Invokes the segment muxer.-segment_times 60,120,180: A comma-separated list of the exact times where you want to split the video. You can specify these in seconds (e.g.,60,120,180) or inhh:mm:ssformat (e.g.,00:01:00,00:02:00,00:03:00).-reset_timestamps 1: Resets the timestamps at the beginning of each segment so that each new video file starts at zero. This is crucial for player compatibility.-c copy: Copies the video and audio streams without re-encoding. This process is extremely fast and preserves original quality, but it relies on existing keyframes (keyframes are points in a video where a complete image is stored).output_%03d.mp4: The output file template.%03dis a placeholder that FFmpeg replaces with sequential numbers starting from zero (e.g.,output_000.mp4,output_001.mp4, etc.).
Achieving Frame-Accurate Splits
When using -c copy, FFmpeg can only split the video at
the closest I-frame (keyframe) before or after your specified
timestamp. If your timestamps must be frame-accurate, you have two
choices:
Option 1: Re-encode and Force Keyframes (Recommended for Precision)
To force FFmpeg to cut exactly at your specified timestamps, you must
re-encode the video and tell the encoder to create keyframes at those
exact seconds using the -force_key_frames option:
ffmpeg -i input.mp4 -f segment -segment_times 60,120,180 -force_key_frames 60,120,180 -reset_timestamps 1 output_%03d.mp4Because the video is re-encoded, this process takes longer than stream copying, but it guarantees that the cuts happen exactly at the specified seconds.
Option 2: Split and Re-encode Specific Codecs
If you want to use a specific video codec (like H.264) and audio codec (like AAC) while forcing keyframes, use this command:
ffmpeg -i input.mp4 -f segment -segment_times 00:01:30,00:03:00 -force_key_frames 00:01:30,00:03:00 -c:v libx264 -c:a aac -reset_timestamps 1 output_%03d.mp4