http://blog.163.com/chenzhenhua_007/blog/static/128492649201182335633965/
FFmpegPHP 是一个纯面向对象的
ffmpeg
的 PHP封装。提供一些简单易用、面向对象的API用以访问视频和音频文件的信息,可直接从视频中获取帧的图片,这常用来做视频的缩略图。支持的视频格式包 括: MOV, AVI, MPG, 和 WMV.
FFmpegPHP封装,没有提供视频转换的接口。(至少我没找到,如果大家知道还望赐教。)
我使用的集成环境APMServ。
1.下载ffmpeg-php: http://sergey89.ru/files/ffmpeg-php-win32-all.zip
2. 解压 ffmpeg-php-win32-all.zip 后有下面几个文件:
avcodec-51.dll
avformat-51.dll
avutil-49.dll
php_ffmpeg.dll
pthreadGC2.dll
3. 将四个文件拷贝到windows/system32文件夹下面
avcodec-51.dll, avformat-51.dll, avutil-49.dll, pthreadGC2.dll
4.将php_ffmpeg.dll拷贝到php的ext目录下。
5. 然后需要到apache/bin文件下找到php.ini文件下允许使用dll文件加入extension=php_ffmpeg.dll 并允许 extension=php_gd2.dll, extension=php_gettext.dll这两个设置。(去掉前面的分号)
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_ffmpeg.dll
6. 重新启动APMServ后使用phpinfo()函数看到一下信息配置:
以上就表明ffmpeg在php环境中配置成功了。
7. 下面我们建立一个php的页面来测试是不是可以使用ffmpeg的一些函数功能。建立testvideo.php文件
代码如下:
<?php
extension_loaded('ffmpeg');
$ffmpegInstance = new ffmpeg_movie('D:\Program Files\APMServ5.2.6\www\htdocs\test.mpg');
echo "getDuration: " . $ffmpegInstance->getDuration()."<br>" .
"getFrameCount: " . $ffmpegInstance->getFrameCount()."<br>" .
"getFrameRate: " . $ffmpegInstance->getFrameRate()."<br>" .
"getFilename: " . $ffmpegInstance->getFilename()."<br>" .
"getComment: " . $ffmpegInstance->getComment()."<br>" .
"getTitle: " . $ffmpegInstance->getTitle()."<br>" .
"getAuthor: " . $ffmpegInstance->getAuthor()."<br>" .
"getCopyright: " . $ffmpegInstance->getCopyright()."<br>" .
"getArtist: " . $ffmpegInstance->getArtist()."<br>" .
"getGenre: " . $ffmpegInstance->getGenre()."<br>" .
"getTrackNumber: " . $ffmpegInstance->getTrackNumber()."<br>" .
"getYear: " . $ffmpegInstance->getYear()."<br>" .
"getFrameHeight: " . $ffmpegInstance->getFrameHeight()."<br>" .
"getFrameWidth: " . $ffmpegInstance->getFrameWidth()."<br>" .
"getPixelFormat: " . $ffmpegInstance->getPixelFormat()."<br>" .
"getBitRate: " . $ffmpegInstance->getBitRate()."<br>" .
"getVideoBitRate: " . $ffmpegInstance->getVideoBitRate()."<br>" .
"getAudioBitRate: " . $ffmpegInstance->getAudioBitRate()."<br>" .
"getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate()."<br>" .
"getVideoCodec: " . $ffmpegInstance->getVideoCodec()."<br>" .
"getAudioCodec: " . $ffmpegInstance->getAudioCodec()."<br>" .
"getAudioChannels: " . $ffmpegInstance->getAudioChannels()."<br>" .
"hasAudio: " . $ffmpegInstance->hasAudio();
8.执行后如果拿到视频的一些信息如下就表示环境配置成功了,那我们就可以开始开发我们的视频操作。
执行结果:
略....
9.实现视频的截图 。(接上面的php代码)
$ff_frame = $ffmpegInstance->getFrame(20);//截取视频第20帧的图像
$gd_image = $ff_frame->toGDImage();
$img=$_SERVER['DOCUMENT_ROOT']."/test2.jpg";//要生成图片的绝对路径
imagejpeg($gd_image, $img);//创建jpg图像
imagedestroy($gd_image);//销毁一图像
10.关于ffmpeg-php的API。
Method | Description |
$movie = new ffmpeg_movie(String path_to_media, boolean persistent) |
Open a video or audio file and return it as an object.
|
$movie->getDuration() | Return the duration of a movie or audio file in seconds. |
$movie->getFrameCount() | Return the number of frames in a movie or audio file. |
$movie->getFrameRate() | Return the frame rate of a movie in fps. |
$movie->getFilename() | Return the path and name of the movie file or audio file. |
$movie->getComment() | Return the comment field from the movie or audio file. |
$movie->getTitle() | Return the title field from the movie or audio file. |
$movie->getAuthor() alias $movie->getArtist() | Return the author field from the movie or the artist ID3 field from an mp3 file. |
$movie->getCopyright() | Return the copyright field from the movie or audio file. |
$movie->getArtist() | Return the artist ID3 field from an mp3 file. |
$movie->getGenre() | Return the genre ID3 field from an mp3 file. |
$movie->getTrackNumber() | Return the track ID3 field from an mp3 file. |
$movie->getYear() | Return the year ID3 field from an mp3 file. |
$movie->getFrameHeight() | Return the height of the movie in pixels. |
$movie->getFrameWidth() | Return the width of the movie in pixels. |
$movie->getPixelFormat() | Return the pixel format of the movie. |
$movie->getBitRate() | Return the bit rate of the movie or audio file in bits per second. |
$movie->getVideoBitRate() |
Return the bit rate of the video in bits per second.
NOTE: This only works for files with constant bit rate. |
$movie->getAudioBitRate() | Return the audio bit rate of the media file in bits per second. |
$movie->getAudioSampleRate() | Return the audio sample rate of the media file in bits per second. |
$movie->getFrameNumber() | Return the current frame index. |
$movie->getVideoCodec() | Return the name of the video codec used to encode this movie as a string. |
$movie->getAudioCodec() | Return the name of the audio codec used to encode this movie as a string. |
$movie->getAudioChannels() | Return the number of audio channels in this movie as an integer. |
$movie->hasAudio() | Return boolean value indicating whether the movie has an audio stream. |
$movie->hasVideo() | Return boolean value indicating whether the movie has a video stream. |
$movie->getFrame([Integer framenumber]) |
Returns a frame from the movie as an
ffmpeg_frame
object. Returns false if the frame was not found.
|
$movie->getNextKeyFrame() | Returns the next key frame from the movie as an ffmpeg_frame object. Returns false if the frame was not found. |
ffmpeg_frame object methods
Method | Description |
$frame = new ffmpeg_frame(Resource gd_image) |
Create a frame object from a GD image.
NOTE: This function will not be available if GD is not enabled. |
$frame->getWidth() | Return the width of the frame. |
$frame->getHeight() | Return the height of the frame. |
$frame->getPTS() alias $frame->getPresentationTimestamp() | Return the presentation time stamp of the frame. |
$frame->resize(Integer width, Integer height [, Integer crop_top [, Integer crop_bottom [, Integer crop_left [, Integer crop_right ]]]]) |
Resize and optionally crop the frame. (Cropping is built into ffmpeg resizing so I'm providing it here for completeness.)
NOTE: Cropping is always applied to the frame before it is resized. Crop values must be even numbers. |
$frame->crop(Integer crop_top [, Integer crop_bottom [, Integer crop_left [, Integer crop_right ]]]) |
Crop the frame.
NOTE: Crop values must be even numbers. |
$frame->toGDImage() |
Returns a truecolor GD image of the frame.
NOTE: This function will not be available if GD is not enabled. |
Method | Description |
$gif = new ffmpeg_animated_gif(String output_file_path, Integer width, Integer height, Integer frame_rate, [Integer loop_count]) |
create a new ffmpeg_animated_gif object
|
$frame->addFrame( ffmpeg_frame frame_to_add) |
Add a frame to the end of the animated gif.
|