Three different scenarios. I added the first two for clarification.
1 - Want to keep only the last N seconds of a video
instead of [-ss #] use [-sseof -#]
Example: -sseof -7
$ ffmpeg -sseof -7 -i input.mp4 -c copy output.mp4
Keeps the last 7 seconds of the video and discards the rest
2 - Want to delete the last N seconds of a video and keep the rest. (Manually enter duration)
This option requires manual entry of end time (for automatic, see #3)
Use -ss and -t (duration from start point) or -to (specific timestamp)
You'll need to calculate the end time manually for this option.
Examples:
$ ffmpeg -ss 00:00:00 -to 01:32:00 -i input.mp4 -c copy output.mp4;
Keeps the video from timestamp 00:00:00 to timestamp 01:32:00
$ ffmpeg -ss 00:00:04 -t 01:00:00 -i input.mp4 -c copy output.mp4;
Keeps the video from timestamp 00:00:04 to timestamp 01:00:04
1 hour long video.
3 - Want to delete the last N seconds of a video and keep the rest. (Auto-detect duration of file)
You'll need to use both ffmpeg
and ffprobe
, but it can all be done in terminal, with very little code.
The only way to cut off seconds based on the end time is to get the end time. I do batch processing of videos with varied lengths, so manually looking up and inputting each duration was out of the question. I needed to automatically access the duration value and pass it to ffmpeg.
The Magic Formula:
duration=`ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4`
duration=`bc $duration - seconds`
Example:
$ duration=`ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4`
$ duration=`bc $duration - 7`
$ ffmpeg -ss 00:00:00 -to $duration -i input.mp4 -c copy output.mp4
Removes the last 7 seconds of the video.
ffmpeg -i input.avi
without output file name. That will show duration. Then you can useduration - 7
to use for the-ss
flag. Provide the complete console output here.