Video Editing

10 min read

How to trim video using FFmpeg?

Learn how to easily trim and cut your videos using FFmpeg. Our guide provides methods and step-by-step instructions to help you create precise video clips for various purposes.

ffmpeg trim video
How to trim video using FFmpeg?

FFmpeg is a powerhouse for multimedia processing and a top solution for video trimming. With its rich array of filters, you can make edits like resizing, cropping, colour correction, and more to your videos. But FFmpeg isn’t just about filters/editing—it's also great for live streaming, screen recording, and managing metadata. In this article, we'll learn how to trim video with ffmpeg in different scenarios.

Why use FFmpeg for video trimming?

FFmpeg is a versatile and powerful command-line tool that offers several advantages for video trimming:

  • Efficiency: FFmpeg is designed for high-performance video processing, making it a fast and efficient choice for trimming videos.
  • Flexibility: It supports various video formats, codecs, and resolutions, allowing you to work with various video files.
  • Precision: FFmpeg provides precise control over video trimming, enabling you to specify exact start and end times for your clips.
  • Automation: You can use FFmpeg to automate video trimming tasks, saving time and effort for repetitive operations.
  • Integration: FFmpeg can be integrated into scripts, workflows, and other applications, making it a valuable tool for video processing pipelines.

Overall, FFmpeg's combination of speed, flexibility, precision, and automation capabilities make it an excellent choice for video trimming needs.

How to trim Video with FFMpeg?

This guide will help you install FFmpeg, locate your video files, and perform basic operations like trimming.

To begin with, let's talk about installation on different platforms:

  1. On Windows: You have a few options to install FFmpeg:
    1. Download the FFmpeg executable directly from the official website.
    2. Use package managers like Chocolatey or Winget. For example:
choco install ffmpeg
winget install ffmpeg
  1. On Mac: Homebrew is the easiest way to install FFmpeg. Open the terminal and run:
brew install ffmpeg
  1. On Linux: Install FFmpeg via your distribution’s package manager. For Ubuntu, you can use:
sudo apt install ffmpeg

Next, you need to find the video file or URL.

  • For online videos, use tools like youtube-dl or browser extensions like m3u8 sniffer to get the video URL.
  • Make sure FFmpeg is accessible from any location by adding it to your system’s PATH variable or placing the ffmpeg executable in the same directory as your video files.

Now, let's move to performing ffmpeg cut video operations. To trim a segment from a video, we use the following command. This example trims from 1 minute to 1 minute and 30 seconds:

ffmpeg -ss 00:01:00 -i example.mp4 -to 00:01:30 -c:v copy -c:a copy trimmed_example.mp4

Let's discuss the 4 methods involved in trimming videos with FFmpeg examples:

Method 1: Using the -ss and -t options

Trimming videos with FFmpeg can be efficiently done using the -ss and -t options. These options allow you to seek to a specific start time and define the duration of the segment you want to keep.

Input seeking involves placing the -ss option before the input file (-i). This tells FFmpeg to seek to the specified start time before they begin processing the input video. However, this makes it slightly less accurate in terms of frame precision.

Trimming from the Beginning

To ffmpeg trim the beginning of the video, you need to use the -ss option, which will let you specify the start time and -t, which is to set the duration of the segment you want to keep.

Example:

If you want to trim the first 10 seconds of a video and keep the rest, you can use:

ffmpeg -ss 00:00:10 -i myclip.mp4 -c:v copy -c:a copy trimmed_start.mp4

This command skips the first 10 seconds of myclip.mp4 and keeps the rest of the video. The -c:v copy -c:a copy options are used to avoid re-encoding, which speeds up the process.

Trimming from a Specific Point to the End:

To trim a video from a specific point (say, the 5-second mark to the end), here's the command to use:

ffmpeg -ss 00:00:05 -i myclip.mp4 trimmed_output.mp4

Trimming to a Specific End Time:

You can use the -to option to specify the end time of the trim. For example, to trim from the 5-second mark to the 9-second mark:

ffmpeg -ss 00:00:05 -to 00:00:09 -i myclip.mp4 trimmed_to.mp4

Alternatively, output seeking places the -ss option after the input file (-i). This method processes the entire input video and discards the frames before the specified start time. It is generally more accurate but can be slower for large files.

When trimming videos, you need to consider the codecs used and check whether or not they are compatible. If you need to avoid re-encoding, please include the command line -c:v copy and -c:a copy. This will retain the original quality and pace up the process.

For example, 

ffmpeg -ss 00:00:05 -t 4 -i myclip.mp4 -c:v copy -c:a copy output_trimmed_copy.mp4

Key Takeaways

  • This is the most straightforward and ideal method for basic trimming tasks. 
  • It is typically useful for specifying the start time and duration of the segment you want to trim.
  • Input Seeking: Faster but may be less accurate. Use -ss before -i.
  • Output Seeking: More accurate but can be slower for large files. Use -ss after -i

Method 2: Using the trim filter

FFmpeg's `libavfilter` library includes the `trim` filter, which provides flexibility for cutting segments from your video. This filter lets you specify durations or exact start and end times for your trim.

Trimming by Duration

