91.11 Video Image Crop


This section deals with cropping the video image over the extent of the video. If you are wanting to extract time extents of the video (i.e., trim or cut the video) see Section 91.22.

Using ffmpeg we can remove, for example, the right most 230 pixel wide margin of a video with the following command.

$ ffmpeg -i input.mp4 -vf "crop=iw-230:ih:0:0" cropped.mp4

The input video is identified using -i. Video filtering is introduced with -vf as a shorthand for -filter:v. A filtergraph is provided as the argument to the video filtering. The final parameter names to output video file.

A crop filter is utilised through the filtergraph. The filter’s first parameter is the width of the output video. Here, iw refers to the original input video’s width from which we subtract the amount that we wish to crop. The second parameter is the height of the output video which using ih as the input video height retains the original height. The next two parameters are the x (horizontal) and y (vertical) positions of the input video that will become the left and top edge of the output video. Thus 0:0 indicates no cropping on the left or the top.

The filtergraph crop=iw-230:ih:0:0 will crop 230 pixels from the right edge of the video, for every frame.

To determine what to crop, extract a single frame from the video:

$ ffmpeg -ss 0.5 -i input.mkv -vframes 1 frame.jpg

Then load frame.jpg into gimp and measure the pixels on the left and top, and the right and bottom, to crop. Then modify the crop options. For example, to crop 40 pixels from the left, and 64 from the top, with the resulting video being 772 pixels wide and 431 high:

$ ffmpeg -i input.mkv -vf "crop=772:431:40:64" cropped.mkv

Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0