Problems? Solutions...
Problems
May 15th, 2009
Given an .avi, .mov, or .flv on disk, convert it to a specifically sized .flv accounting for thumbnails and common aspect ratio ( common having been determined to be 4:3 or 16:9 ).
<?php
function convertVideo( $v ) {
$old = $this->base.$v;
$new = preg_replace( "/\.mov|\.avi|\.flv/i", "_conv.flv", $v );
$newFullPath = $this->base.$new;
///////////////////////////
// FIND ASPECT RATIO USING A FFMPEG HACK + STRING MATCHING
$cmd = "ffmpeg -i $old 2>&1";
$w = `$cmd`;
$matches = array();
$aspect = 0;
preg_match( "/[0-9]{3}x[0-9]{3}/", $w, $matches );
if ( count( $matches ) ) {
$parts = split( "x", $matches[0] );
$aspect = round( $parts[0]/$parts[1], 1 );
}
else return false;
//////////////////////////
// SET COMMAND VARIABLE BASED ON ASPECT
if ( $aspect*10 != 18 ) {
$cmd = "ffmpeg -y -i $old -f flv -s 336x252 -b 900000 -ar 44100 \
-padcolor 000000 -padleft 56 -padright 56 $newFullPath";
}
else {
$cmd = "ffmpeg -y -i $old -f flv -s 448x252 -b 900000 -ar\
44100 $newFullPath";
}
///////////////////////////
// CONVERT && CHECK
$w = `$cmd`;
if ( file_exists( $newFullPath ) ) {
//////////////////////////
// CREATE A THUMBNAIL
$ps = $this->ps;
$img = preg_replace( "/\.mov|\.avi|\.flv/i", ".jpg", $v );
$thumb = $this->base."thumbnails".$ps.$img;
$tcmd = "ffmpeg -itsoffset -10 -i $newFullPath -vcodec mjpeg -vframes 1 -an -y\
-f rawvideo -s 448x252 $thumb";
`$tcmd`;
$this->temp = $img;
return $new;
}
return false;
}
?>
Other code in the class marks the video as converted, updates the thumbnail in the db, and slates the converted .flv to be put to a CDN such as S3.