Skip to content

Clip sections of a video with FFmpeg

Extracting specific sections or clips from a larger video is a common task in video editing. You may want to create clips from an existing video to share on social media. Or you may want to give users a preview in the form of a clip so they can get a sample of your content.

LinkMethod 1: Using the -ss and -t options

This is the most straightforward method for extracting a clip from a video.

bash
ffmpeg -i input_file.mp4 \ -ss 00:01:30 \ -t 00:00:30 \ -c copy output.mp4

Breakdown of the command:

  • -i input_file.mp4: Specifies the input video file
  • -ss 00:01:30: Sets the start time (1 minute and 30 seconds into the video)
  • -t 00:00:30: Sets the duration of the clip (30 seconds)
  • -c copy: Copies the streams without re-encoding (faster)
  • output.mp4: Name of the output file

LinkMethod 2: Using -ss and -to for precise end time

If you prefer to specify the exact end time instead of duration:

bash
ffmpeg -i input_file.mp4 \ -ss 00:01:30 -to 00:02:00 \ -c copy output.mp4

Here -to 00:02:00 specifies the end time of the clip (2 minutes into the original video)

LinkMethod 3: Re-encoding for higher precision

For more precise cutting, especially at the beginning of the clip, you can place the -ss option before the input file and re-encode:

bash
ffmpeg -ss 00:01:30 \ -i input_file.mp4 \ -t 00:00:30 \ -c:v libx264 \ -c:a aac output.mp4

This method is slower but can provide a more accurate cut, especially for the starting point.

LinkAdvanced techniques

LinkExtracting multiple clips

To extract multiple clips in one command:

bash
ffmpeg -i input_file.mp4 \ -filter_complex \ "[0:v][0:a]trim=start=00:01:30:end=00:02:00,setpts=PTS-STARTPTS[v1][a1]; \ [0:v][0:a]trim=start=00:03:00:end=00:03:30,setpts=PTS-STARTPTS[v2][a2]; \ [v1][a1][v2][a2]concat=n=2:v=1:a=1[outv][outa]" \ -map "[outv]" -map "[outa]" output.mp4

This command extracts two 30-second clips (1:30-2:00 and 3:00-3:30) and concatenates them.

LinkCutting without re-encoding at I-frames or keyframes

For faster cutting at I-frames:

bash
ffmpeg -i input_file.mp4 \ -ss 00:01:30 \ -to 00:02:00 \ -c copy -avoid_negative_ts make_zero output.mp4

The -avoid_negative_ts make_zero option helps maintain audio-video sync when cutting at non-keyframes.

LinkTips for successful video clipping

  1. Use the right time format: FFmpeg accepts various time formats. HH:MM:SS.mmm is the most readable.
  2. Consider keyframes: When using `-c copy`, FFmpeg will cut at the nearest keyframe, which might not be exactly where you specified.
  3. Re-encode for precision: If you need frame-accurate cuts, re-encode the video instead of using -c copy.
  4. Check your output: Always verify the output video to ensure it contains the exact clip you intended.
  5. Handle audio carefully: When cutting precisely, ensure that audio doesn't get out of sync, especially when starting cuts on non-keyframes.

LinkClipping with Mux

If you have videos hosted with the Mux Video API you can create clips from Assets and Live Streams.

No credit card required to start using Mux.