So this entry, I’m going to get a bit complicated. I was working on a project where administrators needed to upload Windows Media formatted videos (WMV), and wanted them converted to Flash video (FLV). This would duplicate what YouTube or GoogleVideo is currently doing. The browser would then use a Flash player to play the streamed videos. Also, I would need to capture a frame for the preview. I thought this tutorial might be a good resource for others that might be looking for this functionality for their affiliate sites, etc.
Now, I must discuss my environment a bit. This tutorial will cover Linux (specifically Gentoo Linux), using PHP and a Apache web server. Also, you will need some vital packages (ffmpeg-php, ffmpeg and flvtool2). I’m guessing that a few web hosts our there won’t have any of these packages available. This where the argument of getting a dedicated server or virtual server shines. If you had your own server you could install and configure it whatever way you want. I personally prefer dedicated servers. You can pickup dedicated hosting for approximately $100-$300/month depending on what server specifications you’re looking for. All of my servers are around the $150/month rate and work great for me.
ffmpeg-php is a fantastic PHP extension, that ties into ffmpeg and can return fantastic and incredibly useful information of a raw video file (such as frame rate, bitrate and can also grab a GD image handle of a frame in the video).
ffmpeg is a audio/video converter (make sure it’s compiled with amr, encode, ogg, oss, sdl, truetype, vorbis, zlib).
flvtool2 is a manipulation tool for Flash Video files, but we need to use it to embed META data extracted from our WMV file.
You’ll also need to be sure that PHP is compiled with GD support.
I’m going to skip the upload piece to the server. If you need more information on how to get the source WMV file to the server, let me know.
Here is the main chunk of code to worry about. I’ll explain this after the code snippet.
$ffmpeg = "/usr/bin/ffmpeg"".""/path/to/videos/""/path/to/videos/"".jpg"; // just get the first part of the name
$imagePath = "/path/to/videos/".$thumbname;
imagejpeg($imageSnap, $imagePath, 100);
$cmd = $ffmpeg . " -i " . $wmvName . " -ar " . $srcAR . " -ab " . $srcAB . " -acodec mp3 -f flv -deinterlace -nr 500 -s 320×240 -aspect 4:3 -r ".$srcFPS." -b 270k -me_range ".$srcFPS." -i_qfactor 0.71 -g 500 " . $flvPath . " | " . $flvtool2 . " -U stdin "
$ffmpegObj is a object we’re creating from ffmpeg-php. Here we pass the path to where our original source WMV file is. ffmpeg-php then analyzes the file and we have various methods to extract information. The makeMultipleTwo function is there, since the width and height has to be a multiple of two (this was discussed at a great resource here). Along with the width and height information, we wan thte frame rate, audio sample rate and audio bit rate. Those are found by using the following methods (getFrameRate(), getAudioBitRate() and getAudioSampleRate()).
Next use the getFrame() method, which will grab a frame snap from the video. In this example, we’re grabbing the 3rd frame. This also returns a new object which we’re calling $frameSnap. We can access various methods within this new object (getWidth() which is the width of the image, getHeight() which is the height of the image, and toGDImage() which will return us a GD handle for the image). Once we have the GD handle for the image, we can simply write the image to the file system with imagejpeg() which is a method in the GD library.
At this point we have information about the WMV file and a frame snap for the preview image. We need to to convert the file to FLV now. Our next step is to create a command string which we’ll need to execute.
This converts the file to FLV, but also ‘pipes’ the output to FLVtool2 with the ‘|’. ffmpeg converts the file, but then we use flvtool2 to overwrite the file, but add important META information for FLV.
If your PHP environment is in safe_mode you will run into a problem with the ‘|’ in the command string. Since safe_mode runs all commands through escapeshellcmd() which will try to escape that character. To get around this is semi-tough if you do not have shell access to the server. I run safe_mode on my servers, so what I did was create a bash script with the command string in it, and passed values via exec() to my bash script. The ‘|’ pipe command is in the bash script, so it was not being escaped by PHP. Any time you’re dealing with exec() and shell scripts, it’s incredibly important to check and sanitize user input!!!!!
This is a tricky project, but can be quite cool when you get it working. The next piece is to get a flash player to play your FLV file. I personnally like this one:
s0.addParam("allowfullscreen","true""showdownload","true""link","<?=$webroot?>/videos/<?=$FLVName?>"); // wmv might be an option
"file","<?=$webroot?>/videos/<?=$FLVName?>""image","<?=$webroot?>/videos/<?=$ThumbName?>""smoothing","false""backcolor","0xFFFFFF""largecontrols","false"‘width’,‘323’); // reset width/height for IE issues and cache/refresh
UPDATE – 1/2/2009
I found this hosting company that offers and promotes FLV hosting. Could be a very good solution for some of you! Hope this helps!