php 从视频文件生成预览图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate preview image from Video file?
提问by JD Isaacks
Is there a way in PHP given a video file (.mov, .mp4) to generate a thumbnail image preview?
PHP 中是否有给定视频文件 ( .mov, .mp4) 的方法来生成缩略图预览?
采纳答案by mr-sk
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
看看http://ffmpeg.org/以及这个讨论http://board.phpbuilder.com/showthread.php?10327812-How-to-install-ffmpeg-php-to-create-thumbnails-from-视频
回答by Nabi K.A.Z.
Solution #1 (Older) (not recommended)
解决方案 #1(旧版)(不推荐)
Firstly install ffmpeg-phpproject (http://ffmpeg-php.sourceforge.net/)
首先安装ffmpeg-php项目(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 .sofile, It's very old and last update was for 2008. So, maybe don't works with newer version of FFMpegor PHP.
描述:这个项目使用二进制扩展.so文件,它很旧,上次更新是 2008 年。所以,可能不适用于较新版本的FFMpeg或PHP。
Solution #2 (Update 2018) (recommended)
解决方案 #2(2018 年更新)(推荐)
Firstly install PHP-FFMpegproject (https://github.com/PHP-FFMpeg/PHP-FFMpeg)
(just run for install: composer require php-ffmpeg/php-ffmpeg)
首先安装PHP-FFMpeg项目(https://github.com/PHP-FFMpeg/PHP-FFMpeg)
(只运行安装: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 FFMpegand PHP. Note that it's required to proc_open()PHP function.
描述:它是更新和更现代的项目,适用于最新版本的FFMpeg和PHP。请注意,它是proc_open()PHP 功能所必需的。
回答by Pekka
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 playerthat allows the creation of manual screenshots that are then sent to a server-side script.
使用像流行的ffmpeg这样的命令行工具,但是你几乎总是需要一个自己的服务器(或一个非常好的服务器管理员/托管公司)来获得它
使用长尾视频播放器的“ screenshoot”插件,允许创建手动截图,然后发送到服务器端脚本。
回答by hakiko
I recommend php-ffmpeglibrary.
我推荐php-ffmpeg库。
Extracting image
You can extract a frame at any timecode using the
FFMpeg\Media\Video::framemethod.This code returns a
FFMpeg\Media\Frameinstance corresponding to the second 42. You can pass anyFFMpeg\Coordinate\TimeCodeas argument, see dedicated documentation below for more information.
提取图像
您可以使用该
FFMpeg\Media\Video::frame方法在任何时间码提取帧 。此代码返回
FFMpeg\Media\Frame对应于第二个 42的实例。您可以将任何FFMpeg\Coordinate\TimeCode作为参数传递,有关详细信息,请参阅下面的专用文档。
$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 jpgimages.
默认情况下,这会将帧保存为jpg图像。
You are able to override this using setFrameFileTypeto save the frames in another format:
您可以使用setFrameFileType以另一种格式保存帧来覆盖它:
$frameFileType = 'jpg'; // either 'jpg', 'jpeg' or 'png'
$filter = new ExtractMultipleFramesFilter($frameRate, $destinationFolder);
$filter->setFrameFileType($frameFileType);
$video->addFilter($filter);

