How to Trim Videos with the Command Line Using AVConv

How to Trim Videos with the Command Line Using AVConv

Lets say you have a video that is too long and you want to cut out a piece from the middle without having to bother with importing the video into an editor, making some cuts, then exporting again. Not only is this annoying, you’ll be unnecessarily transcoding your video! There’s an easy way to trim videos from the command line using avconv. Simply use this command:

Let me give you an example of how we can use this. Suppose we have a video that has about 42 seconds of unwanted footage at the beginning.

41seconds

We’d need to set the -ss parameter to 00:00:42. This parameter accepts a time code value formatted as hours:minutes:seconds. It tells avconv that we only want to keep the video after 42 seconds in. Easy, right?

But wait, I forgot to mention that we have about 12 seconds of footage that we don’t want at the end of the video. We can use the -t parameter to specify the duration of video to keep. This is a bit tricky because -t is not the out point, it is the total duration of the output clip. Meaning what once was 00:00:42 for my input file is now 00:00:00 for the output file. So if I want to trim 12 seconds off of the end, I have to do a teeny tiny bit of math. An easy way to figure this out is to subtract your -ss start time from the outpoint you want to use and enter that as the -t parameter. So lets say the outpoint we want to use in the source video is 00:04:08. If we subtract 00:00:42 fromĀ 00:04:08 we get 00:03:26.

The final avconv command would look like this:

Now we have a trimmed video that is three minutes and twenty six seconds long. Just as we anticipated.

alltrimmed

Now I must confess something… This is how trimming a video with avconv would work in theory. In practice, I had to make a small change. For whatever reason, the audio was not copying over to my output video nicely, so I had to transcode it. If that happens to you, you can easily fix it by using this command:

With this command we have told avconv to copy only the video codec from the source file and transcode the audio to flac. Flac is a lossless codec, so I don’t feel too bad about transcoding it.

Learn With Us

We want to take you on a journey through the other side of the creative process: making a living. Explore the necessary evils of business as we develop a creative media studio using free and open source software. Sign up for our New Moon newsletter delivered on, you guessed it, every New Moon.

Conquer HTML with Daisy's Web Development Diary!

Defeating dragons, exploring the depths of the ocean, and advising robots about dating are just a few of the many adventures in Daisy's Web Development Diary. Explore the foundational web language, HTML. Learn how to build a game board with the table element, create forms for ordering sandwiches, link to exotic locations, and more.

Get It Now!
About the Author

How to Trim Videos with the Command Line Using AVConv was posted by on . I am one half of the creative force behind Dototot. I'm a writer, web programmer, composer, designer, and video editor with an interest in all things digital media.

Discuss this article in the Comments below, or and it with your friends. Learn more about , , and our creative process by subscribing to the New Moon Newsletter .

Recommended for you
Join the discussion
  • aenews

    Awesome was looking for a simple tool to trim an MKV without a loss in quality due to re-encoding. Only issue is that the first few seconds seem to have strange glitches in the output file for me. Plus there doesn’t seem to be an option to crop to the tenth/hundredth of a second which makes crops often impractical. Still nice post.

  • Jimmy

    I’m trying to trim my WEBM video, and if the seek time is 00:00:00 everything works fine, but anything that seeks to another position, the copy has a black video, and no audio. Any ideas?

  • Some codecs don’t work well with the trim features. You could try transcoding it to another format when you trim, but you’ll either have to transcode to a big, lossless file or lose a bit of quality.

Subscribe to our New Moon Newsletter

How do you make money doing what you love? Sign up for monthly emails exploring the necessary evils of business as we develop a creative media studio using free and open source software.

The email entered is invalid
×