Thursday, October 4, 2012

How to take screenshot from video in php


Here i am going to explain you how to take screenshot(thumbnail) from video in php.

First Of All we need to installed ffmpeg on our server.If your server does not have this facility please contact your web hosting provider.

function captureImageFromVideo($imagename,$videoname)
 {

$ffmpeg = '/usr/bin/ffmpeg';

//video dir
$video = '/public_html/video/'.$videoname;


//where to save the image
$image = '/public_html/image/'.$imagename;

//what time to take screenshot from video, here it will take screenshot after 5 second from it being started.
$interval = 5;

//screenshot size
$size = '290x191';

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

//PHP built in function execute an external program    
exec($cmd);

}

We can also use ffmpeg to convert videos from one format to another.
For example if you uploading video which have .wmv extension. suppose if i want to convert this video to .mp4 format you can easily achieve this using ffmpeg.
We can use this ffmpeg command for this task
$cmd=”$ffmpeg -i $videofile -vcodec libx264 -acodec libfaac test.mp4“;

The installed ffmpeg should support the libx264 and libfaac.

I hope it will help some of you guys !

1 comment: