Yesterday I spent almost seven hours on the server-side of one of my (numerous) personal projects. It's not that I really hate server-side coding, but I'm a front-end guy, so it can be a pain to do that part. So if you're on your way to prototype the next youtube and you're not an expert on server-side stuff, this post is for you.
After setting up the main class for uploading in php, the first thing you want to do is to call shell_exec($cmd). This is an amazing function in php, allowing you to call any shell command. $cmd will be in that case the ffmpeg command line.
To do so, you need a binary. On mac os x, the simpliest thing is to install ffmpegx (the gui of ffmpeg), open the package and copy the ffmpeg binary into your usr/local/bin
folder so it's accessible from everywhere.
At this point, it's really easy. What can be more tricky is the h264 conversion to make it compatible with flash player 9 and more. What I discovered there is that the h264 codec for encoding is not efficient. I experienced lots of artifacts and bad quality encodings.
After going through ALL the ffmpeg command line options - and there are a lot - deblocking, deinterlace, pixel shapes, bitrate etc, it seems that the codec itself was the problem. On some videos, most of them coming from a Canon 500D camera (which are already h264 encoded files!), it was simply not possible to convert them in any format with a good quality.
The solution is to call another library called libx264. The main problem is you need to recompile your ffmpeg binary to include that lib. Once you try to do so - using ./configure --enable-libx264 - you get a nice error, saying "libx264 not found". I tried many forums, as I'm not used to that kind of coding. It's really tricky to find out the solution if you're not familiar with C programming, and the people working on that kinds of projects (I'm talking about libx264, compiling codecs etc.) are not the most sociable and gentle people on earth, from far. You must be a nerd to enter that world. I mean a proper one. I even try to compile myself libx264 and I also got errors while building, and it became more and more obvious that the solution wasn't to try to fix those low-level problems.
So I finally found the solution on that website: http://24b6.net/content/ffmpeg-binary (not to be confused with http://www.27bslash6.com, which is a very interesting website for a completely different reason^^), which offers you a compiled binary for linux and mac os x. Untar the file, then run the script using the terminal. It replaces the ffmpeg binary with a new one that includes libx264 and other codecs like libfaac for audio, and many others.
Then replace the video codec in the command line to libx264, and don't forget to add aac as the audio codec, so Flash player will properly handle it. All the videos I tried works, in a lot of formats and from different devices. You can use that line, it gives an average good result on everything:
/usr/local/bin/ffmpeg -i yourfile.mov -vcodec libx264 -b 1200 -ar 24000 -ab 32 -acodec aac -ab 96k yourfile.mp4
/usr/local/bin/
needs to be added in order to work in php using mamp, on most of the webserver it's not needed.
Oh and one last thing: don't forget to change the upload_max_size and post_max_size in your php.ini config file, so you won't be limited by the 32M default values when uploading large files.




