🎬 Overview #
FFmpeg is a powerful, command-line–driven multimedia toolkit widely used for video and audio processing on Linux and other platforms. One of its most common use cases is cutting or trimming video clips based on specific start and end times.
In this tutorial, we’ll explore several practical approaches to clipping videos with FFmpeg. We’ll cover methods that re-encode video for accuracy, techniques that copy streams instantly without re-encoding, and an alternative approach using FFmpeg’s filter framework. Each method serves a different purpose, depending on whether you prioritize speed, precision, or flexibility.
đź§° Installing FFmpeg #
Newer FFmpeg releases provide broader codec support, improved filters, and better timestamp handling. For best results, it’s recommended to install a recent version.
Most Linux distributions provide FFmpeg through their package managers, but Snap is a convenient way to install an up-to-date build:
sudo snap install ffmpeg
After installation, verify the version:
ffmpeg -version
A successful installation will display build details and enabled libraries.
✂️ Clipping with Re-Encoding #
Re-encoding is the most time-accurate way to cut a video. FFmpeg decodes the input stream, trims the desired segment, and then encodes it again—typically using the same codecs as the source file.
Here’s a basic example:
ffmpeg -i my_video.mp4 -ss 00:00:15 -t 00:00:10 -async -1 clip.mp4
Key options explained:
-ispecifies the input file-ssseeks to the start timestamp-tdefines the clip duration-async -1adjusts audio timing to match the trimmed segment
This command extracts a 10-second clip starting at 15 seconds into the original video. Because the content is re-encoded, this method ensures precise cuts even when timestamps do not align with keyframes.
Forcing Keyframes for Accuracy #
Video streams can only be cleanly cut at keyframes. If the clip starts between keyframes, playback issues may occur. To avoid this, you can force keyframes at specific timestamps:
ffmpeg -i my_video.mp4 -force_key_frames 00:00:15,00:00:25 clip.mp4
This ensures the clip starts and ends on valid keyframes, producing a clean, playable result. However, excessive keyframe insertion should be avoided to prevent unnecessary file size increases.
⚡ Instant Clipping with Stream Copy #
When speed matters more than frame-level precision, FFmpeg can copy audio and video streams directly without re-encoding. This approach is nearly instantaneous.
ffmpeg -i my_video.mp4 -ss 00:00:15 -to 00:00:25 -c copy clip.mp4
Important options:
-tospecifies the end timestamp-c copycopies both audio and video codecs as-is
This method works best when the input and output containers are the same and when cutting near keyframes.
Handling Different Containers #
If the input and output containers differ, specify audio and video codecs explicitly:
ffmpeg -i my_video.mkv -ss 00:00:15 -to 00:00:25 -acodec copy -vcodec copy clip.mp4
This avoids container mismatch errors while still skipping re-encoding.
🎛️ Clipping with the Trim Filter #
FFmpeg’s filter system provides another re-encoding-based approach using the trim filter. This method is especially useful for short videos or when combining trimming with other filters.
ffmpeg -i my_video.mp4 -vf trim=10:25,setpts=PTS-STARTPTS clip.mp4
Option breakdown:
-vfenables video filteringtrim=10:25cuts from 10 to 25 secondssetpts=PTS-STARTPTSresets timestamps so playback starts immediately
This approach offers fine-grained control but is generally less efficient for long videos.
âś… Conclusion #
FFmpeg provides multiple ways to cut videos by start and end time, each suited to different needs:
- Re-encoding delivers frame-accurate cuts
- Stream copy offers near-instant results with minimal processing
- Trim filters enable advanced, filter-based workflows
By choosing the right method, you can balance accuracy, performance, and flexibility when working with video clips on the command line.