How to Trim Video Using FFmpeg on Linux
Extracting a specific time segment from a video file is one of the most common tasks performed with FFmpeg, a powerful command-line tool available on Linux. This guide provides a straightforward walkthrough of the exact commands needed to cut a video based on specific start and end times or durations. Whether you want to re-encode the video for maximum precision or quickly slice it without losing quality, you will find the precise syntax and practical examples required to get the job done efficiently.
The Basic Command Structure
To extract a portion of a video, you primarily use two
time-specifying flags: -ss to define the start time and
-to (or -t) to define the end point or
duration.
The standard syntax looks like this:
ffmpeg -ss [start_time] -i input.mp4 -to [end_time] -c copy output.mp4Specifying Time Formats
FFmpeg accepts time inputs in two formats:
- Sexagesimal:
HOURS:MM:SS.MILLISECONDS(e.g.,00:01:30for 1 minute and 30 seconds). - Seconds: A simple integer or floating-point number
(e.g.,
90or90.5).
Method 1: Cutting Without Re-encoding (Fastest)
If you want to extract the segment instantly without losing any video
quality, you can use the stream copy mode (-c copy). This
tells FFmpeg to stream copy the video and audio tracks rather than
re-encoding them.
ffmpeg -ss 00:02:00 -i input.mp4 -to 00:05:00 -c copy output.mp4-ss 00:02:00: Starts the extraction at the 2-minute mark.-i input.mp4: Specifies the input file.-to 00:05:00: Cuts the video up to the 5-minute mark of the original video.-c copy: Copies the video and audio codecs directly without re-encoding.
Note on Flag Placement: Placing
-ssbefore the-iflag makes the operation much faster because FFmpeg seeks to the specified timestamp using keyframes, rather than reading through the entire video sequentially.
Method 2: Cutting with Re-encoding (Highest Precision)
While the copy method is incredibly fast, it can sometimes result in slight inaccuracies or a few frames of black screen at the beginning of the cut. This happens because FFmpeg must cut at the nearest “keyframe.”
If you need frame-accurate precision, omit the -c copy
flag. This forces FFmpeg to re-encode the segment, perfectly slicing it
at the exact millisecond you requested.
ffmpeg -ss 00:02:00 -i input.mp4 -to 00:05:00 output.mp4Method 3: Using Duration Instead of an End Time
If you prefer to specify how long the extracted clip should be,
rather than calculating the exact end timestamp, replace the
-to flag with the -t flag.
ffmpeg -ss 00:01:15 -i input.mp4 -t 30 -c copy output.mp4In this example, FFmpeg will start extracting at 1 minute and 15 seconds, and capture a segment exactly 30 seconds long, ending at 00:01:45.