The straightforward way to create your own PHP video upload tool
Written by Bert Goossens
Question: How many websites use video these days? Answer: A lot!
Should we? Well, users uploading videos provide extra interest and interactivity on your website, help to build a community, add relevant content and allow conversations between your users.
But, wait a second … Question: How many different video formats exist? Answer: A lot! Problem? No, just use the right video upload tool.
But, hold on a minute, … Question: How do you create the right video upload tool? Answer: Keep reading to find out.
To recap the situation, multiple visitors want to upload videos in lots of different formats for you to show on your website. And you need to find a way to automatically convert these videos into a format usable on your website.
Sounds simple, doesn’t it?
There are some strong tools and extensive libraries available that can handle this type of job. The best known and most powerful is FFMPEG.
FFMPEG is a great tool for converting audio and video files. It uses the libavcodec library (this is one of the biggest codec libraries around, which is important for the variety of video formats your users are likely to upload). Popular video players, such as Mplayer and VLC, use this library. Plus, the latest libavcodec version supports the latest format types, including HTML5 video.
You can download FFMPEG here (Link: ffmpeg.org) or here(Windows users).
BTW: I used the Windows version. The installation was fairly easy. I downloaded the files from arrozcru.org and put the exe file in the root folder of the web server.
Whichever version you download, FFMPEG is easy to access. You send commands with PHP using the exec method to open an external program, in this case FFMPEG.
Let’s look at an example:
exec(“ffmpeg.exe -y -i $input -s qvga -sameq -f flv $output 2> $logfile”)
So, what’s happening? FFMPEG converts an input file ($input) to an output flv file ($output). The input and output variables are strings that represent the path to the file. At the same time, we’re also writing a log file ($logfile) that will be used later to provide the user with progress updates … and, in a worst-case scenario, it can tell us why a video wasn’t converted properly.
Which parameters did we use?
-i filename input name -y overwrite output files -s size set frame size -sameq use same video quality as source -f fmt format output
As you may already know, there are a huge number of possible settings and options for converting videos. Luckily, FFMPEG has some preset options available to get you started.
Our video player needs a snapshot of the video. This is another easy task with FFMPEG. For example: exec(“ffmpeg.exe -i $input -an -ss 00:00:05 -r 1 -vframes 1 -f mjpeg $output”;
Which parameters did we use this time?
-an disable audio recording -ss seek to given time position in seconds. hh:mm:ss -r frame rate -vframes the number of frames to be taken
FYI: mjpeg is a motion jpeg that creates a jpeg of a video frame. More options can be found on http://www.ffmpeg.org/ffmpeg-doc.html.
Remember: Security issues might prevent you from using the exec function The PHP GD library needs to be enabled
It’s now time to start converting, isn’t it? Well, actually, no, not yet.
Your user now has some questions that need to be answered: What’s the status of the conversion? How long is it going to last? Is everything working OK?
FFMPEG can help answer those questions. The logging txt file shows you how many seconds have already been converted. Combine this information with the FFMPEG-PHP extension (which can tell you how long the video is) and you can calculate progress as a percentage. The FFMPEG-PHP extension can also tell you the bit-rate, title, author, frames, …
Now you can start converting.
Over to you: So, what do you think? Are you ready to start creating your own video upload tool?








