FFmpeg is a remarkably versatile tool. You can use it to perform all kinds of amazing manipulations on video (and audio) files, if only you can work out the correct command line arguments. Unfortunately that’s the tricky part.
I recently needed to cut a few sections out of a pre-existing MP4 file to make it a bit shorter. It took me a few goes to find out how this can be done, but eventually I got something working.
First of all, make sure you have downloaded a build of FFmpeg. I used the static 32 bit Windows version from here. For convenience you’ll probably want to make sure
ffmpeg.exe is on your PATH.
In this example, the file we want to edit is called
input.mp4. And we want to keep three sections of the file: 2:00-9:28, 10:50-1:02:20 and 1:19:00-1:27:05.
We perform the operation in two stages. First we create three shorter mp4 files containing just the portions we want to keep. And then we stitch them all back together again.
Here’s the first part:
ffmpeg -i input.mp4 -ss 00:02:00 -t 00:07:28 part1.mp4 ffmpeg -i input.mp4 -ss 00:10:50 -t 00:51:30 part2.mp4 ffmpeg -i input.mp4 -ss 01:19:00 -t 00:08:05 part3.mp4
Now most of that is probably self-explanatory. The
-i switch introduces the input file name, the
-ss switch indicates the start time of the section we want to cut. The slightly tricky one is the
-t parameter, which is not the end time of the section you want to cut, but the duration. If you’re a C# programmer like me, you’ll just fire up LINQPad and enter a quick expression to calculate the difference between the start and end times:
TimeSpan.Parse("1:02:20") - TimeSpan.Parse("0:10:50")
And of course the final parameter is the output file.
So now we have our three parts, we need to join them together. There are a few different concatenation options in FFmpeg, and in our case it is the “concat demuxer” we need, as the “concat protocol” won’t work with mp4 files.
To use the concat demuxer we first need to create a simple text file containing the details of each file we want to concatenate. Let’s create
inputs.txt with the following content:
file 'part1.mp4' file 'part2.mp4' file 'part3.mp4'
Now we are finally ready to concatenate our files, and we can do that with the following syntax:
ffmpeg -f concat -i inputs.txt -c copy output.mp4
And that’s all there is to it. Hope someone finds this helpful, and if there is an even better way to achieve the same results (which I’m sure there is), do let me know in the comments.