views:

1317

answers:

5

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 640x480 (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:

  1. Determine the aspect ratio of the original video.
  2. If not 4:3, pad top and bottom with black bars.
  3. Convert to 640x480 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 640x480).

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.

Anyone have any ideas?

Thanks.

+1  A: 

Hi Andrew,

This works for me:

$data = 'ffmpeg output';
$matches = array();

if (!preg_match('/Stream #(?:[0-9\.]+)(?:.*)\: Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*)/',$data,$matches)
   preg_match('/Could not find codec parameters \(Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*)\)/',$data,$matches)

This might not always work, but it works most of the times, which was good enough in my case :)

Evert
Thanks. I'll give that a try.
Andrew
This is working for every video I've tested so far. As soon as I have a robust solution, I'll post my code as an answer in case anyone who wanders onto this page needs it. Thanks.
Andrew
In case you're interested, I've finally put all the pieces together and posted my answer for reference. http://stackoverflow.com/questions/1106955/php-and-ffmpeg-performing-intelligent-video-conversion/1123332#1123332
Andrew
+1  A: 

I'm not familiar with PHP, but I wrote a utility to work with ffmpeg in C# several months ago. I used regular expressions to do this. There are some regular expressions which may help you from there:

// this is for version detection
"FFmpeg version (?<version>(\w|\d|\.|-)+)"
// this is for duration parsing
"Duration: (?<hours>\d{1,3}):(?<minutes>\d{2}):(?<seconds>\d{2})(.(?<fractions>\d{1,3}))?"

// these are connected:
// 0) this is base for getting stream info
"Stream #(?<number>\d+?\.\d+?)(\((?<language>\w+)\))?: (?<type>.+): (?<data>.+)"
// 1) if the type is audio:
"(?<codec>\w+), (?<frequency>[\d]+) (?<frequencyUnit>[MK]?Hz), (?<chanel>\w+), (?<format>\w+)(, (?<bitrate>\d+) (?<bitrateUnit>[\w/]+))?"
// 2) if the type is video:
"(?<codec>\w+), (?<format>\w+), (?<width>\d+)x(?<height>\d+), (?<bitrate>\d+(\.\d+)?) (?<bitrateUnit>[\w\(\)]+)"

So getting width and height you can calculate an aspect ratio.

Note: I know that in some cases there expressions may fail.

Alex
Thanks. I'll give that a try too, once I'm able to actually capture the output of ffmpeg. :-)
Andrew
Actually I can post my own code for this written in c# if you want, but guess it couldn't help.
Alex
Yeah. I need the results in PHP. I just got the output of ffmpeg figured out and Evert's solution works for me. Thanks for your help anyway. It might be useful to some Googler looking for how to do this in C# =p
Andrew
+1  A: 
Andrew
SO seems to have trouble rendering this answer for some reason. I've posted it on my website if anyone is interested.http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/
Andrew
A: 

Looks like your code got truncated on your last post. Think you could fix it? Thanks!

SO seems to have trouble rendering this answer for some reason. I've reposted it on my website here: http://andrewensley.com/2009/10/php-and-ffmpeg-performing-intelligent-video-conversion/
Andrew
A: 

Wow- just saw that post... oh, goody! Thanks for the update!!

Steven