[英]FFMpeg - Creating Thumbnail for Video file
我是php的新手。 我在我的本地iis中安裝了ffmpeg ..是否可以為flv文件生成縮略圖圖像。 請幫忙..
試試這個
$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 filename (源視頻的文件路徑)
-deinterlace (將隔行掃描視頻轉換為非隔行掃描格式)
-an (錄音已禁用;我們不需要縮略圖)
-ss position (輸入視頻被解碼並轉儲,直到時間戳到達位置 ,我們的縮略圖位置以秒為單位)
-f 輸出文件格式
-t duration (輸出文件在持續時間后停止寫入)
-r fps (將幀速率設置為寫入; fps以赫茲(1 /秒)表示;我們使用1以便我們只抓取一幀)
-y (如果輸出文件已存在,則覆蓋輸出文件)
-s widthxheight(幀的大小,以像素為單位)
下面是使用PHP FFMPEG Library創建的腳本ic。 您可以使用給定的功能獲取視頻信息和縮略圖
<?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
<?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);
?>
沒有必要指定時間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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.