Trim/Cut/Extract Video Using FFmpeg in 3 Easy Ways

Tags:

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.

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!

About The Author

I’m Dr. Krishna Rao Vijayanagar, and I am the Founder and Editor of OTTVerse.com. I've spent several years working hands-on with Video Codecs (AVC, HEVC, MultiView Plus Depth), ABR streaming, and Video Analytics (QoE, Content & Audience, and Ad). I hope to use my experience and love for video streaming to bring you information and insights into the OTT universe. Please use the Contact Page to get in touch with me.

4 thoughts on “Trim/Cut/Extract Video Using FFmpeg in 3 Easy Ways”

  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

Leave a Reply