ffmpeg-php安装与使用

(1)简单调用demo,在单独的文件使用,不包含类,不指定名字空间

//按指定帧生成缩略图

if(extension_loaded('ffmpeg')){//判断ffmpeg是否载入    

$mov = new ffmpeg_movie('D:\robot.avi');//视频的路径    

$ff_frame = $mov->getFrame(2);    

$gd_image = $ff_frame->toGDImage();    

$img=$_SERVER['DOCUMENT_ROOT']."/test.jpg";//要生成图片的绝对路径    

imagejpeg($gd_image, $img);//创建jpg图像    

imagedestroy($gd_image);//销毁一图像

}else{    

  echo "ffmpeg没有载入";

}

?>
注意:将ffmpeg_movie('D:\robot.avi');中的路径替换成你视频的路径,你发现在你PHP文件的同目录下生成了一个test.jpg,说明创建成功。


(2) 在名字空间和其它类中使用

If you want to access another namespace, which includes the global namespace (which includes PHP classes, interfaces, as well as any custom defined global items), you have two choices.

  1. Access it using it's full namespace (which is \ for global) & class name:

    $obj = new \ffmpeg_movie;
    $obj = new \DateTime;
    
  2. Reference the external class using use:

    use ffmpeg_movie;
    use DateTime as AwesomeDateTimeClass;
    
    $obj = new ffmpeg_movie;
    $obj = new AwesomeDateTimeClass;
    

(3) 缩放图片尺寸

       if(extension_loaded('ffmpeg')){
            //var_dump(realpath($sourceFileUrl)); exit();
            //如果目标目录不存在,将创建目录
            $this->createFolder(dirname($targetFileUrl));
            
            $mov = new \ffmpeg_movie(realpath($sourceFileUrl));
            $ff_frame = $mov->getFrame($frameNum);        
            $gd_image = $ff_frame->toGDImage();        
            //$targetFilePath=$_SERVER['DOCUMENT_ROOT']."/test.jpg";    //要生成图片的绝对路径
            //$targetFilePath = dirname($targetFileUrl) . "/test.jpg";
            imagejpeg($gd_image, $targetFileUrl, 80); //创建jpg图像
            
            if ((!empty($newWidth)) && (!empty($newHeight))){
                header('Content-type: image/jpeg');
                list($width, $height) = getimagesize($targetFileUrl);                
                $source = imagecreatefromjpeg($targetFileUrl);
                $newImg = ImageCreateTrueColor($newWidth, $newHeight);
                
                imagecopyresized($newImg, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
                imagejpeg($newImg);
            }        
            imagedestroy($gd_image);    //销毁图像        
        }



1.下载ffmpeg

http://download.csdn.net/detail/xmlife/8608487

2. 解压ffmpeg

3. 将除了php_ffmpeg.dll文件,其它拷贝到windows\system32文件夹

4. extension=php_gd2.dll
    extension=php_gettext.dll
    extension=php_ffmpeg.dll

 

5. 重新启动wamp后使用phpinfo()函数看到一下信息配置:

ffmpeg support (ffmpeg-php) enabled
ffmpeg-php version 0.5.2.1
libavcodec version Lavc51.43.0
libavformat version Lavf51.12.2
ffmpeg-php gd support enabled

 

Directive Local Value Master Value
ffmpeg.allow_persistent 0 0


 以上就表明ffmpeg在php环境中配置成功了。

 

6. 下面我们建立一个php的页面来测试是不是可以使用ffmpeg的一些函数功能。建立testvideo.php文件

 代码如下:

extension_loaded('ffmpeg');

$ffmpegInstance = new ffmpeg_movie('C:\wamp\www\top10.mp4');
echo "getDuration: " . $ffmpegInstance->getDuration()."
" .
"getFrameCount: " . $ffmpegInstance->getFrameCount()."
" .
"getFrameRate: " . $ffmpegInstance->getFrameRate()."
" .
"getFilename: " . $ffmpegInstance->getFilename()."
" .
"getComment: " . $ffmpegInstance->getComment()."
" .
"getTitle: " . $ffmpegInstance->getTitle()."
" .
"getAuthor: " . $ffmpegInstance->getAuthor()."
" .
"getCopyright: " . $ffmpegInstance->getCopyright()."
" .
"getArtist: " . $ffmpegInstance->getArtist()."
" .
"getGenre: " . $ffmpegInstance->getGenre()."
" .
"getTrackNumber: " . $ffmpegInstance->getTrackNumber()."
" .
"getYear: " . $ffmpegInstance->getYear()."
" .
"getFrameHeight: " . $ffmpegInstance->getFrameHeight()."
" .
"getFrameWidth: " . $ffmpegInstance->getFrameWidth()."
" .
"getPixelFormat: " . $ffmpegInstance->getPixelFormat()."
" .
"getBitRate: " . $ffmpegInstance->getBitRate()."
" .
"getVideoBitRate: " . $ffmpegInstance->getVideoBitRate()."
" .
"getAudioBitRate: " . $ffmpegInstance->getAudioBitRate()."
" .
"getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate()."
" .
"getVideoCodec: " . $ffmpegInstance->getVideoCodec()."
" .
"getAudioCodec: " . $ffmpegInstance->getAudioCodec()."
" .
"getAudioChannels: " . $ffmpegInstance->getAudioChannels()."
" .
"hasAudio: " . $ffmpegInstance->hasAudio();

 7.执行后如果拿到视频的一些信息如下就表示环境配置成功了,那我们就可以开始开发我们的视频转换了。



你可能感兴趣的:(PHP)