GreyBeard Inc.

    
    
     

Tips for Handling Video Uploads

    I am not a huge video fan, though watching a video online does have an undeniable coolness factor. I have done some client work involving videos for a site and during the process ran into a few things that might be useful to others trying to handle video. There are quite a few open source and inexpensive tools to help deal with video handling, and combined with some server settings and PHP code it is possible to build some pretty cool video features.

- Uploading

    Uploading a video is no different than uploading any other kind of file. PHP code can be used to check the $_FILES array to make sure the file is a supported format and size, and then move it to a permanent storage location. I usually also maintain a DB table with the resulting filename to use for building displays and associating meta information with the video. Video files are usually a bit bulky so be sure to check the maximum upload settings in your php.ini and possibly apache configuration files (or .htaccess files!).  Uploading a decent size video will normally be a slow process so it might be a good idea to include a progress bar to let the user know that something is happening. PHP has no support for this with the exception of a PHP5 progress meter extension that provides information about pending uploads in temporary files on the server. This information can be used via an ajax call or refreshing iframe to show a user the status of the in progress upload.

- Converting

    There are many video formats out there so for easy and consistent playback of uploaded videos it's a good idea to to convert them to a standard format. A little digging online showed me that YouTube converts movies to FLV files for playback (FLV is a "flash video file") and there are many inexepensive flash/js video players available to handle playback of FLV files from within a webpage. The trick is then how to convert the uploaded file to the correct FLV format for playback. I found the mencoder program to be a good solution for this. Getting this installed can be a bit tricky depending on your server OS. I managed to get it up and working on both Debian Unstable and Red Hat Enterprise 5 without too much trouble. Once mencoder is installed the next trick is navigating it's HUGE array of command line switches and options. I found that the same set of options that worked on one machine did not always work on another, so here is the command that works on RHE5, but you may have to tweak a few things depending on your OS and mencoder version:

mencoder input_file.mpg -o output_file.flv -of lavf -ovc lavc -oac lavc -lavcopts vcodec=flv:vbitrate=250:autoaspect:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:precmp=2:cmp=2:subcmp=2preme=2:turbo:acodec=mp3:abitrate=56 -vf scale=320:240 -srate 22050 -af lavcresample=22050 -ofps 25 -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames

This should all be one single line command by the way. The result is a FLV file built from the uploaded video that is resampled to 25FPS at 320x240, which is exactly what YouTube does with uploaded video (at least at the time I wrote this, they may have increased the capabilities since then). A nice benefit of using mencoder for this is it has support for loads of video file formats. 

- Screenshots

    Having a nicely converted and resampled video is great, but what about showing a screenshot of the uploaded video? I decided to use another great command line tool, ffmpeg, to build a thumbnail of the video. Like mencoder ffmpeg has a dizzing array of options and flags. Here is a command that builds a thumbnail at 320x240 resolution from an FLV file. It takes the snapshot 5 seconds into the video playback, which is a nice way to get an actual representation of the video content instead of a thumbnail of a splash screen.

ffmpeg -y -i input_file.flv -vcodec png -vframes 1 -an -f rawvideo -ss 5 -s 320x240 output_file.png

    The above command should be all on one line, and should output a nice 320x240 PNG file that can be associated with the video.

- Playback

    Since in the above examples I choose to convert the videos to FLV files, we then need a flash video player for playback purposes. There are tons of these available, and while I don't recall a completely open source version, I did find several excellent packages that are very inexpensive to license and use. One could also convert uploaded files to another format, like mpeg, and let a users browser handle the playback. I think the inline flash player is nicer way to watch the video personally (you won't hear me recommend flash very often!) but either way works.

Handling videos is not that difficult thanks to the excellent tools available on many server operating systems. mencoder and ffmpeg surely won't be installed by default and for some operating systems you will likely need to look outside the official package repositories. I found the rpms required for RHE1 at the DAG repo, and for Debian the mencoder/mplayer tools are available through the www.debian-multimedia.org apt repository.

 


Images
No Images with this post
Comments
No comments posted yet

Add a comment


Name:
Email:
Subject:
Comment:
Security Image:
security image
Enter the letters you see above.