Video thumbnail

匆匆过客 提交于 2019-12-04 14:09:48

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1474957/video-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!