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 !
Read More »

Wednesday, October 3, 2012

How to create zip and download


In this post I am going to explain you how to create zip file and download.
We need some files on some directory which we will zipped by code.
For this task we are using PHP zip library, it must enabled on your server.
Here is function
function zipFilesDownload($file_names,$archive_file_name,$file_path)

{

    $zip = new ZipArchive();

    //create the file and throw the error if unsuccessful

    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {

                    exit("cannot open <$archive_file_name>\n");

    }

    //add each files of $file_name array to archive

    foreach($file_names as $files)

    {

                    $zip->addFile($file_path.$files,$files);

                    //echo $file_path.$files,$files."<br />";

    }

    $zip->close();

    //then send the headers to force download the zip file

    header("Content-type: application/zip");

    header("Content-Disposition: attachment; filename=$archive_file_name");

    header("Pragma: no-cache");

    header("Expires: 0");

    readfile("$archive_file_name");

    exit;

}
There are three parameters in function  $file_names, $archive_file_name and $file_path
$file_names =>  where you have to passed files in array.
$archive_file_name => It is the name of your zip file which is created by PHP library if it’s not open.
$file_path =>  It is your directory path where your files are placed which you want to zipped.
Read More »