How to Cut Video (Extract/Trim) with FFmpeg in 3 Easy Ways
October 12, 2020 Krishna Rao Vijayanagar FFmpeg
In this tutorial, we will see how to cut/trim/extract part of a video file in 3 different ways using FFmpeg. There are quick ways to do this using a less precise find and copy video, and there is a frame-accurate technique that, while slow, has the option to re-encode the video.
Look for using the -ss parameter
Suppose you want to extract a portion of a video - say from the 10th second to the 20th second.
The first thing you need to do is tell FFmpeg to search until the 10th second, right? This is achieved using the -ss
parameter in the FFmpeg command line, 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 look for 01:02:03 - the 3rd second of the 2nd minute of the 1 hour movie!
Specify end time
Using -ss
, we specify the start time. Now, let's learn to specify an end time. And, if we put the two together, we can efficiently cut/stitch the video using FFmpeg.
-t
range You can specify the duration of the clip you want with the parameter -t
. For example, -ss 40 -t 10
instructs FFmpeg to extract 10 seconds of video starting at the 40th second.
-to
range You can specify the end time with the parameter -to. For example, -ss 40 -to 70
instructs FFmpeg to extract 30 seconds of video from the 40th to the 70th second.
Note : If you use both -t and -to, then only -t
will be used.
Cut/Trim with Recode
If you re-encode your video while cutting/trimming, then you'll get a frame-accurate cut because FFmpeg will re-encode the video and start at the I-frame. This is the command line to search with the output. In this example, you would instruct FFmpeg to read a video named inputVideo.mp4 and extract 5 seconds of video, starting at 3 and ending at 8 - using both 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 command line to re-encode at a specific bitrate or quality, use crf
to change the resolution, etc.
Keep in mind that this option will take a lot of time and resources because you are performing a re-encoding. However, it does have advantages that cannot be ignored.
I cut a 5 second section and encoded it with libx264. You can see it starts exactly at the requested time without any stuttering or black boxes. If you look closely, the timestamp will indicate this.
This is because FFmpeg re-encodes the video from the start, and can insert I-frames as needed to produce a frame-accurate clip of the video.
Quick cut/trim without recoding (search with copy and enter)
Here's a simple command line you can use to cut/trim/extract parts of a video - fast!
ffmpeg -ss 00:00:03 -i inputVideo.mp4 -to 00:00:08 -c:v copy -c:a copy trim_ipseek_copy.mp4
Parameters are easy to understand. You are instructing FFmpeg to read the named inputVideo.mp4 and extract 5 seconds of video, which starts at 3 seconds and ends at 8 seconds.
Related: Audio Transcoding with FFmpeg - Easily Change Audio Codecs with FFmpeg Also, you tell FFmpeg to copy audio and video without performing re-encoding - it's very fast!
Put the input search parameter -ss
before the parameter -i
, and it's very fast because FFmpeg jumps from I-frame to I-frame to reach the search point.
Any questions?
Since the seek operation jumps between I-frames, it doesn't stop at exactly the frame (or time) you requested. It will search for the nearest I-frame and start the copy operation from that point.
Cut/prune with output search without recoding If we insert the ---1ed0ef0be41cd5614f77a0fc2bd243db -i
parameter after the -ss
parameter, it is called an output search.
ffmpeg -i inputVideo.mp4 -ss 00:00:03 -to 00:00:08 -c:v copy -c:a copy trimmedVideo.mp4
However, here is another problem. In video compression, you have I-frames encoded independently, and you have predicted frames (P, B) that depend on other frames for decoding.
If the start time you specify falls on the predicted frame, the copy operation will start at that frame (called X). The 'X' frame that needs to be decoded may be missing from the output! Therefore, the output video may not start smoothly until the first I frame is reached, and there may be some stuttering or black video.
Here is the output.
You can see the timestamps start around the 5th second and continue until the 8th second. Also, similar to an input search, it cannot find I-frames to perform an accurate clip.
in conclusion
With it - three easy ways to cut, trim, extract parts of your video with FFmpeg. All three methods serve different needs, so be sure to try them out, understand your requirements, and use the right method for your project!
Please visit our other FFmpeg tutorials here.
Thank you and see you next time!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。