Trimming a clip to a specific start and end time is one of those everyday developer tasks that pops up in video workflows, CI jobs, and batch scripts. If you have ever clipped intros, grabbed highlights, or split long recordings, you have probably run into FFmpeg flags that look similar but behave differently.
Hi all,
I have a long MP4 and I need to extract a specific segment without messing up audio sync or re-encoding unless I must. What is the correct command and the common pitfalls?
In short: how to cut a video by time with FFmpeg, including fast keyframe-based cuts and frame-accurate trims, and what flags should I use for best results?
Thanks!
FFmpeg offers two primary approaches for time-based cuts. Let’s break them down:
Use stream copy for speed. This is ideal when you can start on or near a keyframe and accept slight inaccuracy at the first frame.
# Cut from 3s to 10s (duration 7s) using stream copy
ffmpeg -ss 3 -i input.mp4 -t 7 -c copy -avoid_negative_ts make_zero cut-fast.mp4Code language: CSS (css)
- Pros: Instant, no quality loss, low CPU.
- Cons: Start may snap to the previous keyframe, which can be a few frames early.
Tip: If your container or codecs are not friendly to stream copy at arbitrary points, re-encode the video and audio instead of using -c copy.
Place -ss after the input for accuracy and re-encode. Use -t for duration or -to for an absolute end time.
# Accurate: from 3s to 10s by re-encoding
ffmpeg -i input.mp4 -ss 3 -to 10 \
-c:v libx264 -crf 20 -preset veryfast \
-c:a aac -movflags +faststart cut-accurate.mp4Code language: CSS (css)
-crfcontrols visual quality for H.264: lower is higher quality. 18 to 23 is common.-presettrades speed for efficiency. veryfast through slow are typical picks.-movflags +faststartoptimizes MP4 playback over the web.
- Times can be seconds or HH:MM:SS.xx
- Using duration:
-ss 00:00:03 -t 7 - Using end time:
-ss 00:00:03 -to 00:00:10
To preserve all streams including audio, subtitles, and data:
ffmpeg -ss 3 -i input.mkv -t 7 -map 0 -c copy cut-all-streams.mkv
If changing containers, ensure target formats support the codecs you keep. For background on FFmpeg concepts and tradeoffs, see FFmpeg features, pros and cons.
- If audio drifts, prefer re-encoding with
-ssafter-i. - For batch clipping, wrap these commands in a shell or Python script and feed in start and end times from a CSV or JSON.
- If you need the first output frame to be the exact requested frame, re-encode. Keyframe-aligned cuts are fast but approximate.
If you want to trim without maintaining your own servers or pipelines, you can generate segments at request time using a media platform. For example, with Cloudinary you can transform and deliver clips via a URL or API, and pair the trim with format conversion, bitrate control, or thumbnail generation.
Example: trim 7 seconds starting at 3 seconds using a delivery URL:
https://res.cloudinary.com/demo/video/upload/so_3,du_7/sample.mp4
Programmatic example using a Node SDK pattern:
// Build a trimmed delivery URL
// start_offset: 3 seconds, duration: 7 seconds
const url = cloudinary.url('sample', {
resource_type: 'video',
transformation: [{ start_offset: 3, duration: 7, fetch_format: 'mp4' }]
});
// Use this URL in your player or return it from your API.Code language: JavaScript (javascript)
This approach offloads encoding and caching, helps standardize outputs, and works well when you dynamically clip highlights or social cuts at scale.
- Output length wrong: Make sure you aren’t mixing
-toand-tin confusing ways. Use one or the other. - No audio or missing subtitles: Add
-map 0to keep all streams or map them explicitly. - Poor quality after re-encode: Adjust
-crf,-preset, or choose a different codec depending on your delivery needs.
- Need speed and no re-encode:
ffmpeg -ss START -i in -t DURATION -c copy out.mp4but accept keyframe-based starts. - Need frame accuracy: Put
-ssafter-iand re-encode video and audio. - Use
-map 0to keep all streams and-movflags +faststartfor web playback. - Cloudinary can trim on the fly and combine with transcoding, thumbnails, and caching.
Ready to trim, transcode, and deliver video at scale with on-the-fly transformations? Sign up for a free Cloudinary account and start optimizing your video workflow today.