ffmpeg-php Installation and use

Source: Internet
Author: User
Tags imagejpeg

(1) Simple call demo, used in a separate file, does not contain classes, does not specify a name space

<?php

Generate thumbnails by specified frame

if (extension_loaded (' ffmpeg ')) {//Determine if FFmpeg is loaded

$mov = new Ffmpeg_movie (' D:\robot.avi ');//The path to the video

$ff _frame = $mov->getframe (2);

$GD _image = $ff _frame->togdimage ();

$img =$_server[' Document_root ']. " /test.jpg ";//to generate an absolute path to a picture

Imagejpeg ($gd _image, $img);//create JPG image

Imagedestroy ($GD _image);//Destroy an image

}else{

echo "FFmpeg not Loaded";

}

?>
Note: Replace the path in the Ffmpeg_movie (' D:\robot.avi ') with the path to your video, and you find that a test.jpg is generated in the same directory as your PHP file, stating that the creation was successful.


(2) Use in namespaces and other classes

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

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

$obj = new \ffmpeg_movie;
$obj = new \datetime;

Reference the external class using use:

Use Ffmpeg_movie;
Use DateTime as Awesomedatetimeclass;

$obj = new Ffmpeg_movie;
$obj = new Awesomedatetimeclass;

(3) Scale picture size

if (extension_loaded (' ffmpeg ')) {
Var_dump (Realpath ($SOURCEFILEURL)); Exit ();
If the destination directory does not exist, the directory is created
$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 "; Absolute path to generate a picture
$targetFilePath = DirName ($TARGETFILEURL). "/test.jpg";
Imagejpeg ($gd _image, $TARGETFILEURL, 80); Create a JPG image

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); Destroying images
}



1. Download FFmpeg http://download.csdn.net/detail/xmlife/8608487

2. Extract FFmpeg 3. will be in addition to php_ffmpeg.dll files, other copies to the Windows\System32 folder

4. Extension=php_gd2.dll
Extension=php_gettext.dll
Extension=php_ffmpeg.dll

5. After restarting the WAMP, use the Phpinfo () function to see the information configuration:

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

Local
DirectiveValue Master Value
Ffmpeg.allow_persistent 0 0


The above indicates that FFMPEG is configured successfully in the PHP environment.

6. Below we build a PHP page to test for some function functions that can be used with ffmpeg. Create a testvideo.php file

The code is as follows:

<?php

extension_loaded (' ffmpeg ');

$ffmpegInstance = new Ffmpeg_movie (' C:\wamp\www\top10.mp4 ');
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 ();

7. After execution, if you get some information about the video to show that the environment configuration is successful, then we can start to develop our video conversion.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.