如何在PHP中获取视频的时长、尺寸和大小?

56

我想知道如何在PHP中获取上传视频文件的持续时间、尺寸和大小。该文件可以是任何视频格式。

4个回答

84

getID3支持视频格式。请参见:http://getid3.sourceforge.net/

编辑:因此,在代码格式中,它看起来像:

include_once('pathto/getid3.php');
$getID3 = new getID3;
$file = $getID3->analyze($filename);
echo("Duration: ".$file['playtime_string'].
" / Dimensions: ".$file['video']['resolution_x']." wide by ".$file['video']['resolution_y']." tall".
" / Filesize: ".$file['filesize']." bytes<br />");

注意: 在使用此功能前,您必须先包含getID3类!请参见上面的链接。

编辑:如果您有能力修改服务器上的PHP安装,则可以使用名为ffmpeg-php的PHP扩展来实现此目的。请参见:http://ffmpeg-php.sourceforge.net/


1
使用不同的库或者像 ffmpeg-php (http://ffmpeg-php.sourceforge.net/) 这样的 PHP 扩展。 - aendra
getID3和FFmpeg有什么问题吗?为什么它们不能制作出合适的用户界面,使阅读变得非常用户友好...只看getID3主页就已经厌烦了。 - gkd
@GhanshyamDobariya 不同的工具用于不同的用途。如果getID3有一个UI,它对我来说将会显著地不那么有用。 - aendra
1
我用一个分辨率为480的FLV文件进行了测试,$file['video']['resolution_x']给出了int(16)。我能够通过$width=$file['flv']['meta']['onMetaData']['width'];获取到正确的视频宽度。 - kmoney12
5
如果您正在使用Composer:{"require":{"james-heinrich/getid3": ">=1.9.9"}},那么请执行php composer.phar update来更新。 - John Erck
显示剩余4条评论

30
如果您在服务器上安装了FFMPEG (http://www.mysql-apache-php.com/ffmpeg-install.htm),可以使用命令 "-vstats" 获取视频属性,并且使用一些正则表达式解析结果,如下例所示。然后,您需要使用PHP函数filesize()来获取大小。
$ffmpeg_path = 'ffmpeg'; //or: /usr/bin/ffmpeg , or /usr/local/bin/ffmpeg - depends on your installation (type which ffmpeg into a console to find the install path)
$vid = 'PATH/TO/VIDEO'; //Replace here!

 if (file_exists($vid)) {

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $vid); // check mime type
    finfo_close($finfo);

    if (preg_match('/video\/*/', $mime_type)) {

        $video_attributes = _get_video_attributes($vid, $ffmpeg_path);

        print_r('Codec: ' . $video_attributes['codec'] . '<br/>');

        print_r('Dimension: ' . $video_attributes['width'] . ' x ' . $video_attributes['height'] . ' <br/>');

        print_r('Duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
                . $video_attributes['secs'] . '.' . $video_attributes['ms'] . '<br/>');

        print_r('Size:  ' . _human_filesize(filesize($vid)));

    } else {
        print_r('File is not a video.');
    }
} else {
    print_r('File does not exist.');
}

function _get_video_attributes($video, $ffmpeg) {

    $command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';
    $output = shell_exec($command);

    $regex_sizes = "/Video: ([^,]*), ([^,]*), ([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];
}

2
非常感谢!我发现提供的正则表达式不够用,因为我的测试视频在编解码器部分有一个额外的逗号。话虽如此,对于有同样问题的人,这里有个修复方法:$regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; 另外,我的 ffmpeg 路径是:/usr/local/bin/ffmpeg (在控制台中输入 which ffmpeg 查找安装路径)。这是 Regexr 的一个示例:http://regexr.com/3fs8d - 1owk3y
最好的一个。非常感谢。 - Fokrule

12

如果您使用WordPress,则可以使用内置的WordPress函数和提供的视频ID wp_get_attachment_metadata($videoID)

wp_get_attachment_metadata($videoID);

它帮助了我很多,这就是为什么我要发布它,尽管它只适用于WordPress用户。


4

https://github.com/JamesHeinrich/getID3 下载 getid3 压缩文件,然后将仅包含 getid3 名称的文件夹复制粘贴到项目文件夹中,并按以下方式使用它...

<?php
        require_once('/fire/scripts/lib/getid3/getid3/getid3.php');
        $getID3 = new getID3();
        $filename="/fire/My Documents/video/ferrari1.mpg";
        $fileinfo = $getID3->analyze($filename);

        $width=$fileinfo['video']['resolution_x'];
        $height=$fileinfo['video']['resolution_y'];

        echo $fileinfo['video']['resolution_x']. 'x'. $fileinfo['video']['resolution_y'];
        echo '<pre>';print_r($fileinfo);echo '</pre>';
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接