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.

Eric Leschinski
  • 2,221
  • 1
  • 20
  • 24
artfulrobot
  • 8,543
  • If you need to chop the end of the video, please see the answer i gave on another topic: http://askubuntu.com/questions/412558/ffmpeg-video-trim-from-the-end-of-the-video – v010dya Nov 09 '14 at 13:11
  • 2
    http://superuser.com/questions/377343/cut-part-from-video-file-from-start-position-to-end-position-with-ffmpeg relates well and offers a similar solution to that below but adds some good info. – pbhj May 06 '15 at 22:34
  • Just found this Video Trimmer – A Stupid Simple App to Cut a Clip Out of a Video in Linux – UbuntuHandbook - https://ubuntuhandbook.org/index.php/2021/08/video-trimmer-stupid-simple-app-cut-clip-video-linux/amp/ – artfulrobot Aug 28 '21 at 07:32

2 Answers2

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)

earthmeLon
  • 11,247
  • 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. – artfulrobot Jan 05 '14 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). – thomasrutter Jun 01 '15 at 01:36
  • 1
    +1, still working perfectly in 2021 on Ubuntu 20.04 with ffmpeg – Jon Bentley Feb 14 '21 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