Video transcoding for the N97

A big part of why I bought the N97 was the media player. It has some pretty impressive specs: supports the h264 and MPEG4 codecs, the mp4 and 3gp containers, AAC audio … so yeah that’s pretty sweet. First thing I did when I got the phone all charged up was go into the old media library, copy over a file I thought met there specs and load it up into the media player. Yeah it definitely didn’t work.

Upon closer look there are some constraints I wasn’t meeting and unfortunately some bugs I didn’t know about. Let’s start with what I was doing wrong.

First off the constraints on the video dimensions are 630 by 320 pixels. Anything over this won’t play, the phone won’t scale the image down. So we’ve gotta convert the video, no getting around that. First thing I did was look into using GStreamer. I wanted to mock up a pipeline and throw it at gst-launch to do the transcoding. First thing I did was break out the playbin to do the decoding:

gst-launch-0.10 playbin uri:file:///path/to/vid

This does some GStreamer magic detecting all of the media file’s attributes and builds a pipeline in the bin to do the decoding. If you execute the above the bin will build default sinks for both the audio and video streams which will basically play the file.

Now it’s a matter of linking the audio and video streams from the playbin to a pipeline, simple right? I couldn’t figure out a way to do this from gst-launch. In fact I found a mailing list post indicating that this can’t be done through gst-launch. So now it’s build my own application or find one that already does this. As much as I like rolling my own, this is no small feat.

Coincidentally I’m at LinuxCon this week and I ran into another guy with the same phone. He pointed me to HandBrake. That was the ticket. This app has a great CLI interface and supports the codecs we need. It also has some built in profiles for popular devices.

My goal was to produce transcode to h264 at 630×320, AAC (don’t really care about bitrate etc) in an mp4 container. The HandBrake iPhone profile comes pretty close with a few minor modifications:

HandBrakeCLI -i ./infile.avi -o outfile.mp4 -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 --maxWidth 640 --maxHeight 320 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1

This is a pretty compute intensive task. On my laptop it maxed out both CPUs for about a half hour for a 24 minute TV episode. In the end I had an mp4 file that was about 150MB with the desired attributes. Playing it with Totem was successful. The properties menu showed all the attributes were as expected and it looked great. Success?

Not quite. Turns out my N97 still couldn’t play it. The media player just threw up an error, wouldn’t play audio or video. I spent a while playing around with the parameters for the x264 encoder (on a much smaller file) but wasn’t able to get the phone to play any h264 encoded video. On a few occasions it would play the audio but never the video.

So h264’s out and MPEG4’s in. After getting rid of the h264 stuff from the above command I ended up settling on something like this:

HandBrakeCLI --input ./infile.avi --output outfile.mp4 --encoder ffmpeg --rate 24 --audio 1 --aencoder faac --ab 128 --arate 48 --mixdown dpl2 --format mp4 --maxWidth 640 --maxHeight 320 --markers

That’s the trick. Originally I had included the quality flag form the h264 command line. This produced a super small video file, 3.5 times smaller than the original but the video produced looked kinda grainy. Since I’m not pressed for space on my phone yet (32GB of storage is a lot of space) I dropped the quality flag and let it run presumably at 100%. This produced a file about 25MB smaller than the original (likely attributable to the decreased image resolution) with excellent picture quality. Better yet my N97 played it perfectly.

Conclusion: looks like the h264 implementation on the N97 isn’t all it’s cracked up to be. This is likely a bug. So for now, stick to MPEG4 through HandBrake and enjoy some videos on your N97. The new firmware update (v20) has been in the Nokia press recently but I haven’t seen anything about fixing h264 support. Meh.

“Web: No gateway reply” Nokia N97 fix

Better late than never here’s how I got around that pesky “Web: No gateway reply” error message when using my N97 with the WiFi at my house.  Initially the wireless would work but after a while I started getting the error messages.  I took a lucky guess and figured the phone was doing some power saving stuff that was screwing up the link between it and my router.

Seemed reasonable to try to turn off any WiFi related power saving stuff and see if this made any difference.  This setting is buried so you’ll have to go to

Menu > Settings > Connectivity > Wireless LAN > Options > Settings > Options > Advanced Settings

You’ll get a warning messaging when you open the Advanced Settings that says

Changing advanced settings is not recommended. Continue anyway?

If they hide the configuration under a dozen dialog boxes and recommend that you don’t change it, you probably should … just a rule of thumb. Anyways, scroll all the way down to the bottom and you’ll find the “Power saving” configuration.  By default mine was enabled but after disabling it I’m able to use the WiFi in my house quite reliably.

Sorry for the lack of screen shots but it looks like there isn’t a good screen capture application for the N97 yet.  That combined with the convoluted process to get a developer certificate to sign the program before installing it was enough to deter me for now … I’ll get to it eventually 🙂