1. Overview
In this tutorial, we’re going to see how we can use the Linux command-line to cut videos. We’ll cover a few approaches to clipping videos using FFmpeg, which is a suite of tools provided for video manipulations.
2. Installing the Latest Version
The newer versions of FFmpeg support more options and filters. For that reason, we’ll need to have the latest version of FFmpeg. The ffmpeg package is available on package repositories for most distributions. We can use yum or apt to install FFmpeg.
Additionally, we can also use Snap to install the latest version of FFmpeg:
$ sudo snap install ffmpeg
Once FFmpeg is installed, we can verify it through the command:
$ ffmpeg -version
ffmpeg version n4.4 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11.1.0 (GCC)
3. Clipping with Re-Encoding
In basic terms, video encoding is the process of compressing and preparing a video for output, thus, making video sizes reasonably small and quick to process. With FFmpeg, we can cut a portion of a video, re-encode that portion, and, finally, save it as a file. By default, the re-encoding will use the codec used in the original video. Let’s see it in action:
$ ffmpeg -i my_video.mp4 -ss 00:00:15 -t 00:00:10 -async -1 clip.mp4
frame= 250 fps= 19 q=-1.0 Lsize= 5297kB time=00:00:09.98 bitrate=4346.5kbits/s speed=0.767x
video:5132kB audio:157kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.167893%
Now, let’s review each of the options and arguments we used here:
- The -i argument is used for specifying input files.
- The -ss argument seeks to the timestamp specified as the argument.
- The -t argument is used to specify the duration of the clip. In this case, it’s 10 seconds.
- The -async option specifies whether to contract or stretch the audio to match the timestamp. The value 1 will correct the start of the stream without any later correction.
The command above will process the my_video.mp4 input file and seek 15 seconds forward in the video. Next, it will cut the video from 00:00:15 to 00:00:25. The value of -t is added to the sought timestamp, which is 15 seconds. Finally, it will carry out an audio correction to match the timestamp and re-encode the video with the original my_video.mp4 codec.
Alternatively, if we need a more time-accurate cut, we can manually add the keyframes to the start and end of the clipped video:
$ ffmpeg -i my_video.mp4 -force_key_frames 00:00:15,00:00:25 clip.mp4
We used the -force_key_frames option because video clipping occurs at keyframes. However, if the first frame is not a keyframe, then the frames before the first keyframe in the clip will not be playable. Therefore, we forced FFmpeg to add keyframes at the first and last frames to ensure we encode a perfect clip. Moreover, to limit errors, we should avoid adding lots of keyframes.
We should note that re-encoding takes time depending on the size and the type of codec used. However, there’s an alternative that we’ll cover in the next section.
4. Clipping Instantly via Stream Copy
In some cases, we might not want to re-encode the video. Fortunately, FFmpeg allows for copying the codec from the original video to the trimmed video, which takes only a few seconds.
A video might have two codecs — one for the video track and another for the audio track. If we need to copy both codecs to the cut clip, we can simply use the -c option:
$ ffmpeg -i my_video.mp4 -ss 00:00:15 -to 00:00:25 -c copy clip.mp4
frame= 247 fps=0.0 q=-1.0 Lsize= 3455kB time=00:00:09.98 bitrate=2835.6kbits/s speed= 745x
video:3279kB audio:167kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.263277%
Let’s review the options we used in the command:
- The -to option specifies the end of the clip. In other words, the video will be cut from 00:00:15 to 00:00:25
- The -c option tells FFmpeg to copy both audio and video codecs to the clip.mp4 container from the my_video.mp4 container
The above command works well if we’re using the same container for both input and output videos. If we’re using different containers, we’ll be presented with a container mismatch error. Luckily, there’s a workaround. If we have two different containers, we can specify the copying options separately:
$ ffmpeg -i my_video.mkv -ss 00:00:15 -to 00:00:25 -acodec copy -vcodec copy clip.mp4
Mind that the input file is using an MKV container, whereas the output file is using an MP4 container.
5. Clipping Using the trim Filter
This is an alternative to the first method, which also re-encodes the cut video. It’s useful when we have a short video, preferably less than a minute, and we want to cut a small portion of it:
$ ffmpeg -i my_video -vf trim=10:25,setpts=PTS-STARTPTS clip.mp4
Taking a closer look at the options in the above command:
- The –vf option specifies that we’re using a video filter.
- We provided the trim filter with the value 10:25, which will slice the video from 00:00:10 to 00:00:25.
- The setpts filter sets the presentation timestamp for each video frame in the clip. In our case, we set its value to be PTS-STARTPTS to make sure our clip doesn’t delay or halt at the start, and the frames are synchronized relative to the setpts value, which is 0.
6. Conclusion
In this tutorial, we saw how we could cut a portion of a video based on providing timestamps. We looked at three different methods that we can make use of in different situations.