Detecting a videos dimensions using PHP and FFMPEG
Posted on March 16th, 2009 in Linux, PHP | 2 Comments »
Here is a method, that I pulled out of one of my video conversion classes.
All you need to do is set the location of ffmpeg ($this->ffmpeg)
It will then parse and return the video dimensions. More video methods to follow
/**
* Get the dimensions of a video file
*
* @param unknown_type $video
* @return array(width,height)
* @author Jamie Scott
*/
function __get_video_dimensions($video = false) {
if (file_exists ( $video )) {
$command = $this->ffmpeg . ' -i ' . $video . ' -vstats 2>&1';
$output = shell_exec ( $command );
$result = ereg ( '[0-9]?[0-9][0-9][0-9]x[0-9][0-9][0-9][0-9]?', $output, $regs );
if (isset ( $regs [0] )) {
$vals = (explode ( 'x', $regs [0] ));
$width = $vals [0] ? $vals [0] : null;
$height = $vals [1] ? $vals [1] : null;
return array ('width' => $width, 'height' => $height );
} else {
return false;
}
} else {
return false;
}
}
2 Responses
Hi dude, thx for the function, I really needed it, it helped me. By the way, have you ever used ffmpeg php extension, maybe there will be easier to gain dimensions?
I’m still not exactly how you dig the dimensions out of stdout using regex, but it works flawlessly.
Thanks a lot for this. It saved me a bunch of time.