Apr 09

Simple video editing with ffmpeg (avconv)

Category: Linux  ,, — Published by goeszen on April 9, 2012 at 3:20 pm

If you want to trim, cut, chop or in any way edit a video on the command line, these commands here help you to get simple editing done with ffmpeg:
(If you're looking at combining or merging videos with avconv, additive video editing, read this article.)

Editing a section out from the middle of a video (with re-encoding):

$ ffmpeg -i <input> -ss hh:mm:ss.000 -t hh:mm:ss.000 -sameq <output>

(-ss seek to position, -t duration of relevant segment, -sameq maintain quality of input)

You might also use -vcodec copy -acodec copy to 1:1 edit out data from the input, but I've found that most videos don't have keyframes where you expect them, for example with one mov I've edited, the result was all black as the video didn't render. So on editing video, you seem to have to re-encode with most video to get what you expect, loosing a generation in the process.

Editing a section out from the middle of a video (without re-encoding, with "copy"):

$ ffmpeg -i <input> -ss hh:mm:ss.000 -t hh:mm:ss.000 -acodec copy -vcodec copy <output>

So, if you want to losslessly copy /edit out a section from a video, you would use the above "copy" syntax. But please keep in mind that this does a raw "dump" of the data found at the specified section. As far as I know, ffmpeg doesn't offer a built-in function to seek-back at the starting position to the last keyframe and it can't re-encode just the section that has now starting keyframe (a technique commonly called 'soft copy'), so the outputted video file might start with garbled data or simply black video (might depend on how your media player renders garbled data/ data without a starting keyframe...).

Be aware of the new -to switch

More recent versions of avconv and ffmpeg offer the -to switch, which is a huge improvement over working with -ss and -t alone. So far you had to calculate the duration (specified by -t) of the section you wanted to edit out by subtracting the seek in-point from the timecode/seek-offset of the out-point (relative). The new -to switch allows you to specify the out-point as an absolute timecode value.

Example: $ avconv -i video.mp4 -ss 00:01:54 -to 00:22:34 -codec copy out.mp4

1. ffmpeg is being renamed these days. In the future, the functionality found in ffmpeg will be in avconv!
"*** THIS PROGRAM IS DEPRECATED ***  This program is only provided for compatibility and will be removed in a future release. Please use avconv instead."

2. There's a difference in where you use the -ss, in front or after the -i option. This wiki article from ffmpeg's docs has more about it. Basically, the "seek before the -i" tells ffmpeg to seek fast, based on keyframes, while the "seek after the -i" means seeks "frame by frame" (slowly). To get both of the worlds, you can combine the fast and the slow seek option by stating two seeks (-ss). Again, more here.

3. Regarding sub-second fractions in the timecode syntax, be aware of this bit from the docs! "(I)f you use a fraction, like 02:30.05 this is interpreted at '5 100ths of a second' not as frame 5 (for instance 02:30.5 would be 2 minutes, 30 seconds, and a half a second)."


Leave a Reply