Coding For Online Success

Using FFMpeg to create YouTube clone script

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

$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:
http://www.jeroenwijering.com/?item=JW_FLV_Player

<div align="center">
<script type="text/javascript" src="lib/swfobject.js"></script>
<p id="player1" style="padding:4px 4px 4px 4px; border:#999999 1px solid; width:323px;"><br><strong>ERROR</strong><br>Looks like you do not have the Flash plugin installed, or you are using a older version (7 is the minimum required)<br><a href="http://www.macromedia.com/go/getflashplayer"><u>Get the Flash Player</u></a> to see this player/movie.<br><br></p>
<script type="text/javascript">var s0 = new SWFObject("lib/flvplayer.swf","single","323","263","7");
s0.addParam("allowfullscreen","true");
s0.addVariable("showdownload","true");
s0.addVariable("link","<?=$webroot?>/videos/<?=$FLVName?>"); // wmv might be an option
s0.addVariable("file","<?=$webroot?>/videos/<?=$FLVName?>");
s0.addVariable("image","<?=$webroot?>/videos/<?=$ThumbName?>");
s0.addVariable("smoothing","false");
s0.addVariable("backcolor","0xFFFFFF");
s0.addVariable("largecontrols","false");
s0.addVariable(‘width’,‘323’); // reset width/height for IE issues and cache/refresh
s0.addVariable(‘height’,‘263’);
s0.write("player1");</script>
</div>

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!

I’m looking now for a decent dedicated Gentoo hosting.
I want to install on it some YouTube clone script for my client.
Could you suggest a good hosting company that you are personally using and have a good experience with?
You can e-mail me to: lkolchin [at] gmail.com

All due respect. I’ve provided working code.. which I use myself.. but I don’t want to show off the site I built this for. Also, to me, me providing an example is not needed. Go to YouTube. It simply takes a WMV/AVI/etc and converts it to FLV.. just like YouTube.. or many other sites that allow for video uploads and Flash players.

Hi! Great post, really simple. I just have one doubt: the convertion of a large video file will make the proccess to take a while right? So in most cases the default execution time of the php will be overpassed. Youtube just let you add the video and then if its not yet converted they said “come back” in a while.

Thanks for posting! I think you could do this with some server side magic. I would check the file size after the upload. If the file size is more than than what you want to do convert in real time, you could push the file to a tmp directory. Dump the data related to that entry in a ‘pending’ table. At this time you would present that message to the user.

Then use a cron job, to look for pending jobs. Have it look every 5 minutes. If a job is found, then process the video and push the pending data into your main table when it’s completed.

You still will need to worry about execution time, and memory consumption with your server. So you may need to adjust settings within your php.ini. Remember to watch for weirdness in the logs.

You need to have PHP script that handles uploads to the server. I would be concerned about security if this is open to the public. Make sure you validate the MIME type with a ‘approver’ script of some sort on the server. Do NOT look at the extension or by looking at the $_FILES global since these can be manipulated by the attacker. I would look file_info or using `file` to verify the MIME after it’s upload before moving it to a directory that is accessible.

For those of you having trouble with the server configurations, you may want to look at a FLV hosting alternative. I found FLV Hosting, which offers and promotes FLV hosting and could be a excellent solution to provide streaming media.

hi..this tutorial is very useful..actually i’m using zend framework for my project..i’ve create a video controller class and in this class i’ve got an upload function..where should i put this encode script so that all of the formatted video that has been uploaded will be convert to flv automatically before it will be displayed..tq

I haven’t messed with the Zend framework before, but I would create a new method that handles this within the class and call it after the file has been uploaded via your upload method. Verify that file is there and is the right MIME type, then manipulate the file.

You can’t. That’s the problem with a cheap solution like GoDaddy hosting, unless you’ve signed up with a dedicated hosting solution. If you don’t have a dedicated solution, then a shared hosting environment that specializes in Flash video is the way to go…

I found this hosting company that offers and promotes FLV hosting. Could be a very good solution for some of you! Hope this helps!

Good question. There are quite a few out there. I would just google Gentoo hosting and start looking at features. The other option is to see if other big names can install it for you, or at least give you the ability to send them a live CD for the install. In the past I did this with Rackspace. Currently, they’re WAY too expensive, and I would not recommend them now because of this. I would find a good Gentoo friendly hosting facility, and look for other’s opinions on their reputation.

I would also recommend posting a question on the Gentoo forum. That place is awesome, you should get a great response to that question there.

This is the extension. I use the extension to get additional information about the video. You might be able to figure out how to use the binary alone with exec() to get file information and skip the extension. I would still ask the host if they can install it.

I will gladly pay anyone, who can develop a php upload script using the ffmpeg that is already installed on my server, so I can simply upload videos (in other formats) to be converted to FLV – like what YouTube does.

I am not building a Video Sharing site, I am trying to add some videos to some products in addition to the images which we can already upload.

Hi the tutprial is great I have used it but when I check my Videos folder the converted file has 0 kb. I have ffmpeg on my hosting plan but no flvtool which i guess is not important as it only provides meta data . can any one help/