Saturday, May 02, 2009

ffmpeg on OS X.5 Leopard

This is one of those geeky posts that I hope will benefit some random Googling soul when they stumble upon it in the future.

Quick backstory for context: we're getting ready to launch several "video tours" on Minnehaha's home page; to present these, I want to use the Shadowbox javascript library, and then also its preferred flash video player, JW FLV Mediaplayer. There is some limited documentation for both of these resources, though unfortunately not a copious amount.

I've spent a few days battling with Shadowbox and Media Player's configurations; in the end, my solution has been to hard code a flashVars value into the shadowbox.js file that points to a configuration file that Media Player will use. So, in shadowbox.js, near the top, in the list of option, I added

flashVars:{config:'/path/to/config.xml',lightcolor:'FFFFCC'}

Because, as I found out, the lightcolor value doesn't seem to work if you put it in the config.xml file. That config file is just some XML that tells the flash player to load some plugins, which skin to use, and how one of the plugins should behave.



Eventually, I had a local copy of Media Player working, and I'd used QuickTime Pro to export FLVs of our videos (originally all HD QuickTime MOVs). Then I wanted to another one of the plugins from the Media Player site so that users could email a link to their friends from within the player itself. Well, the plugin I wanted didn't work with the version of the player I had - turns out there's a newer player (4.4) available, so I downloaded that. However, this caused a major issue: when I clicked on a video link, the player popped up and the audio played, but the video portion remained black. I found solace in discovering that many others were also experiencing this issue of audio but no video, but I was greatly disappointed that no one has yet come up with a definitive solution.

Obviously, I could have just reverted back to the old player and been done with it, but no, I decided to try to find a solution and make it work. Just 15 minutes, that's it...

5 hours later, I finally threw in the towel, put back the old player, ditched the plugin, and things work beautifully again. However, there was some value in that time spent, because in the process I ended up installing a free program called ffmpeg, and I will now be using it instead of QuickTime to convert all our videos to FLVs ("flash videos").



For anyone who might be trying to install ffmpeg on your Mac OS X.5 Leopard machine, the rest of this post is for you. It's not as easy, straightforward, or well-documented as it should be, but here's what ended up working for me. Please note, you need to use the command line for almost everything from here on out. If that freaks you out, well, you'll have to get over it, it's actually not that bad when you get into it.

Step 1: Download and install yasm: http://www.tortall.net/projects/yasm/wiki/Download

Download the source tar file (currently version 0.8.0), uncompress it in Finder (by double clicking it), then open Terminal (in Applications > Utilities) and navigate your way to the folder (probably in your Downloads folder, in which case you would type cd ~/Downloads/yasm-0.8.0).

Type ./configure

Type make

Wait patiently.

Type sudo make install (you'll be prompted for your password at this point)


Step 2: Download and install Git: http://git-scm.com/download

Follow the same steps from above to change into the directory, configure, compile, and install.


Step 3: Download and install the FAAC encoder package: http://www.audiocoding.com/downloads.html

Use this command to configure: ./configure --enable-shared

Make and install.


Step 4: Download and install the x264 encoder (this one requires yasm to compile properly)

In Terminal, type git clone git://git.videolan.org/x264.git

This will download it into your current working directory, after which you can cd into it
./configure --enable-shared
make
and
sudo make install


Step 5: Download LAME version 3.97 - the current version is 3.98, but it has issues with ffmpeg, at least on Leopard.

Lame source files: http://sourceforge.net/project/showfiles.php?group_id=290&package_id=309

Configure, make, install.


Step 6: Download ffmpeg: http://www.ffmpeg.org/download.html

This configure command worked for me, it may or may not work for you; if not, well, I'm sorry, you're on your own, I was lucky enough to get it working for me :) This site may or may not be of any additional help to you, otherwise, use Google. (NOTE: this command needs to be all on one line)

./configure --prefix=/usr/local/bin --enable-static --enable-shared --enable-gpl --enable-postproc --enable-pthreads --enable-libmp3lame --enable-libx264 --enable-libfaac --disable-ffserver --disable-ffplay --enable-nonfree

Make, install.


Step 7: Pour yourself a drink, you've earned it.


Step 8: Review the ffmpeg documentation. It looks nasty to start with, but it's actually not that bad. If you just take one flag at a time, it won't take more than about 15 minutes for you to figure out what's going on. If you just want to start converting movies right now, here's the command I've been using that seems to generate quality results with a decently small filesize:

ffmpeg -i /path/to/SourceMovie.mov -ar 44100 -r 30 -ab 128k -ac 1 -acodec libfaac -vcodec libx264 -b 400k -s 768x432 /path/to/DestinationMovie.flv

Whoa! That looks complicated! Let's step through it:

ffmpeg: the program, obviously
-i /path/to/SourceMovie.mov: the input file (in my case, it's a QuickTime .mov, but it could be anything)
-ar 44100: audio sampling rate for the output file
-r 30: video frame rate for the output file
-ab 128k: audio bitrate
-ac 1: number of audio channels
-acodec libfaac: audio codec
-vcodec libx264: video codec
-b 400k: video bitrate (in bits/second)
-s 768x432: size / dimensions
/path/to/DestinationMovie.flv: the path for the output file (in my case, an .flv, but it could be anything)

The documentation on all these flags for ffmpeg is quite comprehensive and thorough, and Google also has a number of good resources.

Happy encoding!




Addendum:

After testing the FLV streaming from off-campus, I discovered the bitrate on my encoded videos was too high to stream smoothly. The videos worked beautifully on-campus, but watching from home was choppy.

With the help of Robert Swain's site and the ffmpeg documentation, I wrote a new command to encode FLVs in two passes. Surprisingly, though,the two-pass method didn't handle a shaky camera very well, so I had to try something else.

Here is the new command I'm using, which seems to generate files sufficiently small enough to stream smoothly through the Internet's tubes:

ffmpeg -i /path/to/SourceMovie.mov -ar 44100 -ab 128k -ac 1 -acodec libfaac -vcodec libx264 -s 768x432 -b 400k -bt 100k -vpre hq /path/to/DestinationMovie.flv

1 comment:

Mom said...

Wow! This looks impressive, but it's all Greek to me. Step #7 I understand, however. A cup of hot tea would do me fine. I think I'll go have one, right now...