How to Trim and Cut Videos using FFmpeg in Linux

Spread the love

FFmpeg is a powerful command-line multimedia editor for Linux. It allows you to record and manipulate audio, create screencasts, and even edit and encode high-bitrate video files. Here we show you how to quickly trim and do basic cuts to your video files with FFmpeg.

Content

Tip: learn more about video codecs and bitrate by optimizing your video file size using FFmpeg.

Trimming the Start and End of a Video File

One of the most powerful features of FFmpeg is its ability to trim segments from audio and video files quickly. This allows you to remove any unnecessary parts from your video and even automate it with shell scripts.

Before you start trimming, ensure that you have a backup of your original video file. This is because, unlike graphical editors, FFmpeg doesn’t provide any undo feature for any of its functions:

cp ~/my-video.mp4 ~/my-video-backup.mp4

To trim the first 30 seconds of a 5 minute video file, run the following command:

ffmpeg -ss 00:00:30 -to 00:05:00 -i my-video.mp4 -c copy my-video-trim.mp4

You can also translate this syntax to trim at the end instead of the start of your video file. Using the same video file, running the following will remove the last 30 seconds instead:

ffmpeg -ss 00:00:00 -to 00:04:30 -i my-video.mp4 -c copy my-video-trim.mp4

Aside from that, this syntax also supports trimming on both ends of the video. For example, removing the first and last 30 seconds of a 5 minute video will look something like this:

ffmpeg -ss 00:00:30 -to 00:04:30 -i my-video.mp4 -c copy my-video-trim.mp4

Once done, check whether FFmpeg properly trimmed your video by comparing it to your original file.

Tip: here is a list of the most useful FFmpeg commands for you to manage your video.

Cutting a Short Segment Inside a Video File

FFmpeg can also selectively cut segments inside a video file. On graphical editors, doing this usually requires you to create two separate cuts and manually join them in the video timeline.

Start by creating a backup of your original video file:

cp ~/my-video.mp4 ~/my-video-backup.mp4

Open your video on a media player and determine the exact timestamps for your cut points. In my case, I want to create a video file using only the footage from 2:30 to 4:00 and 7:45 to 8:30 of the original.

Convert your timestamps from minutes to seconds:

echo "00:02:30" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'

Navigate to the directory of your video file, then paste the following command:

ffmpeg -i my-video.mp4 \
-vf "select='between(t,FIRST-START-VALUE,FIRST-END-VALUE)+between(t,SECOND-START-VALUE,SECOND-END-VALUE)', setpts=N/FRAME_RATE/TB" \
-af "aselect='between(t,FIRST-START-VALUE,FIRST-END-VALUE)+between(t,SECOND-START-VALUE,SECOND-END-VALUE)', asetpts=N/SR/TB" \
my-video-cut.mp4

Replace the “FIRST-START-VALUE” and “FIRST-END-VALUE” for both -vf and -af flags with the value for your first cut. In my video, my first cut starts at 150 seconds and ends at 240.

Replace the “SECOND-START-VALUE” and “SECOND-END-VALUE” with the value for your second cut, then press Enter to run your command.

Note: You can add additional cuts by copying the “+between()” function on both -vf and -af flags along with a comma-separated list of your new cut times.

Confirm that your new video is properly cut by comparing it to your original file.

Good to know: FFmpeg can also deal with music. Learn how you can adjust and normalize your music files with FFmpeg.

Cutting a Video File into Uniform Segments

Aside from cutting and joining clips, you can also use FFmpeg to cut and trim an entire video into short segments. This can be useful if you have a long video that you split into smaller clips.

To start, open a terminal session then navigate to the directory of your long video.

Copy the following line of code to your terminal prompt:

ffmpeg -i my-video.mp4 -c copy -map 0 -segment_time 00:10:00 -f segment -reset_timestamps 1 my-video-%03d.mp4

Replace the value for “segment_time” with your target clip length. For instance, you can set this value to “00:05:00” to split your long video into 5 minute segments.

Note: This command will attempt to split your video file based on the closest keyframe to your target segment time. As such, this command may not always result in perfectly uniform segments.

Do you know you can
use FFmpeg to convert WebM format to other video formats? Here is how you can do so.

Learning how to do basic video manipulation in FFmpeg is just one of the cool things that you can do in Linux. Explore more of this operating system’s diverse ecosystem by looking at some of the best software for Linux.

Image credit: Sanjeev Nagaraj via Unsplash and Wikimedia Commons. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Ramces Red
Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Comments (1)