How do I crop (time not picture) an MP4 video file

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.


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)


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