ffmpeg cropping: 'Invalid too big or non positive size for width'

I'm trying to generate images thumbnails using ffmpeg. The folder contains various jpeg files with different resolutions.

ffmpeg -y -i input1.jpg -filter_complex "scale=(iw*sar)*max(238/(iw*sar)\,358/ih):ih*max(238/(iw*sar)\,358/ih), crop=238:358" thumbnail.jpg

It works with most images in the folder but crop=238:358 will fail depending on certain images resolutions:

input1.jpg: 500x800 resize and crop successful

input2.jpg: 470x800 resize and crop fails:

Invalid too big or non positive size for width '238' or height '358'

The problem happens when scaled images get 237px instead of 238px due to aspect ratio scaling.

Since this is a script processing a bunch of files, I'm looking for a solution that can handle all images resolutions


Solution 1:

The scale filter will truncate the result of expressions to an integer, and it's possible for expression to yield e.g. 237.999 which gets truncated to 237. The workaround is to increase the coefficients slightly, so

ffmpeg -y -i input1.jpg -vf "scale=(iw*sar)*max(238.1/(iw*sar)\,358.1/ih):ih*max(238.1/(iw*sar)\,358.1/ih), crop=238:358" thumbnail.jpg