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:
- If the video is vertical, permutate the maxheight and maxwidth values (social media considers both 1280x720 and 720x1280 as "720P")
- The video must not have a height superior to maxheight or a width superior to maxwidth.
- The original video and exported video have the same aspect ratio and no distortion occurs.
- No padding or cropping should occur.
- The video should not be scaled if it is already under those maximum dimensions (no upscaling).
- The function must work even with odd input resolutions.
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