Generate preview image from Video file?

前端 未结 4 1175
南笙
南笙 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:08

    Take a look at http://ffmpeg.org/ along with this discussion http://board.phpbuilder.com/showthread.php?10327812-How-to-install-ffmpeg-php-to-create-thumbnails-from-videos

    0 讨论(0)
  • 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:

    <?php
    $frame = 10;
    $movie = 'test.mp4';
    $thumbnail = 'thumbnail.png';
    
    $mov = new ffmpeg_movie($movie);
    $frame = $mov->getFrame($frame);
    if ($frame) {
        $gd_image = $frame->toGDImage();
        if ($gd_image) {
            imagepng($gd_image, $thumbnail);
            imagedestroy($gd_image);
            echo '<img src="'.$thumbnail.'">';
        }
    }
    ?>
    

    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:

    <?php
    require 'vendor/autoload.php';
    
    $sec = 10;
    $movie = 'test.mp4';
    $thumbnail = 'thumbnail.png';
    
    $ffmpeg = FFMpeg\FFMpeg::create();
    $video = $ffmpeg->open($movie);
    $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($sec));
    $frame->save($thumbnail);
    echo '<img src="'.$thumbnail.'">';
    

    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.

    0 讨论(0)
  • 2020-11-27 12:15

    Two ways come to mind:

    • Using a command-line tool like the popular ffmpeg, however you will almost always need an own server (or a very nice server administrator / hosting company) to get that

    • Using the "screenshoot" plugin for the LongTail Video player that allows the creation of manual screenshots that are then sent to a server-side script.

    0 讨论(0)
  • 2020-11-27 12:34

    I recommend php-ffmpeg library.

    Extracting image

    You can extract a frame at any timecode using the FFMpeg\Media\Video::frame method.

    This code returns a FFMpeg\Media\Frame instance corresponding to the second 42. You can pass any FFMpeg\Coordinate\TimeCode as argument, see dedicated documentation below for more information.

    $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(42));
    $frame->save('image.jpg');
    

    If you want to extract multiple images from the video, you can use the following filter:

    $video
        ->filters()
        ->extractMultipleFrames(FFMpeg\Filters\Video\ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, '/path/to/destination/folder/')
        ->synchronize();
    
    $video
        ->save(new FFMpeg\Format\Video\X264(), '/path/to/new/file');
    

    By default, this will save the frames as jpg images.

    You are able to override this using setFrameFileType to save the frames in another format:

    $frameFileType = 'jpg'; // either 'jpg', 'jpeg' or 'png'
    $filter = new ExtractMultipleFramesFilter($frameRate, $destinationFolder);
    $filter->setFrameFileType($frameFileType);
    
    $video->addFilter($filter);
    
    0 讨论(0)
提交回复
热议问题