Trimming Video Files Lossless Using ffmpeg

Recently, I needed to trim an H.264 video file of several megabytes size down to a few seconds worth without loss of quality. I simply didn't wanted to deal with this big video file in my editor and wasting space on an enormous proxy video file. Of course, ffmpeg immediately comes to mind, but how to tame that beast...?

Sure, ffmpeg is the right tool for this job. I still have not the slightest clue what this amazing piece of code can do and where its limits are ... but judging from the enormous list of command line options, its functions must be legion.

For my task at hand, I just needed to extract some part of a video file without any loss of quality. Put to task on one of my GoPro HD Hero 3 video files, the command line looks like this:
ffmpeg -i video.mp4 -ss 00:11:00 -t 00:01:00 -vcodec copy -acodec copy cutvid.mp4
The options are as follows:
  • -i input-file: name of the source (or input) video file, here: «video.mp4».
  • -ss start-timestamp: the timestamp in format HH:MM:SS, from which the video should be copied into a new file. If you leave out this option, then ffmpeg will copy right from the beginning of the source video file.
  • -t duration: duration of video to copy, again in format HH:MM:SS.
  • output-file: the name of the output video file to be created. For instance, in our example this will be «cutvid.mp4».
Since we want to copy without any loss the exact begin and duration of the output video will differ slightly from what we've specified on the command line. The reason is that if we don't want any loss, we don't want any transcoding to occur. Thus, we can start copying only at an I frame that is completely self-sufficient, so to say. Simply spoken, the following frames are deltas to this I frame until the next I frame starts the game anew. In consequence, the final length will most of the time also be greater than what we specified. However, this will be usually in the range of a few seconds, so it doesn't matter. We're throwing the output file at the video editor anyway. But we don't need to deal with the original, very big video file anymore.