如何像视频预览一样创建YouTube?

时间:2012-06-11 04:38:08

标签: php ffmpeg

enter image description here如何创建像视频预览一样的YouTube?那是在youtube中,当我们将鼠标悬停在进度条上时,它会显示视频的预览?如何使用ffmpeg或mencoder进行这种预览,还是有其他方法可以做到这一点?看看这张图片。请到这里进行实时预览。

http://www.youtube.com/watch?v=yto4_QFoLdA&feature=watch-now-button&wide=1

1 个答案:

答案 0 :(得分:3)

您可以使用ffmpeg扩展程序。下面是我编写的一些古代代码,用于从视频中选择六个缩略图。

值得注意的部分是:

$frame = $vid->getFrame(); // pull a frame from the video
$gd = $frame->toGDImage(); // turn the frame into a GD image
imagejpeg($gd, "filename.jpg", 70); // save jpeg of the frame using 70% quality

$vid = new ffmpeg_movie ( $fileName );
$frameRate = $vid->getFrameRate ();
$gapSize = max(10,$vid->getDuration()-10) * $frameRate / 6;
$n = 0; $curFrame = 5 * $frameRate;
while ( $n != 6 ) {
    $f = $vid->getFrame ( $curFrame );
    if ((1==$curFrame && false!==$f) || false !== ($f = $vid->getNextKeyFrame ())) {
        $thumbnails [$n] = array (
            'path' => "muveethumb-$muveeId-$n.jpg", 
            'time' => ceil ( ($vid->getFrameNumber () - 1) * 1000 / $frameRate ) 
        );
        $gd = $f->toGDImage ();
        imagejpeg ( $gd, $targetFolder.$thumbnails [$n] ['path'], 70 );
        ++ $n;
        $curFrame += $gapSize;
    } elseif ($n > 0) {
        while ( $n != 6 ) {
            $thumbnails [] = $thumbnails [$n - 1];
            ++ $n;
        }
    } else {
        if ($curFrame!=1) {
            // attempt to get first frame as a last resort
            $curFrame = 1;
        } else {
            // unless we already tried that
            throw new Exception ( "Could not create thumbnails" );
        }
    }
}