php FFMpeg - 为视频文件创建缩略图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9095918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:09:12  来源:igfitidea点击:

FFMpeg - Creating Thumbnail for Video file

phpffmpegffmpeg-php

提问by user1006460

I am new to php. I have installed ffmpeg in my local iis.. Is it possible to generate a thumbnail image for a flv file.? Please help..

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

回答by ravi404

Try this

尝试这个

$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`;

An explanation of the command options:

命令选项的解释:

-i filename(the source video's file path)
-deinterlace(converts interlaced video to non-interlaced form)
-an(audio recording is disabled; we don't need it for thumbnails)
-ss position(input video is decoded and dumped until the timestamp reaches position, our thumbnail's position in seconds)
-f output file format
-t duration(the output file stops writing after durationseconds)
-r fps(sets the frame rate to write at; fpsis expressed in Hertz (1/seconds); we use 1 so that we grab only one frame)
-y(overwrites the output file if it already exists)
-swidthxheight (size of the frame in pixels)

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

回答by Kiran

Following script ic created using PHP FFMPEG Library . You can grab the video information and thumbnail using the given functions

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

<?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;

}






}




?> 

Function

功能

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

will create number of thumbnails in folder specified in the function . You can alter the code according to your requirement . This works if you installed ffmpeg and php ffmpeg library .

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

Refer this link http://tecserver.blogspot.in/2009/07/ffmpeg-video-conversion-tool.html

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

回答by rdxhere

 <?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);
  ?>

回答by Lloyd Moore

There is no need to specify a time of 1. The ffmpeg api provides a format of image2 and the ability to pass -vframes 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);

回答by Sjoerd

Yes. You can run ffmpeg from PHP using the right parameters.

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

Using ffmpeg-php, you can use $movie->getFrame() to obtain a frame, and $frame->toGDImage() to get a GD image.

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