FFMPEG is a really popular open source command line tool. Many free and paid versions of software make use of FFMPEG, whether we know it or not. It's likely you've used software that, under the hood, uses FFMPEG. Essentially, it's a collection of libraries/tools to help modify, process, and manipulate multimedia files.
A while back, I discovered FFMPEG when I was working on converting a bunch of my media to different containers (MKV to MP4) for my Plex server. I was surprised how easy it was to use. After discovering how useful it was for me, I made a tutorial on how to convert an MKV file to an MP4 file. This was my first 'how to' video and it was fairly well received, despite the minimal video editing and unprofessional feel. Feel free to like/subscribe, as I'd love to actually start creating more content on YouTube and developing a community.
I've continued using FFMPEG over the years. Recently I was working on capturing video using OBS, and wanted an easy method to trim and, in some cases, concatenate (combine) videos. This post will cover how to do these two tasks. This post will not cover setting up your system to use FFMPEG. If interested in a 'getting started' tutorial, let me know and I'll set aside some time to write a document.
There is a ton of documentation available for this tool, but it can be overwhelming. The basic syntax to trim a file using FFMPEG is:
ffmpeg -ss 00:00:00 -i "input.mkv" -to 00:10:00 -c copy "output.mkv" -avoid_negative_ts 1
Let's break down the command in a bit more detail:
-istand for 'input', this typically is followed by a file.
"input.mkv"is the name of the file for your input.
-ssis the time offset. In this example, it's placed before the -i (input option). This is done intentionally, but this may need to played around with. Doing SS's are forced to split on i-frames, and there's a lot of posts about how to use this in certain scenarios. For example, if you notice you have a couple seconds of audio before your video starts (video freezing) you might need to choose a different configuration or cut the video at the start of an i-frame. You can use
ffprobeor just experiment to try and figure that out.
00:00:00This represents our starting time in the file. There's some documentation regarding the format of time duration.
-tothis is used to specify time duration. In our example this is placed between two time durations
00:10:00Time duration, since this is after the
-tothis is the ending time.
-c copyspecifies the codec to encode. In this example, we're copying the video and audio codecs.
"output.mkv"specifies the output file
-avoid_negative_ts 1This ensures the time base is correct, however I have seen some online forums suggesting this may make it not start on a keyframe.
In our example command, this takes whatever file we give it, and outputs only the first 10 seconds of the file. This has a few advantages from using software:
- It is fast.
- It can be easily automated with most scripting languages. For example, you could easily use this in conjunction with powershell.
- There is no quality loss.
FFMPEG offers several ways to combine files, depending on the container/file type. In this example, we are focusing on MKV files. Not all methods FFMPEG provides will work in all scenarios.
This method (often called the demuxer approach) will involve creating a
.txt to reference our files we want to combine, and make use of ffmpeg filters. This is simply to show the diversity of the software; you can reference the files directly as well, though in this example, it would be a tad more complex, as it would require a complex filter, and while there are more complicated methods using filtergraphs, etc., we want to keep it simple. This tutorial assumes you have two videos that are essentially the same encoding, # of streams, etc.
- Create a text file, name does not matter, we just need to reference it. It's easiest to place the text file in the same directory as the files we want to combine. In our example below, we'll name the file
- In the text file, we'll need to add the filenames in this format:
file 'Example1.mkv' file 'Example2.mkv'
file is case sensitive!
- Run the following FFMPEG command:
ffmpeg -f concat -i Example.txt -c copy output.mkv
As we did with our previous example, we'll break down the command into parts
-fspecifies a filter, short for
concatspecifies using the concatenate filter. Documentation
-iSame as before, this is the input.
Example.txtWe created this above. This is our
inputand ultimately maps out to our actual files.
-c copycopies all codecs
output.mkvspecifies the output file
Hopefully you enjoyed this quick breakdown; thanks for reading!