ffmpegSeveral years ago, I was approached by a media company that created TV commercials for businesses. They were looking for someone to convert their recorded TV commercials into a web compatible format. After a long period of what seemed like endless negotiations, we finally worked out an agreement that would allow me to convert their television commercials into a web format, but only if I could display those videos on one of my websites. From there, other website owners could register with our site, at which point they were provided with a small bit of source code that could display our “web commercials” on their sites. Those website owners were paid per page impressions as well as for click-thrus. The system worked great and we all made a lot of money. However, around the time to renew our contract, the media company decided they could convert the commercials cheaper on their own than they could for paying me. So, the contract was not renewed and we went our separate ways.

After a while, we changed the website to allow anyone to upload videos of their own. However, I was never interested in competing with sites like Hulu or YouTube. And, I was not interested in battling with the courts and the MPAA over copyrighted material. So, like several of my other sites, I sold out and got out. Today, the original site no longer exists and the buyers are no where to be found. But, lucky for us, I still have a little bit of the original source code that I want to share with you now. This isn’t the final source code that was used in production, but it should be enough to point you in the right direction for creating your own YouTube-ish video sharing website. So, let’s have a look.

First off, I’m going to assume that you already have at least a minimum understanding of PHP. If not, there are plenty of helpful websites out there in the interwebernets that can help you out.

The first thing you’re going to need for creating your own video sharing website is a simple HTML form that allows you to select your videos. This form can include any fields such as title, description, keywords, categories, tags, etc… but the only required field is the one for the file itself. Also, when you setup your new form, you will need to make sure to add an encoding type of “multipart/form-data”. This will allow you to upload both ascii & binary formats from within the same form. Here is a simple HTML page to get you started.

<html>
<head>
<title>Video Upload Form</title>
<body>
<form method=”POST” name=”frmVideoUpload” enctype=”multipart/form-data” action=”./upload.php“>
Video: <input type=”file” name=”filename” /><br/>
<input type=”submit” name=”cmdSubmit” value=”Upload” />
</form>
</body>
</html>

Next, you are going to need to create a PHP file by the same name that you used in the “action” from the form above (“upload.php” in the example). Inside that PHP file, you will need to setup a variable for capturing and storing the video file being submitted.

$filename = basename($_FILES['filename']['name']);

When you upload files via PHP, the files are uploaded to a temporary folder first. You can reference this temporary file by calling “tmp_name” from the file you uploaded. You’ll see that just below. Once your videos have been uploaded, you will need to tell PHP to move the newly uploaded file from the temporary directory into the folder of your choice. PHP makes it easy for us to this by calling the “move_uploaded_file” method that’s built into the PHP framework.

move_uploaded_file($_FILES['filename']['tmp_name'], “videos/” . $filename);

You’ll notice that I’ve chosen to have my videos stored in a folder called “videos“. You can change this to whatever you like. In fact, it might be a good idea to upload your videos to a folder that is outside of your web root. This will prevent anyone from accessing your videos directly which can eat up a lot of bandwidth on your server if you’re not careful. It’s also good to point out that PHP requires the file to be completely uploaded to the server before you can get information about the file such as extension, size, etc… This can be a big security vulnerability if left unattended. In a later article, I’ll teach you how to secure your temporary files. But for now, we’ll just work with what we have.

Once you have your file uploaded into your new folder, go ahead and setup variables to hold the file extension of your video file as well as a temporary file name. While we’re at it, we’ll also go ahead and setup a few variables for an image which we’ll use to store a screenshot thumbnail taken directly from the video. This is a nice thing to have when you want to list several videos on one page, but don’t want to require the user to download every video while the page is being loaded initially.

$pathinfo = pathinfo(“videos/$filename”);
$fileext = $pathinfo['extension'];
$newfilename = basename($filename, $fileext);
$newpngname = $newfilename . “png”;
$newfilename = $newfilename . “flv”;
$newswfname = $newfilename . “swf”;

