PWSHub News

How to Trim and Cut Videos using FFmpeg in Linux - Make Tech Easier

A photograph of a desktop running a video editing program.

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.

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.

A screenshot of the VLC media player showing a comparison between the original video and its trimmed copy.

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.

A screenshot of the Kdenlive timeline showing a two-way cut.

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.

A terminal highlighting the modified first cut time for the FFmpeg command.

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

A terminal highlighting the modified second cut time for the FFmpeg 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.

A screenshot of the VLC media player showing the difference between the original video and the cut one.

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.

A screenshot of the VLC media player showing the uniform cuts on a single video file.

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.

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

Ramces Red

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.

Source: maketecheasier.com

Related stories
1 month ago - Want to trim down expenses without downsizing your small business? Check out these cost-cutting strategies specially designed for small businesses.
1 month ago - Although GarageBand isn’t accessible on Android, users can explore alternative apps with features akin to those found in the iOS app.
1 hour ago - Using voice memos to brainstorm ideas is quick, and easier than ever thanks to AI-powered apps. Here's how to get started.
1 week ago - Way back in January (remember January), I wrote a blog post describing how to use gen ai to improve image filenames. This worked by uploading the image to Google Gemini, asking for a short description, and using that description for a new...
Other stories
1 hour ago - Mistral Medium only came out two months ago, and now it's followed by Mistral Large. Like Medium, this new model is currently only available via their API. It scores well …
1 hour ago - Have you started learning React, only to face bugs that made you contemplate a career in goat herding? Don't worry – we've all been there. In this guide, you'll join me on a quest through the quirky wonders of React. I'll help you...
1 hour ago - The Geolocation API is a standard API implemented in browsers to retrieve the location of the people who are interacting with a web application. This API enable users to send their location to a web application to enable relevant...
1 hour ago - Explore the intricacies of reading and writing files in various formats, handling directories, mastering error-handling techniques, and harnessing the efficiency of streams for powerful, performance-boosting file operations.
1 hour ago - WordPress is a popular content management system (CMS) known for its flexibility and user-friendly interface. But its built-in editor is not ideal for collaborative editing. Many publishers who work with writers need collaborative writing...
5 hours ago - Performance testing is an important yet underrated field of software development. And it’s a must-have skill that can help you prevent common software failures that occur in production applications. Performance testing is a routine...