Generate preview image from Video file?

前端 未结 4 1209
南笙
南笙 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: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);
    

提交回复
热议问题