Daily Archives: April 28, 2009

Building a Video Transcoding System for Linux

IOTK has a video module that provides a wide range of video manipulation and processing capabilities. When it was created, the goal was to support as many video formats as possible while deploying the system on a 64-bit Linux operating system (the specific distribution was Ubuntu 8.0.4). There were a number of challenges in achieving this, described below, but the most difficult one to overcome was finding all of the necessary software for a 32-bit installation on a 64-bit operating system and getting it to work together.

The specific application for which the system was built was designed to take uploaded videos (in almost any format) and transcode them to Flash video for display using a custom Flash video player. The set of files I was operating on went as far back as 2003 and included old 3G2 and Intel Indeo 5.0 video formats.

Let me describe the user interaction with the system. A user would select a video to upload from their computer. Upon completion of the upload, the application would identify the video as a legitimate, recognized video format, instantiate the transcoding system and configure the parameters for it, transcode the video to Flash, and store the resultant file in IOTK’s DataStore repository. A reference to the file would be created in the database to map the DataStore ID to the record created for the user. Upon playback, the Flash video player would contact the server with a “token” identifying which video to retrieve and the video would be progressively downloaded to the player.

To solve this, I implemented a solution using both ffmpeg and mplayer. Why? ffmpeg, at the time, was unable to identify certain video formats (like Intel Indeo 5.0) but mplayer, specifically mencoder, was. The code I designed would first attempt to manipulate the video using ffmpeg since it had the broadest range of capabilities and format support. If certain types of error conditions were returned from ffmpeg the system would attempt the same operation using mencoder. Moreover, this logic needed to be (and is) unit tested against real videos to make sure it worked properly.

Here is the complete documentation on installing a video transcoding system on a 64-bit Linux operation system. I can tell you, I wish I had this documentation when I was performing the original installation! I have successfully installed this on OpenSuSE 10.2 and 10.3, RedHat Enterprise Release 5, and Ubuntu 8.0.4.

A few IOTK implementation specifics:

function vid_file_mime()
A function to return the MIME type of a video file. Why? The Fileinfo module of PHP does not properly return a MIME type for 3G2 mobile video formats. This wrapper function uses various techniques to identify these issues.

function vid_transcode_to_flash_video()
Using the classes below, sets up and executes the routines required for transcoding.

class vid_Params
Defines the parameters to be used when manipulating a video including audio bit rate and sampling rate, video bit rate, number of audio channels, geometry for the resultant video, and what format to use for it.

class _vid_Transcoder
Given a vid_Params object, an input file, and an output file, executes the steps described in this posting to transcode the video.

The unit tests for this system were designed to attempt to transcode real video files to Flash including detecting when the system had to fail back to mencoder because ffmpeg was unable to perform the tasks. I can confirm that this system works properly for the following video formats:

3GP
AVI
MOV
MPG
WMV
Intel Indeo 5.0 AVI
3G2
MOV (with header compression)

You can see this software in action (including a Flash based video recorder) at any of these sites:

Create a video ecard at Jalapenyo.com
Create a video ecard at FidoFlix.com
Create a video ecard at OnfuegO.com
Create a video ecard at BirthdayVideoEcards.com


Follow

Get every new post delivered to your Inbox.