Cropping Videos With ffmpeg
Cropping in ffmpeg is down with the [TODO: Code shorthand span ] [TODO: Code shorthand span ] filter. For example :
ffmpeg input.mp4 -vf "crop=OUTWIDTH:OUTHEIGHT:STARTX:STARTY" output.mp4
Where :
-
*OUTWIDTH** is the output width
-
*OUTHEIGHT** is the output height
-
*STARTX** is the horizontal position to start cropping from the top left corner
-
*STARTY** is the vertical position to start cropping from the top left corner
No Crop
Starting with a video that's 3840x2160, this command uses the crop flag but won't change the dimensions since the OUTWIDTH and OUTHEIGHT are the same as the video and the STARTX and STARTY values are both zero.
ffmpeg -i input.mp4 -vf "crop=3840:2160:0:0" output.mp4
Cropping The Right
Removing the right side of the frame is done by *decreasing the value for OUTWIDTH**. That reduces the size of the output while keeping the starting point at zero so the right gets cropped off.
ffmpeg -i input.mp4 -vf "crop=2840:2160:0:0" output.mp4
Cropping The Left
Removing the left side of the frame is done by *increasing the value for STARTX and decreasing the value for OUTWIDTH**. The amount of the increase and decrease need to be the same in order to keep the crop flush against the side. For example, here's what it looks like if the OUTWIDTH is reduced by 1000 and the STARTX is increased by the same amount.
ffmpeg -i input.mp4 -vf "crop=2840:2160:1000:0" output.mp4
Cropping To The Center
Removing both sides is done by *increasing STARTX and decreasing OUTWIDTH by more** For example, this command increase the STARTX value to 1000. The original width of the video is 3840. If we subtract 1000 from that we'd get 2840. If we set the OUTWIDTH to that, it would reach all the way across. Reducing it further to 1840 pulls back 1000 pixels.
ffmpeg -i input.mp4 -vf "crop=1840:2160:1000:0" output.mp4
Enlarging Doesn't Work
Setting the crop values larger than the source video resolves in an error. For example, give a 3840x2160 video this will fail :
-vf "crop=4840:2160:0:0"
with an error message like :
[Parsed_crop_0 @ 0x135828c60] Invalid too big or non positive size for width '4840' or height '2160' [Parsed_crop_0 @ 0x135828c60] Failed to configure input pad on Parsed_crop_0 Error reinitializing filters! Failed to inject frame into filter network: Invalid argument Error while processing the decoded data for stream #0:0 Conversion failed!
- These examples all use with OUTWIDTH and STARTX for cropping along the horizontal axis. The same stuff all applies for the vertical dimension with OUTHEIGHT and STARTY.
- These example images have all be resized down to make them easier to see. The resize was the same on all of them so the ratios are all the same.
- The size can be adjusted in ffmpeg at the same time as the crop. I don't have a good write - up on that yet.
-
[]
write up ffmpeg resizing and cropping together