Image/Video Cropping - 2020
In this chapter, we'll crop the input video to given dimensions.
The command syntax looks like this:
ffmpeg -i before.mp4 -vf "crop=w:h:x:y" after.mp4
The crop filter accepts the following options:
- w
Width of the output video (out_w). It defaults to iw. This expression is evaluated only once during the filter configuration. - h
Height of the output video (out_h). It defaults to ih. This expression is evaluated only once during the filter configuration. - x
Horizontal position, in the input video, of the left edge of the output video. It defaults to (in_w-out_w)/2. This expression is evaluated per-frame. - y
Vertical position, in the input video, of the top edge of the output video. It defaults to (in_h-out_h)/2. This expression is evaluated per-frame.
The original mp4's resolution is 1280x534 and I cropped the width to 712. That makes the aspect ratio 4:3.
ffmpeg -i blue_umbrella.mp4 -vf "crop=712:534" cropped_blue_umbrella.mp4
This will give us the same result as in the previous section.
To achieve the same effect, we need to crop our video (1280-712)/2 = 284px on left/right and 0px on top/bottom use this configuration crop=iw-568:ih:284:0. The parameter iw-568 is the cropped output width (out_w) calculated by the input width - 568px as we wanted to crop 284px from each side. The same could be done for the output height (out_h), but in this case, we want to keep it untouched. The starting coordinates are x = 284 and y = 0.
We do the cropping with an image instead of a video;
ffmpeg -i blue_umbrella_025.png -vf "crop=712:ih:284:0" cropped_blue_umbrella_025.png
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization