Trim video using ffmpeg

It might be over-complicated in some cases to fire up a huge video editing suite just to cut a section out of a video. Trimming a video does not have to be over complicated. The most tedious step is finding the start and end time, the rest is one simple command line away. With the help of ffmpeg this task becomes a simple command in the terminal.

Finding the start and end

Even the most tedious task of finding the exact time to cut or trim the video is easy with the tools included in the ffmpeg package. As described in frame by frame video playback, ffmpeg comes with a simple video player. Instead of trying to drag a cursor on a time line to hopefully find the right frame to cut the clip, ffplay can be used to identify the exact frame very precisely.

$ ffplay video-file.ext

With the player started, a window without controls will start. With its keyboard shortcuts, ffplay allows for some impressive control to navigate within the video. The frame by frame video playback post describes the options for precisely inspecting the video clip to identify the frame to cut or trim.

  17.77 A-V:  0.011 fd=   3 aq=   17KB vq=  824KB sq=    0B f=0/0

While the ffplay window does not provide any controls or details except the video content, the console provides the details needed for the next steps. The last line of the output, as shown above, provides details about the current position in the video clip. The important section in this line is the first number. It represents the current position of the playback in seconds. In the above example the video is at 17.77 seconds into the video clip.

Make note of the position the trimmed video clip should start and end. Those positions will be used to cut out the section of the video clip.

Trimming the video

With the start and end identified, the video clip can be trimmed. ffmpeg provides very simple command line arguments to cut a video. To be precise, ffmpeg will start to copy the audio and video stream from the start position until the end position into an output file. The original video clip file will not be modified by this process.

$ ffmpeg -ss 115.57 -to 508.86 -i video-file.ext -c copy trimmed_video.ext

The first argument in the above command is the “-ss” option specifying the seek position (start position) inside the input media file. It is important the the “-ss” option is specified before the “-i” option as the meaning of this option changes based on its position. While the “-ss” specifies where ffmpeg starts to read from the input media file, the “-to” defines where ffmpeg stops reading from the input file. Both arguments use an absolute time from the input media file.

With the “-i” option, the input media file, in this case the video file, is set. The position this argument is given indicates a separation between input-options and output-options. Arguments before “-i” are input options, like “-ss” and “-to” in the example, while the “-c” option is an output-option as it is specified after the “-i”.

“-c copy” instructs ffmpeg to copy all streams from the input media file to the output file. This option, because it is specified after the “-i”, is an output option. While the “-c” option normally specifies a codec to be used, the special value “copy” used here allows copying the stream as is without re-encoding it. This makes the process a lot faster.

The last option in the command line is the output file.

More details about the different options and the meaning can be found in the ffmpeg Main-option website or the ffmpeg(1) manpage.


Read more of my posts on my blog at https://blog.tinned-software.net/.

This entry was posted in Multimedia and tagged . Bookmark the permalink.