You can use the `trim` filter to set a specific duration for your video clip. By default, this will be trimmed from the beginning of the video.

Example Command

ffmpeg -i myvideo.mp4 -vf "trim=duration=4" output_trimmed.mp4

This command trims `myvideo.mp4` to the first 4 seconds.

Just so you know, using the filter means the video will be re-encoded. To preserve the original audio, use:

ffmpeg -i myvideo.mp4 -vf "trim=duration=4" -c:a copy output_trimmed.mp4

Trimming by End Time

Use the `end` parameter to trim a video to a certain end time. This trims from the start to the specified end time.

Example Command:

ffmpeg -i myvideo.mp4 -vf "trim=end=5" output_end_trimmed.mp4

This trims the video so that it ends at 5 seconds.

Trimming by Start Time

If you need to start trimming from a specific point, you'll need to employ the `start` parameter. This command also requires `setpts=PTS-STARTPTS` to reset timestamps, ensuring that the output video starts at 00:00:00.

Example Command:

ffmpeg -i myvideo.mp4 -vf "trim=start=2,setpts=PTS-STARTPTS" output_start_trimmed.mp4

This trims the video starting from 2 seconds and resets the timestamps.

Trimming by Start and End Times

To cut a specific section of your video, use both `start` and `end` parameters. This allows you to define the exact segment you want to keep.

Example Command:

ffmpeg -i myvideo.mp4 -vf "trim=start=2:end=5,setpts=PTS-STARTPTS" output_segment_trimmed.mp4

This trims the video from 2 seconds to 5 seconds.

You can also use a shorthand notation for this command:

ffmpeg -i myvideo.mp4 -vf "trim=2:5,setpts=PTS-STARTPTS" output_segment_trimmed.mp4

Pros and Cons compared to -ss and -t

Pros

  • Provides detailed control over trimming parameters.
  •  Can be combined with other video processing filters for more advanced editing.

Cons

  • May be less efficient than `-ss` and `-t` for simple trimming tasks.

Method 3: Using the -to option

The -to option in FFmpeg is a straightforward way to trim videos wherein you specify an end time. This is useful when you want to specifically want to extract a segment of a video between a start and end point. 

Here’s how to use it:

ffmpeg -i sample.mp4 -ss 00:02:15 -to 00:07:45 -c:v copy -c:a copy trimmed_output.mp4

In this example, -ss 00:02:15 sets the start time to 2 minutes and 15 seconds, while -to 00:07:45 sets the end time to 7 minutes and 45 seconds. The resulting trimmed_output.mp4 will include the segment from 2:15 to 7:45, which is 5 minutes and 30 seconds long.

  • If the specified end time with -to exceeds the video’s length (e.g., -to 00:20:00 for a 15-minute video), FFmpeg will trim the video up to its actual end. It will not extend beyond the video's duration.
  • If you specify an end time earlier than the start time (-to < -ss), FFmpeg will not execute the command and will return an error saying “ -to value smaller than -ss; aborting.”

Comparison with -ss and -t:

When -ss is placed before -i, the -to option works similarly to -t. It specifies the duration from the start time. For example:

ffmpeg -ss 00:04:00 -i sample.mp4 -t 00:08:00 -c:v copy -c:a copy duration_trimmed.mp4

This command trims the video starting at 4 minutes for a duration of 8 minutes, ending at 12:00. 

The -to option is similar to using -ss with -t, but it focuses on defining the end point. Using -to in combination with -ss allows for precisely trimming specific video segments. For instance:

ffmpeg -ss 00:02:15 -i sample.mp4 -to 00:07:45 -c:v copy -c:a copy precise_trimmed.mp4

Pros and Cons

Pros

  • Focuses on defining the endpoint for trimming.
  • Can be combined with -ss for precise trimming.

Cons

  • Less versatile than the trim filter.
  • Trimming is typically not always perfectly accurate. To improve accuracy, we recommend you use the -accurate_seek option. Without -accurate_seek, some parts before the seek point might be preserved.

Method 4: Using the -sseof option

The -sseof option is similar to -ss, but instead of trimming the input video from the beginning, it trims the video from the end. It takes a negative value that indicates a position before the end of the file. This can be useful when you want to process or trim the last few seconds of a video.

Here’s an example that trims the last 2 seconds of the input video as output:

ffmpeg -sseof -2 -i input.mp4 output.mp4

You can also use the HH:mm:ss format:

ffmpeg -sseof -00:00:02 -i input.mp4 output.mp4

How to trim multiple video segments from a video?

When you need to trim multiple segments from a video—such as removing specific portions like commercials—you can use FFmpeg’s filter_complex tool. It allows precise control over video editing of multiple files.

Here's the basic syntax to do so:

ffmpeg -i input_file -filter_complex \
"[0:v]trim=start=<start1>:end=<end1>,setpts=PTS-STARTPTS[v1]; \
 [0:v]trim=start=<start2>:end=<end2>,setpts=PTS-STARTPTS[v2]; \
 [v1][v2]concat[v3]; \
 [0:v]trim=start=<start3>,setpts=PTS-STARTPTS[v4]; \
 [v3][v4]concat[output]" -map [output] output_file

The syntax is similar for audio operations as well. Let’s say you have a video file called movie.mp4, and you want to trim the following segments:

  • The first 20 seconds
  • A segment from 50 to 70 seconds
  • From 120 seconds to the end of the video

Example Command:

ffmpeg -i movie.mp4 -filter_complex \
"[0:v]trim=duration=20[v1]; \
 [0:v]trim=start=50:end=70,setpts=PTS-STARTPTS[v2]; \
 [v1][v2]concat[v3]; \
 [0:v]trim=start=120,setpts=PTS-STARTPTS[v4]; \
 [v3][v4]concat[output]" -map [output] trimmed_movie.mp4

To trim both video and audio, 

ffmpeg -i movie.mp4 -filter_complex \
"[0:v]trim=duration=20[v1]; [0:a]atrim=duration=20[a1]; \
 [0:v]trim=start=50:end=70,setpts=PTS-STARTPTS[v2]; [0:a]atrim=start=50:end=70,asetpts=PTS-STARTPTS[a2]; \
 [v1][v2]concat[v3]; [a1][a2]concat=a=1:v=0[a3]; \
 [0:v]trim=start=120,setpts=PTS-STARTPTS[v4]; [0:a]atrim=start=120,asetpts=PTS-STARTPTS[a4]; \
 [v3][v4]concat[outputv]; [a3][a4]concat=a=1:v=0[outputa]" -map [outputv] -map [outputa] trimmed_movie_with_audio.mp4

Here's a breakdown of the command:

  • trim=duration=20[v1] trims the first 20 seconds of the video.
  • trim=start=50:end=70,setpts=PTS-STARTPTS[v2] trims from 50 to 70 seconds.
  • concat[v3] combines these segments.
  • trim=start=120,setpts=PTS-STARTPTS[v4] trims from 120 seconds to the end of the video.
  • concat[output] combines all segments into the final output
  • atrim is used for audio trimming and concat=a=1:v=0 to combine audio streams.

Here's what you need to consider:

  • setpts and asetpts to adjust timestamps so as to ensure correct playback
  • Ensure both video and audio streams are trimmed and concatenated together

How to cut video using FFmpeg without re-encoding?

You can use FFmpeg with input seeking to quickly trim a portion of your video without re-encoding. Here's how:

ffmpeg -ss 00:00:05 -i vacationClip.mp4 -to 00:00:12 -c:v copy -c:a copy trimmed_fast.mp4

When you place the -ss option before the -i option, FFmpeg leverages input seeking—meaning it will jump directly from one keyframe (I-frame) to the next to reach the desired start time. This is a fast and efficient technique, but it is not exactly frame-perfect. Alternatively, you can use the -ss option after the -i option to perform output seeking:

ffmpeg -i vacationClip.mp4 -ss 00:00:05 -to 00:00:12 -c:v copy -c:a copy precise_trim.mp4

In this case, FFmpeg will begin processing from the start of the video and decode it until it reaches the specified start time of 5 seconds. 

Conclusion

FFmpeg offers unmatched control, efficiency, and flexibility—handling even large and complex tasks effortlessly. Whether you need to convert files between different formats for compatibility or perform detailed edits, FFmpeg has you covered. It ensures top-notch quality without the limitations you’d encounter with online tools.

FAQs

How does re-encoding help with preserving video quality?

Video encoding typically involves both lossy and lossless compression. When you re-encode a video, it gets compressed to make the file smaller. This is done by getting rid of extra data and simplifying some parts of the video. Even though the file size is reduced, the video still looks pretty close to the original, so the quality is mostly preserved depending on the settings and compression techniques used during the process. 

How to cut video in FFmpeg by keeping time in milliseconds?

To cut a video using FFmpeg, where you specify the time in milliseconds, you can use the -ss and -t options with millisecond precision. Here's how:

ffmpeg -i input.mp4 -ss 00:00:05.500 -to 00:00:10.250 -c copy output.mp4

This command will cut the video from 5.5 seconds to 10.25 seconds and create a new file called output.mp4.

How to cut/trim video frame by frame in FFmpeg?

To trim a video frame by frame using FFmpeg, you need to calculate the timestamp for the frame where you want to start. Let’s say your video is 25 frames per second (fps), and you want to start trimming at frame 200. You would divide the frame number by the fps: 200 / 25 = 8 seconds

Then, you need to specify the number of frames you want to include using the -frames:v option. Here’s an example command:

ffmpeg -ss 8 -i input.mp4 -c:v libx265 -c:a aac -frames:v 100 out.mp4

How to trim audio files in FFmpeg?

To trim an audio file using FFmpeg, you need to specify a starting point (-ss) and either of these two: a duration (-t) or an ending point (-to). For example,

ffmpeg -i podcast-episode.wav -ss 15 -t 20 output.mp3

You can also use timestamps in the format HOURS:MINS:SECS.MILLISECONDS:

ffmpeg -i podcast-episode.wav -ss 00:00:15.25 -to 00:00:25.5 output.mp3
Reduce bounce rate by 74%

Get the fastest loading images and videos possible for your websites with Gumlet.