Trim Seconds of a Video with FFMPEG

Google failed me. How do you trim x amount of time off a video when you don’t know its length (imagine a script scenario). All this whilst preserving its quality, i.e. copy only, no transcoding.

First we need to find its length. We do this via ffprobe:

ffprobe -v quiet -show_entries format=duration movie.mp4

-v quiet shuts off ffprobe’s info vomit and just gives us the line we want. Let’s get rid of the lines we don’t care about:

ffprobe -v quiet -show_entries format=duration movie.mp4 | grep "duration"

Now let’s use cut to get the figure:

ffprobe -v quiet -show_entries format=duration movie.mp4 | grep "duration" | cut -d "=" -f 2

We have finally got a number in seconds for the video length!

Trim the length of time you desire via bc:

echo $(ffprobe -v quiet -show_entries format=duration movie.mp4 | grep "duration" | cut -d "=" -f 2) - 30.5 | bc

Now we can take this and plug it into our ffmpeg command:

ffmpeg -i movie.mp4 -t $(echo $(ffprobe -v quiet -show_entries format=duration movie.mp4 | grep "duration" | cut -d "=" -f 2) - 30.5 | bc) -c:v copy -c:a copy movie_out.mp4

Done!

One thought on “Trim Seconds of a Video with FFMPEG

  1. Your above example works perfectly, but using ffmpeg -h all, the “format=duration” doesn’t show up at all, and yet it takes the option.

    Thanks for the tip!

    Reply

Leave a Reply to vern Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>