Segment video with ffmpeg

This article introduces how to use the ffmpeg tool via the command line to segment videos.

FFmpeg is a powerful multimedia processing tool that can handle videos, audios, and various other media files.

Segmenting Video by Specified Time Period

Command example: ffmpeg -i input.mp4 -ss start_time -to end_time -c copy output.mp4

Option explanations:

If specifying time in the 00:00:00.00 format, you can omit hours and minutes. For example, 00:10 means the 10th second.

Or use the command ffmpeg -i in.mp4 -c copy -ss start_time -t duration out.mp4

That is, use the -t option to specify the duration from the start time point, rather than the end time.

Evenly Segmenting Video by Specified Duration

Command example: ffmpeg -i input.mp4 -c copy -map 0 -segment_time 3 -f segment -reset_timestamps 1 output%03d.mp4

Option explanations:

Note: This mode cannot precisely segment the video by the specified duration (e.g., 3 seconds) because ffmpeg can only cut at keyframes. If there is no keyframe at the specified time point, the cut point will move forward or backward to the nearest keyframe, resulting in segments of inconsistent duration.

A possible solution is to re-encode the video and force keyframes at the specified time points to achieve precise segmentation. For example, use the -g parameter to set the keyframe interval so that keyframes appear at the expected cut points.