Converting ABCDEFG
to ACDFG
is essentially cutting three segments from the video, and then joining those three segments. If all segments will be cut from the same video, and there is no need to change the video's encoding (resolution, bit rate, frame rate, etc.), then re-encoding isn't necessary, and will degrade quality for lossy formats. However, without re-encoding, the cut positions will be not exact but the key frames nearest to the specified times.
The below examples assume that vidfull.mp4
is ABCDEFG
, where each letter represents a five minute long segment.
Without re-encoding, the process is almost instantaneous, but cutting needs to be done explicitly, where temporary files are created, and joining can be done either with the concatenation protocol (i.e. file level concatenation), and the .ts
container as the intermediate format, which supports concatenation at the file level:
ffmpeg \
-to 5:0 -i vidfull.mp4 \
-ss 10:0 -to 20:0 -i vidfull.mp4 \
-ss 25:0 -i vidfull.mp4 \
-map 0:v -map 0:a -c copy part1.ts \
-map 1:v -map 1:a -c copy part2.ts \
-map 2:v -map 2:a -c copy part3.ts
ffmpeg -i 'concat:part1.ts|part2.ts|part3.ts' -c copy vidcut.ts
ffmpeg -i vidcut.ts -c copy vidcut.mp4
rm -f part{1..3}.ts vidcut.ts
or with the concatenation script demuxer:
ffmpeg \
-to 5:0 -i vidfull.mp4 \
-ss 10:0 -to 20:0 -i vidfull.mp4 \
-ss 25:0 -i vidfull.mp4 \
-map 0:v -map 0:a -c copy -avoid_negative_ts make_non_negative part1.mp4 \
-map 1:v -map 1:a -c copy -avoid_negative_ts make_non_negative part2.mp4 \
-map 2:v -map 2:a -c copy -avoid_negative_ts make_non_negative part3.mp4
cat <<- eof > script.txt
ffconcat version 1.0
file '/path to/part1.mp4'
file '/path to/part2.mp4'
file '/path to/part3.mp4'
eof
ffmpeg -f concat -safe 0 -i script.txt -c copy vidcut.mp4
rm -f part{1..3}.mp4 script.txt
With re-encoding, the process is slow, but cutting and joining is done with a single command by using the same video as the input to a complex concat filter multiple times after seeking to different positions:
ffmpeg \
-to 5:0 -i vidfull.mp4 \
-ss 10:0 -to 20:0 -i vidfull.mp4 \
-ss 25:0 -i vidfull.mp4 \
-lavfi concat=n=3:a=1 vidcut.mp4
No need to label input or output pads to the concat filter since there isn't any other filter than concat in the filter chain to use a specific stream from a specific pad, no stream is intended to be excluded from the output, and all streams from unlabelled pads are added to the first output file automatically (the output format needs to support all stream types to avoid a fatal error). And, among the options of the concat filter, n
is the number of input segments, and needs to be set to 3
since it is 2
by default, and a
is the number of output audio streams, and needs to be set to 1
since it is 0
by default.