FFMpeg - Creating Thumbnail for Video file
Asked Answered
T

5

6

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..

Teacher answered 1/2, 2012 at 12:36 Comment(2)
possible duplicate of Thumbnail video player in PHPRankle
Possible duplicate of Generate preview image from Video file?Alembic
G
16

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 duration seconds)
-r fps (sets the frame rate to write at; fps is expressed in Hertz (1/seconds); we use 1 so that we grab only one frame)
-y (overwrites the output file if it already exists)
-s widthxheight (size of the frame in pixels)

Gift answered 3/2, 2012 at 5:3 Comment(1)
Hint: Put the infile -i value after --ss to considerably speed up the process.Albi
F
3

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

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

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

Ferrosilicon answered 1/2, 2012 at 14:31 Comment(0)
L
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);
  ?>
License answered 20/7, 2014 at 13:34 Comment(1)
this code is not compelete. you should add $mov = new ffmpeg_movie('./Wildlife.wmv'); instead of "$mov = new ffmpeg_movie($pic, false);" that it mean you are using "Wildlife.wmv" exactly on your root drectory close to the .php file. and also you should change "imageGif($new, $name, 100);" to "imageGif($new, "yourname.gif", 100);" it will create a file named "yourname.gif" close to your php file. hope help someoneRealpolitik
C
1

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.

$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);
Crosshead answered 27/8, 2014 at 12:20 Comment(0)
M
0

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

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

Madsen answered 1/2, 2012 at 12:41 Comment(1)
How to use $movie->getFrame().?Teacher

© 2022 - 2024 — McMap. All rights reserved.