【问题标题】:How to get video duration, dimension and size in PHP?如何在 PHP 中获取视频时长、尺寸和大小?
【发布时间】:2011-06-18 09:05:43
【问题描述】:

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

【问题讨论】:

    标签: php video ffmpeg duration


    【解决方案1】:

    如果您使用 Wordpress,您可以使用带有视频 ID wp_get_attachment_metadata($videoID): 的 wordpress 内置函数

    wp_get_attachment_metadata($videoID);
    

    对我帮助很大。 这就是我发布它的原因,尽管它只适用于 wordpress 用户。

    【讨论】:

      【解决方案2】:

      https://github.com/JamesHeinrich/getID3 下载 getid3 zip,然后将 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>';
      ?>
      

      【讨论】:

        【解决方案3】:

        如果您的服务器上安装了 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];
        }
        

        【讨论】:

        • 非常感谢!我发现提供的正则表达式不足,因为我的测试视频在编解码器部分有一个额外的逗号。也就是说,这里有一个针对任何有同样问题的人的修复:$regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; 另外,我的 ffmpeg 路径是:/usr/local/bin/ffmpeg(在控制台中输入 which ffmpeg 以查找安装路径)。这是一个正则表达式示例:regexr.com/3fs8d
        • 最好的。非常感谢
        【解决方案4】:

        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 安装,则用于此目的的 PHP 扩展是 ffmpeg-php。见:http://ffmpeg-php.sourceforge.net/

        【讨论】:

        • 您好 aendrew,谢谢,但这适用于音频文件,但不适用于 mp4、wmv 等视频文件...请让我知道除此之外的任何其他解决方案。
        • 使用不同的库,或者像 ffmpeg-php (ffmpeg-php.sourceforge.net) 这样的 PHP 扩展
        • getID3 和 ffmpeg 有什么问题??为什么他们没有使 UI 变得正确,以便阅读变得非常用户友好......只看 getID3 的主页就淡化了......
        • 我使用具有 480 分辨率的 FLV 文件对此进行了测试,$file['video']['resolution_x'] 给了我int(16)。我能够使用$width=$file['flv']['meta']['onMetaData']['width']; 获得正确的视频宽度
        • 如果你使用作曲家:{"require":{"james-heinrich/getid3": "&gt;=1.9.9"}} 然后运行php composer.phar update
        猜你喜欢
        • 2019-03-11
        • 2013-11-11
        • 2016-04-26
        • 2017-02-18
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多