Generate preview image from Video file?

前端 未结 4 1198
南笙
南笙 2020-11-27 12:06

Is there a way in PHP given a video file (.mov, .mp4) to generate a thumbnail image preview?

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 12:10

    Solution #1 (Older) (not recommended)

    Firstly install ffmpeg-php project (http://ffmpeg-php.sourceforge.net/)

    And then you can use of this simple code:

    getFrame($frame);
    if ($frame) {
        $gd_image = $frame->toGDImage();
        if ($gd_image) {
            imagepng($gd_image, $thumbnail);
            imagedestroy($gd_image);
            echo '';
        }
    }
    ?>
    

    Description: This project use binary extension .so file, It's very old and last update was for 2008. So, maybe don't works with newer version of FFMpeg or PHP.


    Solution #2 (Update 2018) (recommended)

    Firstly install PHP-FFMpeg project (https://github.com/PHP-FFMpeg/PHP-FFMpeg)
    (just run for install: composer require php-ffmpeg/php-ffmpeg)

    And then you can use of this simple code:

    open($movie);
    $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($sec));
    $frame->save($thumbnail);
    echo '';
    

    Description: It's newer and more modern project and works with latest version of FFMpeg and PHP. Note that it's required to proc_open() PHP function.

提交回复
热议问题