FFmpeg: Crop Video - Last 10 Seconds Not Working
When using FFmpeg to cut a 30-second video and only keep the last 10 seconds, the following command might be used:
ffmpeg -ss 21 -i input.webm -c copy -t 10 output.webmHowever, this command might not work as expected, and the resulting video may contain the first 10 seconds instead of the last 10 seconds. This article will explain why this happens and provide a solution.
Understanding the FFmpeg Command
The FFmpeg command used to cut a video consists of several parts:
ffmpeg: the FFmpeg command-line tool-ss 21: the seek point in seconds-i input.webm: the input file-c copy: copy the input codecs to the output-t 10: the duration of the output in secondsoutput.webm: the output file
The -ss option specifies the seek point in the input video, and the -t option specifies the duration of the output. In this case, the command seeks 21 seconds into the input video and outputs the next 10 seconds.
Why the Command Might Not Work
The reason why the command might not work as expected is that the -c copy option tells FFmpeg to copy the input codecs to the output without re-encoding. This means that the output video will start at the seek point specified by the -ss option, but it will not adjust the timestamps of the frames.
As a result, the output video will still have the same timestamps as the input video, and the first frame of the output video will correspond to the 21st second of the input video. This means that the output video will contain the first 10 seconds instead of the last 10 seconds.
A Solution
To fix this issue, we need to re-encode the output video and adjust the timestamps of the frames. This can be done by removing the -c copy option and adding the -avoid_negative_ts make_zero option:
ffmpeg -ss 21 -i input.webm -avoid_negative_ts make_zero -t 10 output.webmThe -avoid_negative_ts make_zero option tells FFmpeg to adjust the timestamps of the frames to avoid negative timestamps. This will adjust the timestamps of the frames in the output video so that the first frame corresponds to the 30th second of the input video.
When using FFmpeg to cut a video and keep only the last 10 seconds, the -c copy option might not work as expected. To fix this issue, we need to re-encode the output video and adjust the timestamps of the frames using the -avoid_negative_ts make_zero option.
References
-
FFmpeg Documentation: https://ffmpeg.org/documentation.html
-
FFmpeg Forum: Cutting a video with FFmpeg: