How to Cut Video Using FFmpeg in 3 Easy Ways (Extract/Trim)

In this tutorial, we’ll see how you can cut/trim/extract part of a video file using FFmpeg in 3 different ways. There are fast ways to achieve this using less-accurate seeking and copying the video, and there is a frame-accurate technique that is slow but accurate with the option of re-encoding your video.

Seek Using -ss Parameter

Let’s suppose that you want to extract a portion of your video – say from the 10th to the 20th seconds.

The first thing that you need to do is tell FFmpeg to seek to the 10th second, right? This is achieved using the -ss parameter in the FFmpeg command line and the syntax is –

./ffmpeg -ss <time> -i <inputvideo> ....... 

Here, the time is specified as HH:MM:SS.MILLISECONDS. For example, you can tell FFmpeg to seek to 01:02:03 – i.e., the 3rd second of the 2nd minute of the 1 hour of the movie!

Specifying the End Time

Using -ss, we specified the start time. Now, let’s learn to specify the end time as well. And, if we put those two together, we can efficiently cut / splice a video using FFmpeg.

-t parameter

You can specify the duration of the required clip using the -t parameter. For example, -ss 40 -t 10 instructs FFmpeg to extract 10 seconds of video starting from the 40th second.

-to parameter

You can specify the end-time using the -to parameter. For example, -ss 40 -to 70 instructs FFmpeg to extract 30 seconds of the video starting from the 40th second to the 70th second.

Note: if you use both  -t and -to, then only -t will be used.

Cut/Trim With Re-encoding

If you re-encode your video when you cut/trim, then you get a frame-accurate cut because FFmpeg will re-encode the video and start with an I-frame. Here is the command line for this using output seeking. In this example, you are instructing FFmpeg to read a video named inputVideo.mp4 and extract 5 seconds starting at the 3rd second and ending at the 8th second – while re-encoding it using libx264.

ffmpeg -i inputVideo.mp4 -ss 00:03 -to 00:08 -c:v libx264 -crf 30 trim_opseek_encode.mp4

You can also use this commandline to re-encode at a particular bitrate, or quality using crf, change the resolution, etc.

Do remember that this option will take a lot of time and resources because you are performing a re-encode. But, it does have its advantages that cannot be overlooked.

Here is what the output looks like.

I cut a 5 second section and re-encoded it using libx264. You can see that it starts accurately at the requested time without any stutters or black frames. The time-stamp indicates this if look carefully.

This is because FFmpeg re-encodes the video from the start-time and can insert I-frames as necessary to produce a frame-accurate clip of the video.

Fast Way to Cut / Trim Without Re-encoding (using Copy and Input Seeking)

Here is a simple commandline that you can use to cut/trim/extract a portion of your video – fast!

ffmpeg -ss 00:00:03 -i inputVideo.mp4 -to 00:00:08 -c:v copy -c:a copy trim_ipseek_copy.mp4

The parameters are simple to understand. You are instructing FFmpeg to read a video named inputVideo.mp4 and extract 5 seconds starting at the 3rd second and ending at the 8th second.

Related:  Convert FLAC to MP3 with FFmpeg With Metadata

Additionally, you are telling FFmpeg to copy the audio and video and not perform re-encoding – this is very fast!

Putting the -ss parameter before the -i parameter is called input seeking and is very fast because FFmpeg jumps from I-frame to I-frame to reach the seek-point.

The problem?

Since the seeking operation jumps between I-frames, it is not going to accurately stop on the frame (or time) that you requested. It will search for the nearest I-frame and start the copy operation from that point.

Cut/Trim using Output Seeking Without Re-encoding

If we insert the -ss parameter after the -i parameter, it is called output seeking.

ffmpeg -i inputVideo.mp4 -ss 00:00:03 -to 00:00:08 -c:v copy -c:a copy trimmedVideo.mp4

But, here again is a problem. In video compression, you have I-frames that are indepently encoded and you have predicted frames (P, B) that depend on other frames for decoding.

If the start time that you specified falls on a Predicted Frame, the copy operation will start with that frame (call it X). It is possible that the frames that “X” requires in order to be decoded are missing in the output! Consequently, it is possible that the output video will not start smoothly and might have some stutter, or black video until the first I-frame is reached.

Here is the output.

You can see that the time-stamp starts around the 5th second and carries on till the 8th second. Again, similar to input-seeking, it cannot find an I-frame to perform accurate clips.

Conclusion

There you have it – three simple ways to cut, trim, extract a portion of your videos using FFmpeg. All three methods cater to different needs, so be sure to try them out, understand your requirements, and use the right one for your project!

Do visit the rest of our FFmpeg tutorials here.

Thank you and see you next time!

krishna rao vijayanagar
Krishna Rao Vijayanagar

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

22 thoughts on “How to Cut Video Using FFmpeg in 3 Easy Ways (Extract/Trim)”

  1. Pingback: Reverse a Video with FFmpeg is Ridiculously Easy - OTTVerse

  2. Pingback: Create Beautiful Vintage Videos Using FFmpeg in 3 Super-Easy Steps - OTTVerse

  3. Pingback: Create Vintage Videos Using FFmpeg in 4 Simple Steps - GistTree

  4. Pingback: Create Vintage Videos Using FFmpeg in 4 Simple Steps | صحافة حرة FREE PRESS

  5. Pingback: Extract (trim) part of a video with FFmpeg – ScribbleGhost

  6. This is fantastic … just what I needed.
    I searched a lot of website before I found this. Very nice. Thanks.

    ffmpeg -ss 00:00:03 -i inputVideo.mp4 -to 00:00:08 -c:v copy -c:a copy trim_ipseek_copy.mp4

  7. Cutting video is a common task that can be accomplished in a number of different ways. One popular method is to use the open-source program FFmpeg. This software can be used to extract or trim a section of video without re-encoding the entire file. This makes it very efficient, especially for large files. There are a few different ways to cut video using FFmpeg. The first is to specify the start and end times for the section you want to extract. For example, to extract the first 30 seconds of a video, you would use the following command: ffmpeg -i input.mp4 -t 30 output.mp4. The second way is to specify the start time and duration of the section you want to extract. For example, to extract a 30-second section starting at 1 minute and 30 seconds into the video, you would use the following command: ffmpeg -i input.mp4 -ss 1:30 -t 30 output.mp4. Finally, you can also trim a section from the beginning or end of a video without specifying an explicit start or end time. For example, to trim the first 10 seconds from a video, you would use the following command: ffmpeg -i input.mp4 -t -10 output.mp4. As you can see, there are a number of different ways to cut video using FFmpeg. Experiment with different methods to find the one that works best for your needs.

  8. This article on cutting videos using FFmpeg is really helpful! The three different methods explained – extracting a portion of a video, trimming a video to a specific duration, and trimming a video based on specific timestamps – are all useful techniques that can come in handy for various video editing needs. The step-by-step instructions and examples provided make it easy to follow along and apply these techniques. Overall, this is a great resource for anyone looking to learn how to cut videos using FFmpeg.

  9. Pingback: A Simple Introduction to FFmpeg | Ray Woodcock's Latest

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.