I’m trying to understand how FFmpeg handles the -to option when used as an input option vs an output option in stream copy mode.
From the FFmpeg documentation
my interpretation is that the position of -to affects what is included in the output file. Specifically:
- When
-tois used as an input option, FFmpeg stops reading input at theend_timestampand this can happen before or after the next keyframe. In this case, the output will only include complete GOP that are within the provided interval, but some frames might be lost if they are beyond end_timestamp.
|—-K1——[K2–start_timestamp——K3——K4]—-end_timestamp—K5—|
- When
-tois used as an output option, FFmpeg stops writing output once the output duration reaches theend_timestamp. In stream copy mode, FFmpeg only writes complete GOPs, so it will include the keyframe immediately after end_timestamp.
|—-K1——[K2–start_timestamp——K3——K4—-end_timestamp—K5]—|
My question:
Is this understanding correct? Specifically, does using -to as an input option vs. an output option change which keyframes are included in the output file? And what’s the correct way to cut a segment so that no frames before the specified end_timestamp are missing?
Command examples:
# Using -to as input option
ffmpeg -ss start_timestamp -to end_timestamp -i input.mp4 -c copy cut_input.mp4
# Using -to as output option
ffmpeg -ss start_timestamp -i input.mp4 -to end_timestamp -c copy cut_output.mp4