Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having trouble finding a cli application that can take a video file (avi, mkv, and mp4 preferably) and cut out very short clips (2-6 seconds) with precision time accuracy. I've tried ffmpeg, mencoder, avidemux, and mp4box but they all cut on keyframes which creates 6+ second clips. Is there a tool that will re-encode the input file and cut the accurate time or cut inaccurately, re-encode, and then cut accurately?

share|improve this question
    
You'll probably have to reencode before cutting to get it right. You could probably speed things up by first cutting out the surrounding keyframes and only reencode the snippets. – Nifle Aug 7 '12 at 18:19
2  
Which FFmpeg command have you tried, exactly? I believe if you decode the video before (i.e. place the -ss parameter after -i), it should be more accurate. – slhck Aug 7 '12 at 18:19
    
The FFmpeg trick worked! I didn't realize the order mattered so much. Is this the same for any of the other tools? – curmil Aug 7 '12 at 19:30
up vote 13 down vote accepted

Cutting video with ffmpeg

You can accurately cut videos with FFmpeg. Since version 2.5 it's very easy. This would for example cut 10 seconds, starting from 0 minutes, 3 seconds and 123 milliseconds.

ffmpeg -ss -ss 00:00:03.123 -i input.mp4 -t 10 -c:v libx264 -c:a aac -strict experimental out.mp4

The position and the time may be either in seconds or in hh:mm:ss[.xxx] form.

Note that in these examples, video and audio will be re-encoded using the x264 and aac encoders.

You can also use -to instead of -t to specify the end point instead of the duration. In this case, however, -to is equivalent to -t, since by putting the -ss in front of -i, ffmpeg will first seek to that point and then start outputting.

See also the Seeking wiki entry.


Accurate cutting for older ffmpeg versions

If you have an older version of ffmpeg, then for accurate seeking, you need to place the -ss after -i, which makes the encoding process a little slower, because the entire video has to be decoded first:

ffmpeg -i input.mp4 -ss 00:00:03.123 -t 10 -c:v libx264 -c:a aac -strict experimental out.mp4

Here, -to and -t behave differently. -t 10 would create a ten second long clip, whereas -to 10 would create a clip that is seven seconds long.


Stream copying vs. re-encoding

You can try copying over the audio stream with -c:a copy too. Copying the video stream can be done with -c:v copy. It may however not work properly because you can only cut a video keyframes, which don't occur so often. In any case, it might lead to inaccurate cutting if you copy the video stream.

share|improve this answer
    
Instead of -c:v libx264 -c:a libfaac I think we can use -acodec copy -vcodec copy which tells ffmpeg just to detect and use the same codecs as the original file. Can anyone confirm? – Baodad Jan 23 '15 at 4:31
2  
@Baodad You can, but this will not accurately cut. When copying video/audio bitstreams, ffmpeg needs to start at a keyframe, which may just be placed at every second or even further apart. – slhck Jan 23 '15 at 7:14
    
How do overcome the "Unknown encoder 'libfaac'" error? – Doug Apr 24 '15 at 3:16
    
@Doug Choose a different encoder, for example -c:a aac -strict experimental. That's the simplest solution. – slhck Apr 24 '15 at 6:34

Like Baodad said in the comments (I post because it's not easy to find if you read quickly), the better approach is to detect the audio/video encoders automatically by ffmpeg, so :

ffmpeg -ss 00:05:17.18 -i in.mp4 -t 00:06:29.10 -acodec copy -vcodec copy out.mp4 
  • start @ 00:05:17.18
  • input = in.mp4
  • stop @ 00:06:29.10
  • output = out.mp4
share|improve this answer
1  
This does not reencode and does not provide frame accuracy. – Andrea Lazzarotto Feb 17 at 1:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .