DEV Community


Posted on • Originally published at on

FFMPEG - Trimming and combining MKV files

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.

Trimming MKV files

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
Enter fullscreen mode Exit fullscreen mode

Let's break down the command in a bit more detail:

  • -i stand for 'input', this typically is followed by a file.
  • "input.mkv" is the name of the file for your input.
  • -ss is 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 ffprobe or just experiment to try and figure that out.
  • 00:00:00 This represents our starting time in the file. There's some documentation regarding the format of time duration.
  • -to this is used to specify time duration. In our example this is placed between two time durations
  • 00:10:00 Time duration, since this is after the -to this is the ending time.
  • -c copy specifies the codec to encode. In this example, we're copying the video and audio codecs.
  • "output.mkv" specifies the output file
  • -avoid_negative_ts 1 This 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:

  1. It is fast.
  2. It can be easily automated with most scripting languages. For example, you could easily use this in conjunction with powershell.
  3. There is no quality loss.

Combining (Concatenating) MKV Files

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.

  1. 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 Example.txt
  2. In the text file, we'll need to add the filenames in this format:
file 'Example1.mkv'
file 'Example2.mkv'
Enter fullscreen mode Exit fullscreen mode

NOTE: file is case sensitive!

  1. Run the following FFMPEG command:
ffmpeg -f concat -i Example.txt -c copy output.mkv
Enter fullscreen mode Exit fullscreen mode

As we did with our previous example, we'll break down the command into parts

  • -f specifies a filter, short for -filter
  • concat specifies using the concatenate filter. Documentation
  • -i Same as before, this is the input.
  • Example.txt We created this above. This is our input and ultimately maps out to our actual files.
  • -c copy copies all codecs
  • output.mkv specifies the output file

Hopefully you enjoyed this quick breakdown; thanks for reading!

Top comments (0)