In previous article, I provide information how you can handle vidoes using ASP.NET, in this article i will extend this functionality to PHP.
Uploading videos, grabbing its thumbnail and converting video to the format that is runnable on the web is now a days a popular source of increasing web site traffic online. One of the main example is www.youtube.com which provides online videos sharing features.
Media Handling Involves the following operations.
- Uploading Videos using PHP Form.
- After Uploading, converting it into FLV Format so that it can be runnable in real time on web.
- Grabbing its thumbnail to display on the list.
- Displaying FLV Video on Plash Player.
You can upload videos using PHP same as uploading other files on the web such as images, documents but little bit more work require for setting web server, because videos is little bit big in size, and upload large files require some extra settings. you will use file html input tag to upload videos, also you can use any third party tool to upload videos on the server.
<input name="file1" id="file1" name="file1" />
Converting Videos to FLV Format so that it can be runnable in real time
Once video has been successfully upload, the next step is to convert that video in FLV Format, you can customize this code to convert video into other formats like mp4, 3gp, if you want to create web application for sharing Videos on Mobile Phone, e.g real time convertion of Media according to the mobile phone that user want to download on it.
For conversion i use FFMPEG, an open source which is a powerful tool now a days for media conversion from one source to another. For using FFMPEG in your application, you must first download its fresh copy from its official web site. You can also download FFMPEG and FLVTool from this article, see below.
After downloading, extract it and put the whole folder in your php application. Also i used FLV Tool for setting Buffereing for FLV converted video, because FFMPEG can't set Buffereing for FLV Video. . After downloading , unzip it and place in root folder of web application.
Sample Video Conversion Program in which i convert a video in flv format , set its buffering and grab its thumbnail.
<?php
class media_handler
{
function convert_media($filename, $rootpath, $inputpath, $outputpath, $width, $height, $bitrate, $samplingrate)
{
$outfile = "";
// root directory path, where FFMPEG folder exist in your application.
$rPath = $rootpath."\ffmpeg";
// which shows FFMPEG folder exist on the root.
'// Set Media Size that is width and hieght
$size = $width."x".$height;
'// remove origination extension from file adn add .flv extension, becuase we must give output file name to ffmpeg command.
$outfile =$filename;
'// Media Size
$size = Width & "x" & Height;
'// remove origination extenstion from file and add .flv extension , becuase we must give output filename to ffmpeg command.
$outfile = 'out_file.flv';
'// Use exec command to access command prompt to execute the following FFMPEG Command and convert video to flv format.
$ffmpegcmd1 = "ffmpeg -i ".$inputpath."\".$filename. " -acodec mp3 -ar " .$samplingrate." -ab ".$bitrate."
-f flv -s ".$size." ".$outputpath."\".outfile;
$ret = shell_exec($ffmpegcmd1);
'// return output file name for other operations
return $outfile;
}
<!-- Becuase FFMPEG can't set Buffering for flv files , so i use another tool that is FLVTool to set buffering of flv file. You must put all files with root folder into the main folder of your web application. -->
function set_buffering($filename,$rootpath,$path)
{
'// root directory path
$_rootPath = rootpath."\flvtool"
'// Execute FLV TOOL command also on exec , you can also use other tool for executing command prompt commands.
$ffmpegcmd1 = "flvtool2 -U ".Path."\".$filename;
$ret = shell_exec($ffmpegcmd1);
'// Execute this command to set buffering for FLV
}
<!-- This function is used to Grab Thumbnail Image from Generated Flv files , to be display on the list, Note that, FFMPEG can create thumbnail only from FLV Files.. -->
function grab_image($filename, $rootpath, $inputpath,$outputpath, $no_of_thumbs, $frame_number, $image_format, $width, $height)
{
'// root directory path
$_rootpath = rootpath."\ffmpeg";
'// Media Size
$size = width. "x".height;
'// I am using static image, you can dynamic it with your own choice.
$outfile = "sample.png";
$ffmpegcmd1 = "ffmpeg -i ".$inputpath."\".filename." -vframes ".$no_of_thumbs." -ss 00:00:03 -an -vcodec ". $image_format." -f rawvideo -s ".$size. " ". $outputpath."\".$outfile;
$ret = shell_exec($ffmpegcmd1);
'// Execute this command using exec command or any other tool to grab image from converted flv file.
return $outfile;
}
}
So this is sample utility class for handling media using php , you can extend its functionality to use every type of ffmpeg command to get complex video handling task in your php application.
Below i show you how you can use this class in your php application.
$_mediahandler as new media_handler;
$rootpath ="../";
$nputpath = $rootpath."/Default";
$outputpath = $rootpath. "/FLV"
$ThumbPath = $rootpath. "/Thumbs"
Save original video in Default folder.
$source = $HTTP_POST_FILES['file1']['tmp_name'];
$name = $HTTP_POST_FILES['file1']['name'];
$fileSize = $HTTP_POST_FILES['file1']['size'];
$filetype = $HTTP_POST_FILES['file1']['type'];
$dest = '';
copy($source, $inputpath . $name);
'// Convert it into FLV Format
$outfile = $_mediahandler.convert_media($name,$rootpath, $inputpath, $outputpath, 320, 240, 32, 22050);
'// Grab Image from it.
$image_name = $_mediahandler.grab_image($outfile, $rootpath, $outputpath, $thumbpath, 1, 2, "png", 110, 90)
'// user flv tool to set buffering, for this to work you must first download FLVTool and take it into the root folder of your .
$_mediahandler.Set_Buffering($outfile, $rootpath, $outputpath)
'// Save all information in the database for other manipulation.
I think this example will give you idea how you can easily manage videos in your PHP Application,. Conversion of Media can take place in the background depend on the size of Video, But PHP suddenly process the whole code without waiting for conversion and displaying user message that you set. so if you show Media on the list , Only show those Videos that is added , ten or twenty minutes lates, due to conversion factor.
Showing FLV Video on the Web
After successfully conversion of your Video into flv format, you can use any flash player to display this media on your web application like in www.youtube.com.
Here is version of FFMPEG that i used in my project, for latest version of FFMPEG, please visit official FFMPEG web site.
Click here to Download FFMPEG for Window.
Clich here to Download FLVTool.
If you have any query, feedback, comment regarding this article, email me on irfan.nettech@gmail.com. I will appreciate your feedback, comments, query inorder to extend this article more and more.