R3D34THR4Y
R3D34THR4Y

Reputation: 101

FFMpeg: scale down videos with maximum width and height while maintaining aspect ratio

In my program the user can input any video file he wants and have it transcoded ready for social media no matter its dimensions and aspect ratio. The "export profiles" have many variables but the important ones here are maxheight and maxwidth.

The FFMpeg filters must output a video that follows the following rules:

I have tried finding a combination of filters that do that but I haven't been able so far, either the video gets distorted or it gets huge black bars if the aspect ratio isn't right, the solution may be simple but I'm a beginner on this library so I'm probably just missing an easy solution.

My current solution:

ffmpeg -i input.mp4 -vf [in] scale=1280:720:flags=lanczos:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1 [res]; [res] format=yuv420p [format] -c:v libx264 -c:a aac -movflags +faststart output.mp4

Thanks for reading

Upvotes: 5

Views: 4729

Answers (1)

R3D34THR4Y
R3D34THR4Y

Reputation: 101

I have found a pretty good compromise on another question which only scales down videos and scales them down to under a certain maximum dimension (which in my case is max(maxHeight, maxWidth)) and doesn't add padding, cropping or distortion: Here is the filter:

-vf 'scale=if(gte(iw\,ih)\,min(1280\,iw)\,-2):if(lt(iw\,ih)\,min(1280\,ih)\,-2)'

Here is the answer mentionned:

Resize videos with ffmpeg - Keep aspect ratio

It's not perfect but good enough

Upvotes: 3

Related Questions