6

我是 php 新手。我已经在我的本地iis中安装了ffmpeg。是否可以为flv文件生成缩略图。?请帮忙..

4

5 回答 5

15

试试这个

$ffmpeg = 'ffmpeg.exe';

//video dir
$video = 'video.flv';

//where to save the image
$image = 'image.jpg';

//time to take screenshot at
$interval = 5;

//screenshot size
$size = '320x240';

//ffmpeg command
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";       
$return = `$cmd`;

命令选项的解释:

-i文件名(源视频的文件路径)
-deinterlace(将隔行视频转换为非隔行格式)
-an(禁用音频录制;缩略图不需要它)
-ss位置(输入视频被解码并转储,直到时间戳到达位置,我们的缩略图的位置,以秒为单位)
-f输出文件格式
-t持续时间(输出文件在持续时间秒后停止写入)
-r fps(设置要写入的帧速率;fps以赫兹(1/秒)表示); 我们使用 1 以便我们只抓取一帧)
-y(如果输出文件已经存在,则覆盖它)
-s widthxheight(帧的大小,以像素为单位)

于 2012-02-03T05:03:25.843 回答
2

以下脚本 ic 使用 PHP FFMPEG 库创建。您可以使用给定的功能获取视频信息和缩略图

<?php

class PHP_FFMPEG
{

function getVideoInformation($videoPath)
{
$movie = new ffmpeg_movie($videoPath,false);

$this->videoDuration = $movie->getDuration();
$this->frameCount = $movie->getFrameCount();
$this->frameRate = $movie->getFrameRate();
$this->videoTitle = $movie->getTitle();
$this->author = $movie->getAuthor() ;
$this->copyright = $movie->getCopyright();
$this->frameHeight = $movie->getFrameHeight();
$this->frameWidth = $movie->getFrameWidth();
$this->pixelFormat = $movie->getPixelFormat();
$this->bitRate = $movie->getVideoBitRate();
$this->videoCodec = $movie->getVideoCodec();
$this->audioCodec = $movie->getAudioCodec();
$this->hasAudio = $movie->hasAudio();
$this->audSampleRate = $movie->getAudioSampleRate();
$this->audBitRate = $movie->getAudioBitRate();


}


function getAudioInformation($videoPath)
{
$movie = new ffmpeg_movie($videoPath,false);

$this->audioDuration = $movie->getDuration();
$this->frameCount = $movie->getFrameCount();
$this->frameRate = $movie->getFrameRate();
$this->audioTitle = $movie->getTitle();
$this->author = $movie->getAuthor() ;
$this->copyright = $movie->getCopyright();
$this->artist = $movie->getArtist();
$this->track = $movie->getTrackNumber();
$this->bitRate = $movie->getBitRate();
$this->audioChannels = $movie->getAudioChannels();
$this->audioCodec = $movie->getAudioCodec();
$this->audSampleRate = $movie->getAudioSampleRate();
$this->audBitRate = $movie->getAudioBitRate();

}

function getThumbImage($videoPath)
{
$movie = new ffmpeg_movie($videoPath,false);
$this->videoDuration = $movie->getDuration();
$this->frameCount = $movie->getFrameCount();
$this->frameRate = $movie->getFrameRate();
$this->videoTitle = $movie->getTitle();
$this->author = $movie->getAuthor() ;
$this->copyright = $movie->getCopyright();
$this->frameHeight = $movie->getFrameHeight();
$this->frameWidth = $movie->getFrameWidth();

$capPos = ceil($this->frameCount/4);

if($this->frameWidth>120)
{
$cropWidth = ceil(($this->frameWidth-120)/2);
}
else
{
$cropWidth =0;
}
if($this->frameHeight>90)
{
$cropHeight = ceil(($this->frameHeight-90)/2);
}
else
{
$cropHeight = 0;
}
if($cropWidth%2!=0)
{
$cropWidth = $cropWidth-1;
}
if($cropHeight%2!=0)
{
$cropHeight = $cropHeight-1;
}

$frameObject = $movie->getFrame($capPos);


if($frameObject)
{
$imageName = "tmb_vid_"1212.jpg";
$tmbPath = "/home/home_Dir/public_html/uploads/thumb/".$imageName;
$frameObject->resize(120,90,0,0,0,0);
imagejpeg($frameObject->toGDImage(),$tmbPath);
}
else
{
$imageName="";
}


return $imageName;

}






}




?> 

功能

getThumbImage($videoPath); //pass path to video file //

将在函数中指定的文件夹中创建缩略图数。您可以根据需要更改代码。如果您安装了 ffmpeg 和 php ffmpeg 库,这将有效。

请参阅此链接 http://tecserver.blogspot.in/2009/07/ffmpeg-video-conversion-tool.html

于 2012-02-01T14:31:55.113 回答
1
 <?php

$W = intval($_GET['W']);
 $H = intval($_GET['H']);
$pic = ''.htmlspecialchars($_GET['file']).''; 
$name = 'wapadmin/'.str_replace('/','--',$pic).'.gif';
$location = 'http://'.str_replace(array('\\','//'),array('/','/'),    $_SERVER                 [ 'HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.$name);

 if(file_exists($name)){
header('Location: '.$location, true, 301);
 exit;
}

$mov = new ffmpeg_movie($pic, false);
$wn = $mov->GetFrameWidth();
$hn = $mov->GetFrameHeight();


$frame = $mov->getFrame(480);

$gd = $frame->toGDImage();

 if(!$W and !$H){
  $a = "131*79";
  $size = explode('*',$a);
 $W = round(intval($size[0])); 
 $H = round(intval($size[1])); 
  }


 $new = imageCreateTrueColor($W, $H);
 imageCopyResampled($new, $gd, 0, 0, 0, 0, $W, $H, $wn, $hn);
 imageGif($new, $name, 100);

  header('Location: '.$location, true, 301);
  ?>
于 2014-07-20T13:34:15.213 回答
1

无需指定时间为 1。ffmpeg api 提供了 image2 的格式和传递 -vframes 1 的能力。

$time = 3;
$infile = 'in.mp4';
$thumbnail = 'my_thumbnail.png';

$cmd = sprintf(
    'ffmpeg -i %s -ss %s -f image2 -vframes 1 %s',
    $infile, $time, $thumbnail
); 

exec($cmd);
于 2014-08-27T12:20:16.347 回答
0

是的。您可以使用正确的参数从 PHP 运行 ffmpeg 。

使用 ffmpeg-php,可以使用 $movie->getFrame() 获取帧,使用 $frame->toGDImage() 获取GD 图像

于 2012-02-01T12:41:52.137 回答