从视频文件生成预览图像?

发布于 2024-08-17 07:12:13 字数 68 浏览 3 评论 0原文

PHP 有没有办法给定视频文件(.mov.mp4)来生成缩略图预览?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮萍、无处依 2024-08-24 07:12:13

解决方案#1(较旧)(不推荐)

首先安装 ffmpeg-php 项目 (http: //ffmpeg-php.sourceforge.net/

然后你可以使用这个简单的代码:

<?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.'">';
    }
}
?>

描述:这个项目使用二进制扩展.so文件,它非常旧,上次更新是 2008 年。因此,可能不适用于较新版本的 FFMpegPHP


解决方案#2(更新2018)(推荐)

首先安装PHP-FFMpeg项目(https://github.com/PHP-FFMpeg/PHP-FFMpeg)
(只需运行安装:composer require php-ffmpeg/php-ffmpeg

然后您可以使用这个简单的代码:

<?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.'">';

描述:它是更新、更现代的项目,可与最新版本的 FFMpegPHP 配合使用。请注意,它是 proc_open() PHP 函数所必需的。

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.

安稳善良 2024-08-24 07:12:13

我想到了两种方法:

  • 使用命令行工具,例如流行的 ffmpeg ,但是您几乎总是需要一个自己的服务器(或一个非常好的服务器管理员/托管公司)来实现

  • 使用“< screenshoot" 插件="http://www.longtailvideo.com/" rel="noreferrer">LongTail 视频播放器,允许创建手动屏幕截图,然后将其发送到服务器端脚本.

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.

如梦亦如幻 2024-08-24 07:12:13

我推荐 php-ffmpeg 库。

提取图像

您可以使用以下命令在任何时间码提取帧
FFMpeg\Media\Video::frame 方法。

此代码返回一个 FFMpeg\Media\Frame 实例,对应于
第二个 42. 您可以传递任何 FFMpeg\Cooperative\TimeCode 作为参数,
有关详细信息,请参阅下面的专用文档。

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

如果您想从视频中提取多个图像,可以使用以下过滤器:

$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');

默认情况下,这会将帧保存为 jpg 图像。

您可以使用 setFrameFileType 覆盖此设置,以另一种格式保存帧:

$frameFileType = 'jpg'; // either 'jpg', 'jpeg' or 'png'
$filter = new ExtractMultipleFramesFilter($frameRate, $destinationFolder);
$filter->setFrameFileType($frameFileType);

$video->addFilter($filter);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文