As you can see, I have chosen to create the thumbnails for the videos as PNGs. In order for our videos to be played back in a web browser, I have chosen to have the videos converted into Flash videos. This conversion will be done by our good buddy “ffmpeg”. If you don’t already have ffmpeg installed, you’ll need to do that before any of this will work. Installation and usage of ffmpeg is outside the scope of this article. So, I won’t be going over any of that here. But, once you have ffmpeg installed, you can pass your new video into ffmpeg and let it do the voodoo it does best.

$command = “/usr/local/bin/ffmpeg -y -i ‘videos/$filename’ -acodec mp3 -ab 96 -ar 11025 ‘videos/$newfilename’”;
$status = system(“$command”, $retval);

As you can see above, I called ffmpeg and passed it the name of the file that we uploaded and move into our “videos” folder along with a few extra parameters such as an audio codec for mp3 (-acodec mp3) and an output name. There are also a few other parameters I’ve passed in. You should check the ffmpeg website for more information on those parameters and for finding more parameters that might suit your needs a little better. After you call the “system” function, PHP will execute your ffmpeg command. At that point, ffmpeg will go ahead and convert your video and save it into the folder and file name you specified above. At this point, we will pass another command to ffmpeg telling it to save a thumbnail image of one of the frames in our video. In my command, I chose to have my thumbnail images created at the 5 second mark of the video. This is helpful since a lot of the videos we converted for commercials started out with a fade-in or something there of. If we didn’t create our thumbnail image from a frame later in the video, the thumbnail would’ve been just a black image. Another thing you can see in the code below is that I chose to have ffmpeg create my images with a size of 160×120. You can change this number to whatever works best for you.

$command = “/usr/local/bin/ffmpeg -y -i ‘videos/$newfilename’ -f image2 -ss 5 -vframes 1 -s 160×120 -an ‘videos/$newpngname’”;
$status = system(“$command”, $retval);

That’s pretty much it. By this point, if everything went according to plan, you should be able to upload a video and have ffmpeg convert it into a Flash video and create a thumbnail image of that video. If you want to do something such as get the length of the video, you will need to use something like “mplayer” to do that. At the time I wrote the commercial converting website, ffmpeg did not provide any kind of support for getting a video length. But, you should still check the ffmpeg site as they might have that ability in the latest release. If you do want to use “mplayer” for getting the duration of the video, here are the commands to do that.

$command = “/usr/local/bin/mplayer -identify \”videos/$newfilename\” -nosound -vc dummy -vo null”;
$status = exec(“$command”, $retval);
$video_length = substr($retval[18], 10);

In my next article, I will share with you a simple Flash player that you can embed into your web pages which can play your newly converted Flash videos in the web browser. Until then, happy coding.

<?php

$filename = basename($_FILES['filename']['name']);

move_uploaded_file($_FILES['filename']['tmp_name'], "videos/" . $filename);

$pathinfo = pathinfo("videos/$filename");
$fileext = $pathinfo['extension'];
$newfilename = basename($filename, $fileext);
$newpngname = $newfilename . "png";
$newfilename = $newfilename . "flv";
$newswfname = $newfilename . "swf";

$command = "/usr/local/bin/ffmpeg -y -i 'videos/$filename' -acodec mp3 -ab 96 -ar 11025 'videos/$newfilename'";
$status = system("$command", $retval);

$command = "/usr/local/bin/ffmpeg -y -i 'videos/$newfilename' -f image2 -ss 5 -vframes 1 -s 160x120 -an 'videos/$newpngname'";
$status = system("$command", $retval);

$command = "/usr/local/bin/mplayer -identify \"videos/$newfilename\" -nosound -vc dummy -vo null";
$status = exec("$command", $retval);
$video_length = substr($retval[18], 10);

?>

Related Posts

Tagged with:  

One Response to Create a Video Upload Site with PHP and ffmpeg

  1. [...] be played inside of a web browser. If you haven’t read that article yet, you should click here and read it first as this article will be building on that article. In this article, I’m [...]

Leave a Reply