视频缩略图

发布于 2024-08-05 16:26:14 字数 205 浏览 3 评论 0 原文

我正在开发一个人们可以提交视频链接的网站。然后我就嵌入它。但是,我想获取视频的缩略图而不将视频保存在我的服务器中。这样当我列出视频时,我可以使用缩略图而不是嵌入所有视频。

我的服务使用 PHP。假设视频为 SWF 格式。

或标签中是否有任何内容可以让我“抓取”缩略图?或者在PHP中,有什么我可以远程获取远程视频的缩略图(或帧)吗?

有什么想法吗?

I am working on a site that people can submit the video's link. Then I just embed it. However, I want to get the thumbnail of the videos without saving the videos in my server. So that when I list the vidoes, I can use the thumbnails instead of embedding all videos.

My serve uses PHP. Assume the video is in SWF format.

is there anything in the or tag that I can 'grap' the thumbnail? or in PHP, is there anything that I can remotely get the thumbnail (or a frame ) of the remote video?

any idea?

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

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

发布评论

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

评论(2

等往事风中吹 2024-08-12 16:26:14

您可以使用“ffmpeg”。通过使用PHP来调用它。

shell_exec  ("ffmpeg -i \"$FILENAME.flv\" -ss 00:00:04 -f image2 \"$FILENAME.jpg\"");

很遗憾,我还没有测试过,所以先尝试一下。

编辑:只是为了好玩,我将其设为一个函数。这里是:

<?php
 
function GetThumbnailFileName($FileName, $ScreenShortSecond = 10) {
    $VDOLastModifiedDate = filemtime($FileName);
    $Thumbnail_FileName  = sprintf("%s-(%s::%02d).jpg", $FileName, $VDOLastModifiedDate, $ScreenShortSecond);
     
    if (!file_exists($Thumbnail_FileName)) {
        $FFMPEG_Command = sprintf(
            "ffmpeg -i \"%s\" -y -ss \"00:00:%02d\" -f image2 \"%s\" > /dev/null 2>&1",
            $FileName, 0 + $ScreenShortSecond, $Thumbnail_FileName
        );
        system($FFMPEG_Command);
    }
     
    if (!file_exists($Thumbnail_FileName))
        return null;
     
    return $Thumbnail_FileName;
}
 
$FileName  = "Test.flv";
$Thumbnail = GetThumbnailFileName($FileName);
if ($Thumbnail != null)
     echo "Thumbnail file is: \"$Thumbnail\"\n";
else echo "Fail creating a Thumbnail of \"$FileName\".";
 
?>

此功能还缓存缩略图,并确保在修改 VDO 时重新创建更新缩略图。

享受

You can use 'ffmpeg'. by using PHP to call it.

shell_exec  ("ffmpeg -i \"$FILENAME.flv\" -ss 00:00:04 -f image2 \"$FILENAME.jpg\"");

I am sorry to say that I've not test it so try it first.

EDIT: Just for fun, I make it a function. Here it is:

<?php
 
function GetThumbnailFileName($FileName, $ScreenShortSecond = 10) {
    $VDOLastModifiedDate = filemtime($FileName);
    $Thumbnail_FileName  = sprintf("%s-(%s::%02d).jpg", $FileName, $VDOLastModifiedDate, $ScreenShortSecond);
     
    if (!file_exists($Thumbnail_FileName)) {
        $FFMPEG_Command = sprintf(
            "ffmpeg -i \"%s\" -y -ss \"00:00:%02d\" -f image2 \"%s\" > /dev/null 2>&1",
            $FileName, 0 + $ScreenShortSecond, $Thumbnail_FileName
        );
        system($FFMPEG_Command);
    }
     
    if (!file_exists($Thumbnail_FileName))
        return null;
     
    return $Thumbnail_FileName;
}
 
$FileName  = "Test.flv";
$Thumbnail = GetThumbnailFileName($FileName);
if ($Thumbnail != null)
     echo "Thumbnail file is: \"$Thumbnail\"\n";
else echo "Fail creating a Thumbnail of \"$FileName\".";
 
?>

This function also cache the thumbnail and also ensure that the update thumbnail is recreated if the VDO is modified.

Enjoy

睫毛上残留的泪 2024-08-12 16:26:14

我使用上面的一些片段和其他一些来源让它工作,这是我的一些代码:

    private function videoScreenshot($originalFile, $newFile, $percentage = 10)
    {
        // Check ffmpeg is configured
        $config = Nutshell::getInstance()->config;
        $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;
        if(!$ffmpeg_dir) return;

        // Get the potision a percentage of the way in the video
        $duration = $this->getVideoDuration($originalFile);
        $position = ($duration * ($percentage / 100));

        // save the screenshot
        $command = "\"{$ffmpeg_dir}ffmpeg\" -i \"$originalFile\" -ss $position -f image2 \"$newFile\"";
        shell_exec($command);
    }

    private function getVideoDuration($filename, $seconds = true)
    {
        $config = Nutshell::getInstance()->config;
        $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;
        if(!$ffmpeg_dir) return;

        ob_start();
        $command = "\"{$ffmpeg_dir}ffmpeg\" -i \"$filename\" 2>&1";
        passthru($command);
        $result = ob_get_contents();
        ob_end_clean();

        preg_match('/Duration: (.*?),/', $result, $matches);
        $duration = $matches[1];

        if($seconds)
        {
            $duration_array = explode(':', $duration);
            $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
        }
        return $duration;
    }

显然,如果您要在自己的类中使用这些函数,您需要用

    $config = Nutshell::getInstance()->config;
    $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;

自己的配置选项替换这些行。

完整的插件框架 可在 github 上找到,此代码片段来自的特定文件是 此处

I got it working using some snippets from above and some other sources, here's some of my code:

    private function videoScreenshot($originalFile, $newFile, $percentage = 10)
    {
        // Check ffmpeg is configured
        $config = Nutshell::getInstance()->config;
        $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;
        if(!$ffmpeg_dir) return;

        // Get the potision a percentage of the way in the video
        $duration = $this->getVideoDuration($originalFile);
        $position = ($duration * ($percentage / 100));

        // save the screenshot
        $command = "\"{$ffmpeg_dir}ffmpeg\" -i \"$originalFile\" -ss $position -f image2 \"$newFile\"";
        shell_exec($command);
    }

    private function getVideoDuration($filename, $seconds = true)
    {
        $config = Nutshell::getInstance()->config;
        $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;
        if(!$ffmpeg_dir) return;

        ob_start();
        $command = "\"{$ffmpeg_dir}ffmpeg\" -i \"$filename\" 2>&1";
        passthru($command);
        $result = ob_get_contents();
        ob_end_clean();

        preg_match('/Duration: (.*?),/', $result, $matches);
        $duration = $matches[1];

        if($seconds)
        {
            $duration_array = explode(':', $duration);
            $duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
        }
        return $duration;
    }

Obviously if you're going to use these functions in your own class, you'll need to replace the lines

    $config = Nutshell::getInstance()->config;
    $ffmpeg_dir = $config->plugin->Plupload->ffmpeg_dir;

with your own configuration options.

The full plugin and framework are available on github, the particular file this snippet is from is here.

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