PHP后台自动获取视频信息并截图功能实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hj960511/article/details/83309866

名称:php自动获取视频信息并截图
描述:PHP后台自动获取视频信息并截图功能实例,包含ffmpeg的安装步骤。
版本:所有版本

步骤1:确保 shell_exec() 函数是否可用!,php.ini中修改下方禁用函数。

disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server

去掉:shell_exec,passthru

步骤2:安装ffmpeg

# wget http://www.ffmpeg.org/releases/ffmpeg-3.1.tar.gz
# tar -zxvf ffmpeg-3.1.tar.gz
# cd ffmpeg-3.1
# ./configure --prefix=/usr/local/ffmpeg
# make && make install
 
等待安装完成...
 
# vi /etc/profile
在最后PATH添加环境变量:
PATH=$PATH:/usr/local/ffmpeg/bin
export PATH
保存退出
 
# source /ect/profile   设置生效
# ffmpeg -version       查看版本

注:命令ffmpeg可以无效,但/usr/local/ffmpeg/bin/ffprobe必须要有效果。

步骤3:自定义获取缩略图函数

//获取base64的图片码并返回
function get_video_orientation($video_path) {
    $cmd =  "/usr/local/ffmpeg/bin/ffprobe ".ROOT_PATH . 'public'.$video_path ." -show_streams 2>/dev/null";
    $result = shell_exec($cmd);
    $orientation = 0;
    if(strpos($result, 'TAG:rotate') !== FALSE) {
        $result = explode("\n", $result);
        foreach($result as $line) {
            if(strpos($line, 'TAG:rotate') !== FALSE) {
                $stream_info = explode("=", $line);
                $orientation = $stream_info[1];
            }
        }
    }
    return $orientation;
}

/**
 * 使用ffmpeg获取视频信息
 * @param  String $file 视频文件
 * @return Array
 */
function getVideoInfo($file,$cmd='/usr/local/ffmpeg/bin/ffprobe -i "%s" 2>&1'){
    ob_start();
    passthru(sprintf($cmd, $file));
    $video_info = ob_get_contents();
    ob_end_clean();

    // 使用输出缓冲,获取ffmpeg所有输出内容
    $ret = array();

    // Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
    if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $video_info, $matches)){
        $ret['duration'] = $matches[1]; // 视频长度
        $duration = explode(':', $matches[1]);
        $ret['seconds'] = $duration[0]*3600 + $duration[1]*60 + $duration[2]; // 转为秒数
        $ret['start'] = $matches[2]; // 开始时间
        $ret['bitrate'] = $matches[3]; // bitrate 码率 单位kb
    }

    // Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
    if(preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $video_info, $matches)){
        $ret['vcodec'] = $matches[1];     // 编码格式
        $ret['vformat'] = $matches[2];    // 视频格式
        $ret['resolution'] = $matches[3]; // 分辨率
        list($width, $height) = explode('x', $matches[3]);
        $ret['width'] = $width;
        $ret['height'] = $height;
    }

    // Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
    if(preg_match("/Audio: (.*), (\d*) Hz/", $video_info, $matches)){
        $ret['acodec'] = $matches[1];      // 音频编码
        $ret['asamplerate'] = $matches[2]; // 音频采样频率
    }

    if(isset($ret['seconds']) && isset($ret['start'])){
        $ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
    }

    $ret['size'] = filesize($file); // 视频文件大小
    if(is_gb2312($video_info)) $video_info = iconv("gb2312","utf-8//IGNORE", $video_info);
    return array($ret, $video_info);

}



/**
 * 判断是否是gbk
 * @param $str
 * @return bool
 */
function is_gb2312($str)
{
     for($i=0; $i<strlen($str); $i++) {
     $v = ord( $str[$i] );
     if( $v > 127) {
         if( ($v >= 228) && ($v <= 233) )
             {
             if( ($i+2) >= (strlen($str) - 1)) return true; // not enough characters
                 $v1 = ord( $str[$i+1] );
                 $v2 = ord( $str[$i+2] );
                 if( ($v1 >= 128) && ($v1 <=191) && ($v2 >=128) && ($v2 <= 191) ) // utf编码
                                return false;
                 else
                 return true;
                 }
        }
    }
 return true;
}

注:将此文件数据获取并保存生成图片即可。

猜你喜欢

转载自blog.csdn.net/hj960511/article/details/83309866