1. 程式人生 > >PHP後臺自動獲取視訊資訊並截圖功能例項

PHP後臺自動獲取視訊資訊並截圖功能例項

名稱: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;
}

注:將此檔案資料獲取並儲存生成圖片即可。