The “ffmpeg” is free, open-source, and the most frequently used tool for editing streams and multimedia through the command line interface in Linux. This tool contains various features, such as cutting and cropping videos. It also supports encoding or decoding multimedia formats, including unusual files.
This post will demonstrate cutting and cropping a video with the ffmpeg tool. The content for the post is as follows:
Install ffmpeg on Linux
As mentioned earlier, the “ffmpeg” is the utility used for editing videos that can be installed using the below-given command.
For Debian/Ubuntu:
$ sudo apt install ffmpeg
The “ffmpeg” will be installed.
For CentOS/RHEL:
$ yum localinstall
$ yum install ffmpeg ffmpeg-devel
Cut a Video Using ffmpeg
To cut the video using the “ffmpeg” utility user can define the starting and the ending point of the video. Let’s cut a video using the “ffmpeg” in the below examples.
Example 1: Cutting Video From Starting Point
To cut the video, use the “ss” flag and give the specific time you want to cut. Use the “i” flag to specify the video to be edited and the “t” flag for the ending point of the video. In the end, the “c” option generates the copy of the edited video:
$ ffmpeg -ss 00:00:05 -i Source.mkv -t 00:02:00 -c copy editedvideo.mp4
The video will be cut from “00:00:05” to “00:02:00”.
Let’s check the video cut with “ffmpeg” in the directory:
The video cut with “ffmpeg” is available.
Example 2: Cutting Video From Ending Point
To cut the last portion of the video, remove the “ss” flag that we used in the previous example. Just give the ending time duration, such as for the last 1 min (00:01:00):
$ ffmpeg -i Source.mkv -t 00:01:00 -c copy editedvideo2.mkv
The new video will include only the last minute of the original video.
You can also check the duration of the video by opening its properties:
The video duration is 1 minute.
Crop a Video Using ffmpeg
Users can also use the “ffmpeg” for cropping a video. Check the below-implemented examples.
Example 1: Displaying the Correct Dimensions and Positions For Cropping the Video
To automatically detect the exact size of the cropping of the video, you can use the below command:
$ ffmpeg -i Video.webm -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1
This command will suggest the user for the exact size that the user can crop. As in our case, it has suggested “640:352:0:4”.
Users can also use the crop size according to their own choice. For example, for cropping the video 200×200 image, use the below command:
$ ffmpeg -i Video.webm -filter:v "crop=200:200:200:0" output.webm
The video will be cropped with the name of the “output.webm”.
Let’s check the dimensions of the cropped video by opening its properties:
The cropped video has dimensions of 200×200.
Conclusion
To cut the video with the “ffmpeg” tool, use the “ss” flag for the starting point and the “t” flag for specifying the ending point. For cropping the video, use the “crop” option with the dimensions. Install the “ffmpeg” utility using the commands given in the guide and follow the method for cutting and cropping the video. This write-up has illustrated the method for cutting and cropping the video with the “ffmpeg” tool.