JamesHeinrich / getID3

http://www.getid3.org/
Other
1.15k stars 245 forks source link

Need help reading dimensions of an mp4 video using ffmpeg #349

Closed generalp2 closed 2 years ago

generalp2 commented 2 years ago

PROBLEM : I need dimensions of my mp4 video -- I am getting the size but not the width and height. Why?

HERE IS THE OUTPUT

Opening fileinfo database worked.
vid: Library/Oct_2011_Bubbie.mp4video/mp4; charset=binary
Mine: video/mp4
array(7) {
 ["codec"]=> NULL
 ["width"]=> NULL
 ["height"]=> NULL
 ["hours"]=> NULL
 ["mins"]=> NULL
 ["secs"]=> NULL
 ["ms"]=> NULL
} Codec:
Dimension: x
Duration: ::.
Size: 5.86M

HERE IS THE CODE:

<?php
// testffmpeg.php
// source: https://newbedev.com/how-to-get-video-duration-dimension-and-size-in-php 
$ffmpeg_path = '/usr/bin/ffmpeg'; 
$finfo = finfo_open(FILEINFO_MIME); // return mime type 
if (!$finfo) {
    echo "Opening fileinfo database failed";
    exit();
}
echo "Opening fileinfo database worked.<br />";
$vid = 'Library/Oct_2011_Bubbie.mp4'; //Replace here!
//$vid = 'UpDataFiles/94/IMG_2590.JPG';
echo "vid: ".$vid;
$filename = $vid;
echo finfo_file($finfo, $filename);
 if (file_exists($vid)) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $vid); // check mime type
     echo "<br /> Mine: ".$mime_type. '<br/>';
    finfo_close($finfo);
    if (preg_match('/video\/*/', $mime_type)) {
//$vid = analyze($filename);
        $video_attributes = _get_video_attributes($vid, $ffmpeg_path);
var_dump($video_attributes);
        echo ('Codec: ' . $video_attributes['codec'] . '<br/>');
        echo ('Dimension: ' . $video_attributes['width'] . ' x ' . $video_attributes['height'] . ' <br/>');
        echo ('Duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
                . $video_attributes['secs'] . '.' . $video_attributes['ms'] . '<br/>');
        echo ('Size:  ' . _human_filesize(filesize($vid)));
    } else {
        echo ('File is not a video.');
    }    
} else {
    echo ('File does not exist.');
}
//  strp2
function _get_video_attributes($video, $ffmpeg) {
    $command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';
    $output = shell_exec($command);
    $regex_sizes =  "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; // or : $regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; (code from @1owk3y)
    if (preg_match($regex_sizes, $output, $regs)) {
        $codec = $regs [1] ? $regs [1] : null;
        $width = $regs [3] ? $regs [3] : null;
        $height = $regs [4] ? $regs [4] : null;
    }
    $regex_duration = "/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/";
    if (preg_match($regex_duration, $output, $regs)) {
        $hours = $regs [1] ? $regs [1] : null;
        $mins = $regs [2] ? $regs [2] : null;
        $secs = $regs [3] ? $regs [3] : null;
        $ms = $regs [4] ? $regs [4] : null;
    }
    return array('codec' => $codec,
        'width' => $width,
        'height' => $height,
        'hours' => $hours,
        'mins' => $mins,
        'secs' => $secs,
        'ms' => $ms
    );
}
function _human_filesize($bytes, $decimals = 2) {
    $sz = 'BKMGTP';
    $factor = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
?>
JamesHeinrich commented 2 years ago

I'm not sure how this relates to getID3? Please reopen this issue if you are actually using getID3 and I missed it in your code example.