简体   繁体   中英

Error when cropping video using FFMPEG

Question : Is there a single FFMPEG crop filter that will work for all videos, including (but not limited to): 856x480, 640x480, and 1280x720?

We have a video processing system (uses DirectShow), and all videos fed into this system must be 16:9 using the MJPEG codec. We use the following ffmpeg command to convert the source videos into MJPEG, scale the pixels to make them square, and then crop them to a 16:9 aspect ratio. This works great for most input videos, and the output is exactly what we want.

ffmpeg -i "1280x720input.mp4" -filter:v "scale=iw*sar:ih,crop=iw:iw/16*9" -codec:v mjpeg -q:v 2 -codec:a pcm_s16le -r 30 -y "output.avi"

However, when we use an input video with a resolution of 856x480, we get the following error:

[Parsed_crop_1 @ 0000000004615720] Invalid too big or non positive size for width '852' or height '480'

I tried a different crop filter that uses the input height in the calculation instead of input width, and it works with 856x480

ffmpeg -i "856x480input.mp4" -filter:v "scale=iw*sar:ih,crop=ih*16/9:ih" -codec:v mjpeg -q:v 2 -codec:a pcm_s16le -r 30 -y "output.avi"

However this does not work with other source videos in 16:9 (1280x720) or 4:3 (640x480) format. Is there a single crop command that will work on all videos?

You need conditional expressions:

crop='if(gte(dar,16/9),ih*16/9,iw)':'if(gte(dar,16/9),ih,iw*9/16)'

dar is the display aspect ratio ie iw * sar / ih

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM