Linux Commands

How to Cut and Crop a Video with ffmpeg

This tutorial explains how to cut and crop videos from the command line using ffmpeg.

FFmpeg is a multiplatform, open-source suite of tools and libraries to edit multimedia and streams from the command line. It supports encoding and decoding most multimedia formats, including uncommon files. After reading this tutorial which includes procedure and video screenshots, you will know how to cut and crop videos using ffmpeg.

Installing ffmpeg:

To install ffmpeg on Debian-based Linux distributions, use the command apt as shown in the example below.

sudo apt install ffmpeg

To install ffmpeg on Redhat / Centos, run the commands below:

yum localinstall
yum install ffmpeg ffmpeg-devel

How to cut videos using ffmpeg:

Cutting videos with ffmpeg is a pretty simple, fast, and low resource-consuming task using ffmpeg. You only need to define starting or end time, or both them if needed and the output file. I will be working with this Linux Hint video (duration 00:03:280) I just downloaded for this tutorial.

The command below uses ffmpeg to cut the video from the second 00:00:05 specified with the flag -ss; this is the flag to define a starting point for your new video in case you want to cut part of the beginning. If you only want to cut part of the end of the video, you don’t need to use this flag. As you can see, the timing format must be HH:MM:SS (Hours, Minutes, Seconds). For example, for 2 minutes and 3 seconds timing, you should type 00:02:03.

The -i flag used to specify the file to be edited; in this case, the file is LinuxHint-vim.mp4.

The option -t is used to specify the end of the new file; in this case, the video will end at 00:02:00. Similarly to -ss, if you don’t want to cut part of the end of the video, but only part of the beginning, you don’t need to apply this flag.

In this case, the -c copy flag is used to define the output file; in this case, the file editedvideo.mp4.

ffmpeg -ss 00:00:05 -i LinuxHint-vim.mp4 -t 00:02:00 -c copy editedvideo.mp4

The whole operation took a couple of seconds without consuming computer resources.

The next example shows how to cut only part of the end of the 00:03:28 seconds video. Thus I omit the option -ss because I want to keep the starting point, and I cut the video at 00:02:00 by implementing the -t flag.

ffmpeg -i LinuxHint-vim.mp4 -t 00:02:00 -c copy editedvideo2.mp4

Contrary to the previous example, the command below only cuts part of the video beginning by using the -ss flag. In this case, the new output will start from 00:01:30.

ffmpeg -ss 00:01:30 -i LinuxHint-vim.mp4  -c copy editedvideo3.mp4

As you can see, cutting videos with ffmpeg is a simple and fast process.

Cropping black borders using ffmpeg:

This section of the tutorial describes how to crop videos with ffmpeg.

You can use ffmpeg to try to automatically detect how to crop your video to remove black borders.

For this example, I will use a 320×240  video which looks like the following:

The command below will try to detect the correct dimensions and positions to crop the video correctly.

ffmpeg -i linux-foundation.mp4 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1

As you can see, ffmpeg returns the proper width (320), height (208), X, and Y positions to crop the video correctly.

The X position: The X position defines the horizontal cropping starting point from the left margin, where the left margin is 0.

The Y position: Y is the vertical cropping start point where the top margin is 0.

The example below the video will be cropped; you can see the -filter:v flag.

The -filter flag implements a filtergraph that divides the input stream, cropping it, and overlays it with the other streaming. As you can see, the dimensions and positions defined in the command below are provided by the previous command.

Also, you can see the flag “-c copy” was omitted, and the output file name was written just after the crop flag.

ffmpeg -i linuxfoundation.mp4 -filter:v "crop=320:208:0:16" output.mp4

As you can see, the black borders were removed:

About cropping videos using ffmpeg:

You can crop any part of a video using the previous technique, not only black borders.

The command below will crop the previous video, returning a 200×200 image, starting 200px from left and 0px from top margins.

fffmpeg -i output.mp4 -filter:v "crop=200:200:200:0" output2.mp4

And here is the cropped video:

Of course, you can define other types of measures, such as rectangles.

The video we will work on (same as the first tutorial section) looks like the image below.

In this first example, we only define the output dimensions but not the position. If you don’t specify the position, ffmpeg will automatically crop the center of the video. Thus, in the example below, in which only the video height and width are defined but no position, ffmpeg will crop the video and return a 500×500 cropped output of the center of the video.

ffmpeg -i LinuxHint-vim.mp4 -filter:v "crop=500:500" LinuxHintvideo2.mp4

And we get the cropped 500x500px output:

If needed, you can use the command ffmpeg combined with grep to learn the original video resolution, as shown below.

ffmpeg -i LinuxHint-vim.mp4 2>&1 | grep Video: | grep -Po '\d{3,5}x\d{3,5}'

If you like ffmpeg simplicity and performance, you can access ffmpeg official documentation here to learn about many additional features and functions available to edit media.

Conclusion:

Cutting and cropping media from the command line is pretty easy with the help of ffmpeg. One of ffmpeg’s main advantages is the low resource consumption and fast speed.
This tutorial shows any Linux user level or any person without knowledge of video editing can professionally edit videos in text mode by learning a few commands and a friendly syntax. FFmpeg is multiplatform, making it a great standard tool to edit video and audio files from the command line. FFmpeg is part of widely known tools like VLC player and was included in core processing for iTunes and Youtube.

I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.