Put the last X seconds of a video file into a new one
February 23, 2016

It is often useful to be able to seperate the last X number of seconds from a video file. This is how to do it from a linux terminal using ffmpeg:

FILENAME=video.mp4
NEWFILENAME=videolastfewsecs.mp4
TIMEFROMENDOFVIDEO=20
END=$(ffprobe -i $FILENAME -show_format -v quiet | sed -n 's/duration=//p' | awk '{printf("%.0f\n", $1)}')
START=$((END-TIMEFROMENDOFVIDEO))
ffmpeg -i $FILENAME -ss 00:00:$START -t 00:00:$END -async 1 $NEWFILENAME

This would put the last 20 seconds of video.mp4 into a new file called videolastfewsecs.mp4, leaving the original file untouched.

Just change the first 3 varibles to change the settings.

Patrick G
Patrick G
I like to write code. I love open source software. I like to self-host stuff.