Layman uses ffmpeg PHP extension library to implement video capture (default)

Time:2020-10-30

In the past few days, one of the requirements is that the user uploads the video file to the server, and then the server automatically intercepts a frame of the video as the corresponding thumbnail of the video. The server language is written in PHP. After looking for the data for half a day, it is found that ffmpeg PHP can meet the requirements. So, the configuration and use of the extension library are briefly introduced.

The test server environment wampserver version 2.1, php5.3.5, ffmpeg PHP configuration and use steps are as follows:

1. Download ffmpeg PHPffmpeg-php-5.3-win32-all.zip

2. Add PHP_ ffmpeg.dll Put it in the PHP extension directory (for example, e: program files / Wamp / bin / PHP / php5.3.5 / ext)

3. Put other DLL files into the system directory (e.g. C: Windows / system32)

4. Modification php.ini , add extension = PHP_ ffmpeg.dll The results are as follows:

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

notes: in the Wamp environment, to modify the php.ini (for example, e: program files / Wamp / bin / Apache 2.2.17 / bin\ php.ini )With gD2 and gettext modules enabled)

5. Restart the Apache server

6. Print out phpinfo and check whether the following output is available. If so, it indicates that the ffmpeg PHP module is configured correctly

ffmpeg-php

ffmpeg-php version 0.6.0-svn
ffmpeg-php built on Sep 8 2010 12:13:47
ffmpeg-php gd support enabled
ffmpeg libavcodec version Lavc52.87.4
ffmpeg libavformat version Lavf52.78.3
ffmpeg swscaler version SwS0.11.0

 

 

 

 

 

 

 

 

Directive Local Value Master Value
ffmpeg.allow_persistent 0 0
ffmpeg.show_warnings 0 0

 

 

 

 

7. Write the test code, the sample code intercepts the second frame of the video as a thumbnail

public function testmp4()
{
  if(extension_ Loaded ('ffmpeg ')) {// judge whether ffmpeg is loaded
    $mov = new ffmpeg_ Movie ('e: \ "program files \ \ Wamp \ \ www \ \ bbshow \ \ test \ \ guangfa15s. MP4 '); // the path of the video
    dump($mov);
    $ff_frame = $mov->getFrame(2);
    $gd_image = $ff_frame->toGDImage();
    $img=$_ SERVER['DOCUMENT_ ROOT']."/ test.jpg "; // the absolute path to generate the image
    imagejpeg($gd_ Image, $IMG); // create JPG image
    imagedestroy($gd_ Image); // destroy an image
  }else{
    Echo "ffmpeg is not loaded";
  }
}

8. Run the test code to generate JPG thumbnail on the server

9. For detailed instructions on ffmpeg PHP, please refer to the official API(http://ffmpeg-php.sourceforge.net/doc/api/)。

Recommended Today

MVC and Vue

MVC and Vue This article was written on July 27, 2020 The first question is: is Vue an MVC or an MVVM framework? Wikipedia tells us: MVVM is a variant of PM, and PM is a variant of MVC. So to a certain extent, whether Vue is MVC or MVVM or not, its ideological direction […]