3

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

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

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

任何想法?

4

2 回答 2

11

您可以使用“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 时重新创建更新缩略图。

享受

于 2009-09-25T01:33:13.627 回答
1

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

    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 上可用,这个片段来自的特定文件在这里

于 2012-08-16T00:21:49.427 回答