How to Trim a Video Using FFmpeg: A Beginner’s Guide
Contents
Did you know that 72% of people prefer watching videos over reading text to learn something new? These days, short-form videos are everywhere, from TikTok and Instagram to YouTube Shorts, and they perform way better than plain text or images—they get more clicks, longer watch times, and better engagement!
With video becoming such a big part of marketing, the demand for editing tools that are both flexible and efficient is growing fast. If you’re working on a video editing platform or tool, one feature you definitely don’t want to skip is trimming, as it’s a basic must-have for any proper video editor.
In this article, I’ll walk you through how to trim a video programmatically using FFmpeg, a free and powerful command-line tool. Let’s get started!
What is FFmpeg
FFmpeg is a powerful cross-platform command-line tool used for handling video and audio files. It can decode, encode, transcode, mux, demux, stream, filter, and play media in just about any format, and it works on all major platforms, including Linux, macOS, Windows, and more.
FFmpeg comes with a set of tools that let you convert, play, and analyse media files, as well as libraries for developers to use in various applications. These include:
- libavcodec
- libavutil
- libavformat
- libavfilter
- libavdevice
- libswscale
- libswresample.
🐱 Meow Memo: When you download the FFmpeg packages or executables to your machine, these libraries will be included automatically.
How to Install FFmpeg
Before you begin, make sure to download FFmpeg from the official website. You can follow this step-by-step guide to install FFmpeg on your Mac, Windows, or Ubuntu/Linux.
Once it’s installed, you can run ffmpeg -version
in your terminal/command prompt to verify the installation.
Trimming Basics: What Do the FFmpeg Options Mean
Before we jump into examples, here are a few flags you’ll need to understand:
ss
: Start time (where the trimmed clip begins)t
: Duration (how long the trimmed clip is)to
: End time (another way to mark where the clip ends)i
: The input filec copy
: Copy the codecs (used to avoid re-encoding)vf
: Apply a video filter (used when applying things liketrim
,crop
, or other transformations)
🐱 Meow Memo: You’ll usually see
-vf
used with filters liketrim
, which we’ll cover later in this article. It stands for video filter, and it tells FFmpeg to process the video stream with whatever transformation you specify.
Method 1: Using the ‘seeking (-ss)’ Option
The seeking (-ss) option can be used to seek within a video file to trim it, and there are several ways to use this option:
Input Seeking
Placing the seeking (-ss
) option before -i <input_video>
performs the seeking before decoding the input. This makes the trimming process faster, but the cut might not be super precise at the start because of keyframe limitations.
Basically, it starts from the specified start time and only saves the frames after that point. For example, the command below starts trimming from the 00:00:03
mark, so the output video will begin at 3 seconds and continue until the end:
ffmpeg -ss 00:00:03 -i input.mp4 output.mp4
Output Seeking
Placing the seeking (-ss
) option after -i <input_video>
performs the seeking after decoding the input. This approach processes the entire input video and discards frames after the specified start time. It ensures more accurate trimming (no keyframe dependency) but may be slower.
Here’s the command that does the same thing as the previous example, but using output seeking:
ffmpeg -i input.mp4 -ss 00:00:03 output.mp4
Avoid Re-encoding
By default, FFmpeg re-encodes both video and audio in these approaches. To avoid re-encoding and speed up the process, you need to explicitly tell FFmpeg to copy the streams with -c copy
:
Input Seeking
ffmpeg -ss 00:00:03 -i input.mp4 -c copy output.mp4
Output Seeking
ffmpeg -i input.mp4 -ss 00:00:03 -c copy output.mp4
Specify the Duration (-t)
We can specify the duration of the trimmed segment using the -t
option followed by the desired duration in seconds. For example, the commands below trim 3 seconds of the input video starting from 00:00:03
:
Input Seeking
ffmpeg -ss 00:00:03 -t 3 -i input.mp4 output.mp4
Output Seeking
ffmpeg -i input.mp4 -ss 00:00:03 -t 3 output.mp4
Specify the Duration (-to)
Alternatively, you can use -to
to indicate the ending timestamp. The commands below trim the video from 00:00:03
to 00:00:06
:
Input Seeking
ffmpeg -ss 00:00:03 -to 00:00:06 -i input.mp4 output.mp4
Output Seeking
ffmpeg -i input.mp4 -ss 00:00:03 -to 00:00:06 output.mp4
Method 2: Using the ‘trim’ Filter
Using the trim filter gives you frame-accurate (very precise) trimming because it's done through filtering. You trim the input video by specifying the duration or the start/end time of your desired output video, and FFmpeg discards frames outside the selected range.
Trim by Duration
To trim the video by duration, use the duration
parameter with the trim
filter and set the number of seconds. For example, the command below trims the video to just 3 seconds from the start:
ffmpeg -i input.mp4 -vf trim=duration=3 output.mp4
🐱 Meow Memo: By default, trimming will start from the beginning of the video unless you set a start time.
Trim by Start/End Time
To set a start time, use the start
parameter. The command below trims the video starting from the 3-second mark instead of the beginning:
ffmpeg -i input.mp4 -vf trim=start=3,setpts=PTS-STARTPTS output.mp4
🐱 Meow Memo:
setpts=PTS-STARTPTS
resets the timestamps in the output video so that it starts from00:00:00
. Without it, your trimmed video might begin at00:00:03
, and you’ll end up with a few seconds of black screen at the start.
On the other hand, to set an end time, use the end
parameter. The command below trims the input video to end at the 3-second mark:
ffmpeg -i input.mp4 -vf trim=end=3 output.mp4
Combining both start
and end
parameters, you can trim specific parts of the video. The command below trims the input video from the 3-second mark to the 6-second mark as the output:
ffmpeg -i input.mp4 -vf trim=start=3:end=6,setpts=PTS-STARTPTS output.mp4
🐱 Meow Memo: You can use the shorthand notation that omits the parameter names
start
andend
, e.g.,-vf trim=3:6
.
Tips for Running FFmpeg Commands Smoothly
If you’re new to FFmpeg or working with the command line, it’s normal to run into errors and feel a bit frustrated. To make your trimming process easier and save you some headaches, here are a few useful tips that can help you avoid common mistakes:
1. Double-Check the Input File Path
If your input video is in a different folder, make sure to include the correct path to the file. If you're unsure, it's safest to open your terminal in the folder where the video is saved and run the command from there.
2. Use -c copy
to Save Time
If you don’t need super precise trimming, you can add -c copy
to skip re-encoding. This makes the command run faster and keeps the original video quality as it is.
ffmpeg -ss 00:00:05 -i input.mp4 -c copy output.mp4
🐱 Meow Memo: If you need accurate trimming, especially at the frame level, you’ll have to re-encode.
3. Combine Options Carefully
FFmpeg reads options in the order you place them. For example, putting -ss
before or after -i
changes how the command behaves. So, it’s important to know what kind of result you want and arrange the options accordingly.
4. Not All Formats Support Copying
If -c copy
gives you errors, the container format might not support copying. In that case, try re-encoding as an alternative:
ffmpeg -ss 00:00:05 -t 3 -i input.mp4 -c:v libx264 -c:a aac output.mp4
Automating Video Editing with Templates and APIs
While FFmpeg gives you powerful control over trimming and other media manipulation functions, it might not be the most practical choice if your goal is to let users generate dynamic videos with custom content like names, quotes, or uploaded images. Instead of building everything from scratch with FFmpeg, try using Clipcat.
Clipcat is a video generation tool that lets you create videos programmatically using templates. You only need to design the video template once, then use its API to dynamically update elements like text (e.g., titles, captions, quotes), images (e.g, profile pictures, logos, product photos), and videos (e.g., user-uploaded clips or animated backgrounds).
This makes it perfect for use cases like:
- Auto-generating promotional videos for new products
- Creating personalized thank-you or testimonial videos
- Generating UGC-style content at scale for social media
Here are some examples of the Clipcat video templates:
Template 1
Template 2
Template 3
It’s a faster and more efficient way to create personalised videos without manually editing clips. The best part is, you can easily integrate it into your application or workflow using the API!
Final Thoughts
Trimming might seem like a small part of video editing, but it’s a crucial one. With FFmpeg, you have total control over how you want to trim the video, whether it’s quick cuts using the -ss
option or more precise edits using the trim
filter.
Here’s a quick comparison of the methods:
Method | Speed | Quality |
---|---|---|
-ss before -i |
Fastest (skips decoding before the trim point) | May cut between keyframes, causing slight inaccuracy or glitches at the start |
-ss after -i |
Slower (decodes everything before the cut) | More accurate cuts with cleaner playback at the trimmed start |
trim filter (-vf trim ) |
Slowest (decodes the whole segment, then trims) | Highest control and accuracy; best for frame-exact edits |
But if your goal is to let users generate videos with different content but the same layout efficiently, tools like Clipcat can save you a lot of time and hassle. With reusable video templates and a powerful API, Clipcat makes it easy to automate video creation and plug it straight into your application or workflow.
🐱 Meow Memo: Want to try Clipcat for your video projects? Sign up now and get 100 free API credits to get started!