Serverside video resizing script for ffmpeg or Mencoder

Some days ago, while working on a project that involves the re-encoding of a lot of videos coming from several sources, aspect ratios, resolutions, etc, i found myself in the situation where i needed to “standarize” somehow all the videos to a prefixed size in order to place them on a fixed space in a web page.
While most linux tools like ffmpeg or mencoder include native functions to scale or resize the video, they don’t care about the aspect ratios or about the fact that most video encoders expect mod16 values for the height and width values.
So my situation was like this:

I needed to make all the videos 480 pixels high, and scale the width proportionally.

That being said, i came up with this PHP script that i call from inside a bash script:

$cmdWidth = 'midentify '.$argv[1];
$finalHeight = $argv[2];
exec($cmdWidth, $output);

foreach ($output as $value) {
    if (strstr($value, "ID_VIDEO_WIDTH")) {
		$width = parseResult($value);
    if (strstr($value, "ID_VIDEO_HEIGHT")) {
    	$height = parseResult($value);
$frameSize = $width / $height;
$finalWidth = $finalHeight * $frameSize;
echo getMod16(round($finalWidth)) . "x" . getMod16(round($finalHeight)) . "\n";
function parseResult($line) {
	$v1 = explode("=",$line);
	$v20 = $v1[1];
	return $v20;
function getMod16($number){
	while (fmod($number, 16) != 0) {
		$number ++;
	return $number;

This script assumes that you have the “midentify” utility (wich comes with mplayer) installed in your path, and expects 2 arguments, being argument 1 the movie you want to resize and argument 2 the actual height you want to reach.
That being said, suppose you have a movie that is 848 pixels width and 480 pixels high and you want to re-scale it so it fits inside a 400 pixels high space, you would call the script like this:

./resizer mymovie.avi 400

The script will output this:


You can use later this value as an input parameter for ffmpeg for example.

Before somebody asks, the reason because i used PHP instead of BASH, is simply because bash does not supports floats.

6 Responses to “Serverside video resizing script for ffmpeg or Mencoder”

  1. […] (specially in the video scaling / resizing area). So in my particular case, I had to use several php / bash scripts in order to feed the encoders with the proper resizing data, etcetera. Well, let me introduce you […]

    by Diego’s Rants » Encoding high quality h.264 / aacPlus v2 files for web streaming with Flash 9, version 2., January 1st, 2008 at 10:48 pm

  2. hey man, do you still have taht script available?
    please email me if you do! thanks man

    by mike, November 25th, 2008 at 7:30 pm

  3. Hello dude

    where can we find this magic script ??

    by Jul, November 26th, 2008 at 7:25 am

  4. Hello
    Seems that this script is what i’m looking for…
    but is this script available somewhere?

    by Ward, February 20th, 2009 at 10:07 am

  5. The script is on this page, but is not being displayed due to a formatting error. View the source of this page, and copy everything between (but not including) the opening “pre” tag and the closing “/pre” tag, and paste it into a new text file called “resizer”. Then give the script execute permission by running “chmod a+x resizer”. This assumes you are not running Windows.

    by Ryan Schmidt, July 2nd, 2009 at 11:49 am

  6. Thanks Ryan for the heads up :)
    I have fixed the page to accomodate for the new syntax highlighter. Cheers!

    by Diego Massanti, July 29th, 2009 at 7:50 am