一個FFMPEG視頻轉換的PHP類

來源:互聯網
上載者:User
opti        $this->options = array_filter($options) + $this->options;        $this->ffmpeg=$ffmpeg;//ffmpeg路徑        $this->backup=$backup;    }    //擷取視頻資訊    function video_info($file)    {        ob_start();        passthru(sprintf($this->ffmpeg . ' -i "%s" 2>&1', $file));//ffmpeg -i test.avi 2>&1        $info = ob_get_contents();        ob_end_clean();        // 通過使用輸出緩衝,擷取到ffmpeg所有輸出的內容。        $ret  = array();        // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/",$info, $match))        {            $ret['duration'] = $match[1]; // 提取出播放時間            $da              = explode(':', $match[1]);            $ret['seconds']  = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 轉換為秒            $ret['start']    = $match[2]; // 開始時間            $ret['bitrate']  = $match[3]; // bitrate 碼率 單位 kb        }        // Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc        if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match))        {            $ret['vcodec']     = $match[1]; // 編碼格式            $ret['vformat']    = $match[2]; // 視頻格式            $ret['resolution'] = $match[3]; // 解析度            $a                 = explode('x', $match[3]);            $ret['width']      = $a[0];            $ret['height']     = $a[1];        }        // Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s        if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match))        {            $ret['acodec']      = $match[1];       // 音頻編碼            $ret['asamplerate'] = $match[2];  // 音頻採樣頻率        }        if (isset($ret['seconds']) && isset($ret['start']))        {            $ret['play_time'] = $ret['seconds'] + $ret['start']; // 實際播放時間        }        $ret['size'] = filesize($file); // 檔案大小        return $ret;    }    function convert()    {        $verifyToken = md5('unique_salt' . $this->options['timestamp']);        if ($verifyToken == $this->options['token'])        {            $orgFile  = $this->options['org_path'] . $this->options['org'];            $setting=' ';            if(isset($this->options['video_size']))                $setting=$setting.'-vf scale="'.$this->options['video_size'].'"';            $mp4      = $this->ffmpeg . ' -i  '  . $orgFile . ' -ss 00:01:02 -vcodec libx264 -strict -2 '.$setting.' ' . $this->options['mp4_path_temp'] . $this->options['org'] . '';//轉換視頻            exec($mp4);            if(isset($this->options['watermark'])){            $watermark=' "'.$this->options['watermark'].'" ';            $mp4 = $this->ffmpeg.' -i '.$this->options['mp4_path_temp'] . $this->options['org'] . ''.' -vf '.$watermark.' '.$this->options['mp4_path'] . $this->options['org'] . '';//增加浮水印            exec($mp4);            @unlink($this->options['mp4_path_temp'] . $this->options['org'] . '');            }            $duration = $this->video_info($orgFile);            $seconds  = intval($duration['seconds']);            $offset   = intval($seconds / 21);//間隔 秒            $thumbs = explode(',', $this->options['thumb_size']);            for ($i = 0; $i <= count($thumbs); $i++)            {                $targetPath = $this->options['thumb_path'] . $this->options['org'] . '/';                if (!file_exists($targetPath))                    @mkdir(rtrim($targetPath, '/'), 0777);                if ($i == 0)                {                    $time     = 1;                    $name     = $i == 0 ? 'default.jpg' : $i . '.jpg';                    $img_size = $this->options['main_size'];                }                else                {                $time       = $i * $offset;                $name       = $i . '.jpg';                $img_size   = $thumbs[$i-1];                }                $jpg = $this->ffmpeg . ' -i  ' . $orgFile . ' -f  image2  -ss ' . $time . ' -vframes 1  -s ' . $img_size . ' ' . $targetPath . $name;//                @exec($jpg);            }            //複製檔案到對應的FTP伺服器            $ftp_server    = pc_base::load_config('ftp_server');            $remote_server = $_POST['remote_server'];            $ftp_server    = $ftp_server[$remote_server];            if($ftp_server['ftp_server'])                pc_base::ftp_upload($orgFile,                                $ftp_server['ftp_server'],                                $ftp_server['ftp_user_name'],                                $ftp_server['ftp_user_pass']);            //備份到所有FTP伺服器            if($this->backup){                $ftp_backup = pc_base::load_config('ftp_backup');                foreach ($ftp_backup as $v)                {                    pc_base::ftp_upload( $orgFile,                                        $ftp_backup['ftp_server'],                                        $ftp_backup['ftp_user_name'],                                        $ftp_backup['ftp_user_pass']);                }            }            $result['url']=$ftp_server['ftp_server']['http_address']. $this->options['mp4_path'] . $this->options['uniqid'] . '.mp4';//記錄視頻播放地址            $result['uniqid']=$this->options['uniqid'];            $result['videoTime'] = $this->video_info($orgFile);            $result['videoTime'] = $result['videoTime']['seconds'];            return $result;//返回處理結果        }        else            die('驗證失敗!');    }}?>

參考

http://www.fieryrain.com/blog/FFMPEG_VIDEO_TIME

http://keren.iteye.com/blog/1773536

http://www.cnblogs.com/dwdxdy/p/3240167.html

http://www.cnblogs.com/chen1987lei/archive/2010/12/03/1895242.html

以上就介紹了 一個FFMPEG視頻轉換的PHP類,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.