Complete FFmpeg Upload and Conversion

Enabling your website to accept video uploads and subsequently process them into a common format for display is no small undertaking. This article intends to inform you of the required steps of the process and then show you how to carry them out in a relatively complete manner. Be advised that there is still room for improvement in operation and security, and how you choose to implement this is up to you. Here is a screenshot of our example website.

Video application screenshot

The complete process

To paraphrase the complete process of video upload and conversion and what we will be covering in this article is as follows;

Receive video from file upload form, transfer video to a storage directory for processing, use FFmpeg to extract information, such as dimensions and duration, about the video to store in the database, set output dimensions of video and video thumbnail, create the thumbnail and process the video conversion. We will also show one method of video display utilizing the free FlowPlayer video player.

Things to consider for this operation are; What output format will we use. Its seems that Flash video (flv) was the de facto standard for ever, it has good quality and great streaming capabilities. The downside however is a relatively large file size, this can take its toll in server disk space and bandwidth usage. From what I can tell the new standard is to convert the video to an mp4 using h.264 (using libx264). This combined with certain server plugins (such as mod_h264_streaming) allow you similar streaming capabilities as flash with a much smaller file size (around 1/3) and comparable video quality.

Pre-requisites

You will need the following in place before using the scripts in this tutorial, otherwise they will not work or you will need to modify them accordingly.

FFmpeg:

FFmpeg is a command line tool used to convert video files between different formats. It can be used to grab images from a video as well as make a video from numerous images. We will be using FFmpeg to grab a thumbnail from our video as well as convert the video file from the uploaded format to our desired output format. FFmpeg must be compiled from source, and the conversion command in this tutorial makes use of the libx264 library, which needs to be built by source and installed on the system prior to building FFmpeg. When you build FFmpeg you will need to set the "--enable-libx264" during configuration. 

Flvtool2

flvtool2 is another command line tool that embeds meta data into flash video files to better facilitate streaming. I believe that recent versions of ffmpeg put the necessary data in there for you, but it is a safe bet to use anyway and guarantees that your flv files stream properly.

qt-faststart

In the same way that meta data is embedded into flv files, mp4 files have meta data as well. The only problem is that FFmpeg puts this information at the end of the file, therefore the entire file needs to be transferred from the server before the video player knows how to stream it, which is obviously counter productive. In the source code folder for FFmpeg there is a directory named "tools" which has a file named "qt-faststart.c". If you have successfully built ffmpeg, then it will be a breeze for you to build qt-faststart.

To build qt-faststart, inside the FFmpeg source code directory change directory into the tools directory. you should see "qt-faststart.c" file in there. Type "make qt-faststart.c qt-faststart". That will create the qt-faststart binary. Copy or move this into the same directory (prob. /usr/local/bin/) as your ffmpeg binary. Done.

Video Player:

You will need some way to play the video files after they are converted. There are many options out there, but I prefer to use Flowplayer. Flowplayer is an easy to set up and use video player that has a plug-in to support h264 streaming. It is free if you don't mind there logo on your video when it is set to full screen. For around $100 dollars you can get a licensed single site version.

mod_h264_streaming (optional)

If you want the ability to stream your mp4 video files you will need to install the mod_h264_streaming mod on your web server. This must be done via the command line but its easy to do and works well. They have options for other various different servers  to suite your needs. 

Prototype (optional)

For this example I use a little javascript to confirm deletion request and initialize the video player javascript. I only javascript in Prototype, so you can either download it for free or implement the old school JS yourself. This is not necessary for the video conversion or display, but is part of the whole picture I am presenting.

I have been playing around with figuring out this video conversion stuff for a while and I always find a way to make it better. This version will allow you to execute the actual conversion with another script and return you to a preview page while the video is processing. It uses a database flag to indicate when conversion is complete and then will display the video if it is or a "still processing" message if not. I have tried to cover all the bases and scenarios I could come up with. Suggestions or examples of improvement will be greatly appreciated.

So Lets get Started. We will first look at creating this projects database and directory structure.