A Beginner’s Guide to FFmpeg-Python: How to Trim Videos
Contents
If you're working with video in a Python project using FFmpeg, whether it’s trimming clips, converting formats, or handling uploads, using the Python bindings for FFmpeg, ffmpeg-python, can save you a lot of headaches. Instead of writing hard-to-read FFmpeg shell commands with subprocess, this wrapper allows you to build FFmpeg command pipelines using Python syntax, making it easier to integrate into any Python project.
With ffmpeg-python
, you can build video processing workflows directly in your Python scripts. It's easier to understand and more maintainable, especially if:
- You’re building a web app with Flask or Django that needs to compress or trim videos uploaded by users.
- You want to automate batch processing tasks like trimming, merging, or converting formats without messy CLI strings.
- You’re a data scientist or ML engineer who needs to preprocess hundreds of video files by extracting clips or frames for training.
Or maybe you just prefer writing readable, IDE-friendly code with proper error handling, something that's hard to get with raw CLI commands.
In this beginner-friendly guide, we’ll walk you through one of the most common tasks: how to trim a video using the ffmpeg-python
library. We’ll show you how to set everything up, write the Python script, and handle common issues.
Setting Up the Environment
Before we start, make sure both FFmpeg and the Python wrapper (ffmpeg-python) are installed on your machine:
- FFmpeg is the actual engine that handles all the video and audio work, like reading files, trimming, encoding, and more.
- 'ffmpeg-python' is the Python wrapper that lets you build FFmpeg commands using Python code, so you don’t have to write them out in the command line.
Step 1. Install FFmpeg
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 or Command Prompt to verify the installation.
Step 2. Install ‘ffmpeg-python’
You can install ffmpeg-python
using pip:
pip install ffmpeg-python
This package gives you access to a Pythonic interface to FFmpeg's functionalities.
Step 3. Test Your Setup
Open a Python shell or create a Python script, and execute the code below:
import ffmpeg
print(ffmpeg)
If this runs without error, your setup is complete!
Understanding Video Trimming with FFmpeg
Before jumping into code, let’s first look at a few flags you’ll need to understand for trimming videos using FFmpeg:
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)
Below is a basic CLI example for trimming a video file using the seeking (-ss) option :
ffmpeg -ss 00:00:03 -i input.mp4 output.mp4
This tells FFmpeg to trim the input file starting at 3 seconds (-ss 00:00:03
) and save it as output.mp4.
How to Trim Videos with FFmpeg in Python
Let’s do the same thing in Python. The code below uses the subprocess
module to run the same FFmpeg command in Python:
import subprocess
command = [
'ffmpeg',
'-ss', '3',
'-i', 'input.mp4',
'output.mp4'
]
subprocess.run(command, check=True)
While using subprocess
to run an FFmpeg command in Python may seem straightforward in the example above, it can quickly become difficult to manage and prone to errors as the complexity of the FFmpeg command increases. It can also sometimes result in platform-specific issues as the command-line syntax may differ depending on the operating system (Windows vs. Linux/macOS).
Using the ‘ffmpeg-python’ Library
On the other hand, using ffmpeg-python
is generally cleaner and more maintainable with the Python syntax. The code below does the same thing as the earlier one, but this time using ffmpeg-python
:
import ffmpeg
(
ffmpeg
.input('input.mp4', ss=3)
.output('output.mp4')
.run()
)
This version is easier to read, especially if you’re not very familiar with FFmpeg’s CLI syntax. And you don’t have to worry about argument ordering or handling quotation marks!
🐱 Meow Memo: You can also write the timestamp in full format like
00:00:03
instead of just3
, e.g.,ss='00:00:03'
.
More Examples of Using 'ffmpeg-python' to Trim Videos
Now that you’ve got the basics of using ffmpeg-python
, let’s look at more practical examples of how to trim videos using different options and settings.
1. Avoid Re-encoding with c copy
By default, FFmpeg will re-encode both the video and audio when creating the output. If you want to speed things up and skip re-encoding, you can tell FFmpeg to copy the original streams using c copy
:
import ffmpeg
(
ffmpeg
.input('input.mp4', ss=3)
.output('output.mp4', c='copy')
.run()
)
🐱 Meow Memo: Using
c copy
to skip re-encoding makes the process faster, but it might not be as accurate as a full re-encode.
2. Trim Using Duration t
To trim a specific part of the video, you can use the t
option followed by the duration in seconds. For example, the code below will trim a 10-second segment starting from the 3-second mark (00:00:03
) of the input video:
import ffmpeg
(
ffmpeg
.input('input.mp4', ss=3)
.output('output.mp4', t=10, c='copy')
.run()
)
3. Trim Using End Time to
Alternatively, you can use to
to specify the end time of the trimmed segment instead of duration. For example, the code below will trim the video from 00:00:03
to 00:00:06
:
import ffmpeg
(
ffmpeg
.input('input.mp4', ss=3)
.output('output.mp4', to=6, c='copy')
.run()
)
4. Frame-Accurate Trim with trim
Filter
Using the trim
filter gives you frame-accurate trimming compared to the previous examples that use seeking. You just need to set the duration (or start and end time) of the part you want, and FFmpeg will drop everything outside that range.
Trim by Duration (No Audio)
import ffmpeg
(
ffmpeg
.input('input.mp4')
.filter('trim', duration=3)
.output('output.mp4')
.run()
)
Trim by Duration (With Audio)
Video and audio are handled separately when using filters like trim
. The trim
filter only applies to the video, so your output won’t have any audio. If you want to include audio as well, you’ll need to use the atrim
filter:
import ffmpeg
input_stream = ffmpeg.input('input.mp4')
video = input_stream.video.filter('trim', duration=3)
audio = input_stream.audio.filter('atrim', duration=3)
(
ffmpeg
.output(video, audio, 'output.mp4')
.run()
)
Trim by Start/End Time (No Audio)
To trim a video by a start time, use the start
parameter. The command below trims the video starting from the 3-second mark to the end:
import ffmpeg
(
ffmpeg
.input('input.mp4')
.filter('trim', start=3)
.output('output.mp4')
.run()
)
To trim by end time instead, use the end
parameter. The command below trims the input video to end at the 3-second mark:
import ffmpeg
(
ffmpeg
.input('input.mp4')
.filter('trim', end=3)
.output('output.mp4')
.run()
)
To trim a specific section of the video, set both the start and end times. The example below shows how to trim the video from the 3-second mark to the 10-second mark:
import ffmpeg
(
ffmpeg
.input('input.mp4')
.filter('trim', start=3, end=10)
.filter('setpts', 'PTS-STARTPTS')
.output('output.mp4')
.run()
)
🐱 Meow Memo: The
setpts=PTS-STARTPTS
filter resets the timestamps in your output video so it starts at00:00:00
. Without this, your trimmed video might start at00:00:03
, and that can cause a few seconds of black screen at the beginning.
Common Errors and Fixes
In this section, we’ll go through some common issues you might face, and how to fix them:
“Error opening input: No such file or directory”
FFmpeg can’t find the input file you gave. Maybe the file name has a typo, or your script is running in a different folder from where the video is saved.
How to Fix It:
Double-check your file name and path. To be safe, you can also use the full (absolute) file path to avoid any confusion:
import ffmpeg
(
ffmpeg
.input('/Users/path/to/folder/input.mp4', ss=3)
.output('output.mp4')
.run()
)
Or on Windows:
import ffmpeg
(
ffmpeg
.input('C:\\Users\\path\\to\\folder\\input.mp4', ss=3)
.output('output.mp4')
.run()
)
🐱 Meow Memo: Make sure to use double backslashes (
\\
) on Windows to escape your slashes.
Trimmed Video Has No Video or No Audio
This might be because the trimming started from a point that’s not a keyframe when using c copy
, which skips re-encoding but is not always accurate.
How to Fix It:
Try trimming with re-encoding (it can be a new codec too):
import ffmpeg
(
ffmpeg
.input('input.mp4', ss=3)
.output('output.mp4', t=10, vcodec='libx264', acodec='aac')
.run()
)
Conclusion
Working with media files using FFmpeg doesn’t have to mean struggling with confusing command-line commands. With the ffmpeg-python
library, you can use FFmpeg’s powerful features in clean and readable Python code.
As your media processing task becomes more complex, this kind of structure and readability will save you time, reduce bugs, and make integration into existing applications or workflows easier!
👉🏻 Check out our blog for more FFmpeg tutorials on how to edit, convert, and manipulate media files.