I have an oddly difficult task to perform. I thought it would be easy, but all my efforts have been fruitless.
I’m converting videos uploaded to a php script from various formats (.avi, .mpg, .wmv, .mov, etc.) to a single .flv format. The conversion is working great but what I’m having trouble with is the resolution of the videos.
This is the command I’m currently running (with PHP vars):
ffmpeg -i $original -ab 96k -b 700k -ar 44100 -s 640x480 -acodec mp3 $converted
Both $original and $converted contain the full paths to those files. My problem is that this always converts to 640×480 (like I’m telling it to) even when the source is smaller. Obviously, this is a waste of disk space and bandwidth when the video is downloaded. Also, this doesn’t account for input videos being in any aspect ratio other than 4:3, resulting in a “squished” conversion if I upload a 16:9 video.
There are 3 things I need to do:
- Determine the aspect ratio of the original video.
- If not 4:3, pad top and bottom with black bars.
- Convert to 640×480 if either dimension of the original is larger or a 4:3 aspect ratio relating to the width/height of the original (whichever is closer to 640×480).
I’ve run ffmpeg -i on a few videos, but I don’t see a consistent format or location to find the original’s resolution from. Once I’m able to figure that out, I know I can “do the math” to figure out the right size and specify padding to fix the aspect ratio with -padttop, -padbottom, etc.
This works for me:
This might not always work, but it works most of the times, which was good enough in my case 🙂