32

I am looking for an app to help me chop bits off start and end of an mp4 file efficiently and losslessly.

e.g. is there something that lets me find a keyframe and copy the lot between that and another keyframe, without needing to re-encode the lot (slow and lossy), and without losing AV sync?

By mp4, I mean H264 video + AAC audio in an MP4 container

Pre h264 days Avidemux was an excellent tool for this as you can reuse the original encoded streams meaning no loss and take just a few seconds to create the trimmed version, but it is hopeless with h264 - opening one gives you two options: use safe mode (which loses AV sync massively) or use the dangerous mode which immediately crashes it.

AFAIK, Openshot, Kdenlive, Pitivi etc. will all do this by decoding and recoding (slow and lossy).

Seems a simple thing (probably showing my lack of knowledge here!), so was wondering if I'm missing something.

4

2 Answers 2

45

ffmpeg

ffmpeg -i source.mp4 -ss 00:00:00 -t 00:00:00 -vcodec copy -acodec copy splice.mp4

This will take source.mp4 and, using the same audio and video codecs and not degrading quality, take -t time after -ss start time video and output your splice.mp4

Please note that the timing is very sensitive to formatting (above is one example formatting). Also, in some distributions, ffmpeg has been replaced with avconv. Simply run the above command and replace the word ffmpeg with avconv to solve this.

Another excellent source of information on this topic. (Thanks @pbhj)

3
  • 3
    Brilliant. Note that you cannot abbreviate the -ss argument, i.e. 9:00 for 9 minutes must be expressed as 00:09:00 otherwise it is silently ignored. I think this answer should also be updated to use avconv, according to ffmpeg's warning. Thanks. Jan 5, 2014 at 21:00
  • Side note, that warning just reflects that you have avconv installed rather than ffmpeg, so your ffmpeg executable is actually just a wrapper around avconv and that wrapper may be deprecated soon. If you had ffmpeg installed but not avconv, you wouldn't get that warning. It's not intended to signify that ffmpeg itself is deprecated (though, recent versions of Ubuntu have chosen to install avconv by default rather than ffmpeg). Jun 1, 2015 at 1:36
  • 1
    +1, still working perfectly in 2021 on Ubuntu 20.04 with ffmpeg Feb 14, 2021 at 13:41
5

In Ubuntu 16.04 the equivalent of ffmpeg is the command avconv

To install it run:

sudo apt-get install libav-tools

Example command line to crop a video by time:

avconv -i originalFile.ogv -ss 00:00:00 -t 00:00:10 -vcodec copy -acodec copy croppedFile.ogv
1

You must log in to answer this question.

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