Thursday, May 6, 2010

How to encode video for the Sansa Connect using FFmpeg

Tarq (my BFF, yo) sent me some goodies in the mail... a bottle of fine Michigan maple syrup from Camp Daggett (his home away from home) and...

A Sansa Connect MP3 player!

I already have a Sansa e250 MP3 player onto which I have installed the awesome, awesome, awesome Rockbox firmware.  The Sansa e200 series models are among the best-supported by Rockbox and there are so many improved features: MPEG-2 video playback, FM radio presets and recording, games, better battery life, faster startup time, album art, Ogg Vorbis and FLAC playback, MicroSDHC memory card support, etc.  The e250 only cost me about $30, and yet its hardware is comparable to the iPod Nano, and its features are way better.

On paper, the Sansa Connect looks like a nice upgrade... its specs include a larger 2.2" 320×240 screen (1.8" 220x176 for the e200 series), 4 GiB flash and a MicroSDHC slot, decent-sounding speakers so that you can listen without headphones, and a faster 125 MHz ARM processor that should help it play higher-resolution videos.  Most excitingly, it features 802.11b/g WiFi  support!  No FM radio, though that feature never worked very well on the e200 except outdoors, due to the poor antenna.

Unfortunately, what the nifty hardware giveth, the poor software taketh away.  Like a lot of embedded devices, the Sansa Connect is based on embedded Linux, which is really cool but a lot of companies really suck at implementing it.  The firmware is really buggy and I have already managed to freeze it in several different ways.  The WiFi is only used for one thing: Internet Radio.  Cool, so I can listen to ShoutCast MP3 streams, right?  Not so fast... the device is tightly coupled to Yahoo! Music, which is now defunct, so you can't listen to any Internet radio at all :-p

Getting video onto this thing is also a PITA.  Sansa provides the crummy Sansa Media Converter software for Windows only.  It's a pain to install and a pain to use.  Fortunately, some clever folks on various forums have figured out how to use FFmpeg to convert video files to the various weird video formats that Sandisk's Sansa firmware always requires.  (You would think a Linux-based device would have awesome video format support based on the extensive libraries of open-source codecs available, but no...)

This thing is just crying out to be hacked to run a real Linux distro, or Rockbox.  Just think of the possibilities... you could use it as a WiFi network finder, a portable WiFi remote, a USB WiFi adapter, a streaming media player... there are all kinds of nifty apps that could run on this thing.  Unfortunately, it's also been discontinued, probably because the crummy firmware didn't win it many fans.

Video conversion

It took me a couple of hours to figure this out, so I thought I would post information on how to convert video for the Sansa Connect under Linux.  You'll need FFmpeg, with the "unstripped" codec libraries which include the not-quite-GPLed FAAC encoder (easily install prepackaged versions from Medibuntu if you're running Ubuntu).  Here's the command line to use under Ubuntu 9.10/Karmic:

ffmpeg -i INPUTFILE -r 29.97 -vcodec libxvid -b 640k -acodec libfaac \
  -ab 128k -ar 48000 -g 300 -s 320x240 -ac 2 -f mp4 -aspect 4:3 \
  OUTPUTFILE

This says... take the input file and convert it to MPEG-4 video (Xvid) at 30 fps in 320×240 resolution, with AAC stereo audio, and a combined bitrate of about 768 kB/s.

Several forums actually suggested fine-tuning the video encoding settings with additional options:

ffmpeg -i INPUTFILE -r 29.97 -vcodec libxvid -b 640k \
  -bufsize 4M -qmax 51 -mbd 2 -flags +4mv+aic -trellis 1 -cmp 2 -subcmp 2 -g 300 \
  -acodec libfaac -ab 128k -ar 48000 -g 300 -s 320x240 -ac 2 -f mp4 -aspect 4:3 \
   OUTPUTFILE

I'm not enough of a video codec expert to understand all of these, but they do improve quality and file size slightly, although they make FFmpeg run a good deal slower (~45 fps with these options, vs. ~110-120 without, on my dual-core AMD64 system).

If you try to alter the video resolution, aspect ratio, container format, or video/audio codecs... you will either cause the Sansa Connect to refuse to play it, to horribly mangle the video display, or to crash the player altogether. If you encode the video with MP3 audio instead of AAC... instant crash on transfer.  It'll also break if you make the bitrate too high (I'm not sure where "too high" is, but the above settings look great for the videos I've transcoded). Clearly they only tested this thing with the exact format produced by their own crummy software. >:-(

But, if you do follow one of the command lines above... you'll get MP4 files that you can transfer to the Sansa Connect's Video folder, and play successfully. Woohoo!

No comments:

Post a Comment