Encoding Video for a Zune (or iPod) in Windows

Probably the most common complaint I see on the Zune forum is that people just don't know how to convert videos so that they play back well on their devices.  I've always been a stickler for video quality, but I've never really been willing to pay for professional video editing software.  As a result I've cobbled together a few techniques for resizing my videos using entirely free software.

Note: All of the software I use in this explanation is "free-as-in-beer."  As far as I know, Windows Media Encoder is the only one that isn't also "free-as-in-speech."

Back to Basics

When people jump into these sorts of tutorials, the authors usually assume a bit more base knowledge than they should.  Here's a list of basic rules and concepts you should consider before you convert a video:

  1. Don't Scale Up.  Whenever possible, you want to go from a high-resolution source to a low-resolution source.  This goes for audio too.  Unless you absolutely have to, never use somebody else's streaming video as a source.
  2. Remember the Aspect Ratio.  When you're going to a portable device, you always want to end up with "square pixels."  That means that your video resolution (320x240) should match your picture shape (4:3).  Widescreen DVDs, for example, usually have "non-square pixels," meaning that the resolution (720x480) doesn't match the picture shape (16:9 or 4:3).
  3. Remember the Frame Rate. If you're converting NTSC video, it's 29.97fps, not 30fps.  Film from an NTSC DVD is 23.976fps, not 24fps.  The differences are minor but can lead to strange synchronization and stuttering issues if you screw it up on a long enough video.
  4. Take Notes. You might end up dealing with a little math when you do this.  Write down resolutions & framerates so that you don't have to keep it all in your head

Getting Started

I'm going to show this process in 2 steps: the first is to resize the video using VirtualDub; the second is to encode using Windows Media Encoder.

The first step is to open your video in VirtualDub.  My video is a DVD rip of The Melancholy of Haruhi Suzumiya.  I know I'm technically not supposed to rip my DVDs, but it's nice to watch this stuff on a portable device sometimes.  I'm not going to mention anything about how the rip is made, but you'll need to know what I'm working with so that you can understand the decisions I make in resizing it.

  • The video is in AVI format, XviD video codec, MP3 audio codec (the Windows default -- l3codeca.acm)
  • Resolution: 720x480
  • Aspect Ratio: 16:9 (note: the resolution doesn't match the aspect ratio, so we're dealing with non-square pixels)
  • Audio: 2 channel 48kHz, 128kbps

So we need to figure out what size we're going to convert to.

  • The Zune screen is 320x240 (and so is the iPod screen) with a 4:3 aspect ratio
    • Unlike the basic iPod, the iPod Touch has a 480x320 screen with a 3:2 aspect ratio
  • Our aspect ratio (16:9) is wider than the screen (4:3).
    • If you have aspect ratios you don't recognize and you want to find out which one's wider, divide the first number by the second number. 16/9 = 1.77777.  4/3 = 1.333333.  The higher the number, the wider the aspect.
  • Because our aspect ratio is wider than the screen, we're going to match the screen's width.  Our target width therefore is 320.
  • We can use the aspect ratio to find our height.  320 * (9/16) = 180.  This is your basic ratio function: 320/x = 16/9 and then solve for x.

Now that we know our target resolution, let's get cracking.  First I've gotta open up the video using VirtualDub:

And now we need to apply some filters to do the resizing.  From the "Video" menu, select "Filters."

Click on the "Add" button to add a filter.  First we need to resize.  This little bit here is the secret to good looking low resolution video.

We resize to 640x360, double our target resolution of 320x180.  This gets us into our aspect ratio while preserving details.

Then we add the filter called "2:1 reduction (high quality)."  This will cut the video size in half (down to our target size 320x180) by combining pixels instead of resampling.  This eliminates stair-stepping and sparkling artifacts that can appear when you scale down detailed video.

Next we need to encode an intermediary file with our high-quality resize.  First set your audio settings.

"Direct Stream Copy" will copy your source audio directly without recompressing it.  This will eliminate audio quality loss.

For video, we'll need to use "Full processing mode" to apply our filters.  Then also select "Compression ..." from the menu to pick a codec.  Select XviD from the list and hit "configure."

I usually keep a relatively high bitrate for these intermediary files (1Mbps in this case, which is high for such a low resolution).  We'll be deleting this AVI once we're done converting it to a WMV.

Last, select File -> Save as AVI to start encoding the intermediary file.  You can also set the thread priority to "Idle" so that you can still use your computer while this processes in the background.  It's unnecessary for short clips, but feature length movies are another story altogether.

Using Other Formats With VirtualDub

It's quick and easy to open and work with AVI files in VirtualDub, but if you're going to use any other formats you're going to need to get a framework called AviSynth (and a few plugins you'll have to search for: MPEGDecoder, MPEG2Dec3, and QTSource).  Here's how it works:

  • You create an empty text file.
  • Load any plugins you plan to use.
  • Call the open function for that source.
  • Save the file and change the extension to ".avs".
  • Open the .avs file in VirtualDub

Using AviSynth you can open all sorts of different formats in VirtualDub.  Here's some quick examples of AviSynth scripts:

Opening a WMV File

source = DirectShowSource("my_movie.wmv")

Opening a Quicktime File

LoadPlugin("c:\Program Files\Avisynth 2.5\plugins\QTSource.dll");
source = QTInput("my_movie.mov")

Opening an MPEG File

LoadPlugin("c:\Program Files\Avisynth 2.5\plugins\MPEGDecoder.dll");
source = MPEGSource("my_movie.mpg")

Opening an MPEG2 File

First, open the MPEG2 file like an MPEG1 file:

LoadPlugin("c:\Program Files\Avisynth 2.5\plugins\MPEGDecoder.dll");
source = MPEGSource("my_movie.mpg")

This will create a .d2v file which you can open with the MPEG2 decoder:

LoadPlugin("c:\Program Files\Avisynth 2.5\plugins\MPEG2Dec3.dll")
source = MPEG2Source("my_movie.d2v")

You could actually do all the resizing mentioned above using just AviSynth, but Windows Media Encoder won't open it.  This is because Windows Media Encoder doesn't use Microsoft's default AVI framework, ironically enough.  However it could save you a step if you're going to MPEG4 or h264 like I discuss later.

Compressing With Windows Media Encoder

Once your intermediary file is done compressing, it's time to start up Windows Media Encoder (or WME).

WME greets you with a wizard view for starting a new project.  Just hit cancel.  This wizard mode is slow and cumbersome. After you cancel out of this, click the "Properties" button in the toolbar at the top of the window.

In the first tab, "Sources," select "File" instead of "Devices" and then select your intermediary file.  It's natural for WME to hang for about 10 seconds after you pick a file.

Next comes the "Output" tab.  Uncheck the "Pull from encoder" checkbox and check the "Encode to file" checkbox.  Then use the "Browse" button to select where you want to save this file.

Next move on to the "Compression" tab.  We don't want any of the defaults so just click straight through to "Edit."

First, increase the audio quality.  Remember, the Zune screen may be tiny but it's designed as a high-quality audio player.  You're going to want high quality sound to accompany your videos.  Instead of risking a typo, select "Same as video input" for Video size.  Unfortunately you can't just keep your framerate, so you'll have to enter it explicitly.  You can increase the video bitrate here, but make sure you always set Video Smoothness to 100.  That will prevent WME from blurring your image.

The WME is incredibly slow when you make a file, but can actually render DVD resolutions at real-time when you're streaming to a server.  This drove me insane the first several times I tried to use it.  However, it turns out you can configure the encoder to do the same sort of real-time performance when encoding to a file if you know where to look.

After you finish with the compression settings, go to "Tools" -> "Options" and then to the "Performance" tab. Uncheck "Use defaults" and drag that bottom slider down to the second tick.  The quality difference is negligible but the speed difference is very significant.

Once you finish with that, click on the "Start Encoding" button to let it fly.

This should finish fairly quickly (since it'll render at faster-than-real-time rates).  Once it does you've got one last step before you add the WMV file to your Zune library.

Start dSharpie and drag & drop your new WMV video into the top list.  Select the videos and add metadata on the right.  Once you're done adding the metadata, hit the "down" arrow to push this movie into your list of videos to be saved, then click the "save" icon in the toolbar.  When you import that WMV into the Zune library the software will recognize most of the fields and even organize TV shows and movies correctly.

How About Video for iPods?

Many of the steps above apply for iPod video, except that we won't be going into WMV.  As a result, all of the resizing and intermediate file information from above still flies.  What you'll need instead is a copy of ffmpeg (and you might need to do some searching to get a compiled copy for Windows).  Ffmpeg is a command-line video utility and it's none-too-pretty.

See what I mean?

It's a pain to look at, but in reality, it's actually very straight forward.  You just need to specify the input file, the output file, the format, and the bitrate.  Here's the parts you'll need to know

  • -i <filename> This is where you specify the input file.
  • -f <format> This is the "container" format ("mov", "avi","m4v", etc ... )
  • Video Options:
    • -vcodec <video codec> This is the video compressor to use
    • -b <bitrate> This is the video bitrate
    • -r <fps> The framerate
    • -aspect <aspect> The aspect ratio
  • Audio Options
    • -acodec <audio codec> This is the audio compressor
    • -ab <audio bitrate> The audio bitrate
  • <output filename> The last thing is the output filename, and it has no prefix.

You can place quotes around the filenames to use files with punctuation or spaces in the filenames. 

The trick of course is to create a video file that will be compatible with iTunes so that you can add it to your library and sync it to your iPod.  Keep in mind that I don't have an iPod or iTunes, but these are my best guesses at some options that should work in that case.

Option 1: MPEG4

ffmpeg -i "intermediate file.avi" -f mov -vcodec mpeg4 -r 29.97 -aspect 16:9 -b 512000 -acodec libfaac -ab 128000 "output file.mov"

Option 2: h264

ffmpeg -i "intermediate file.avi" -f m4v -vcodec libx264 -r 29.97 -aspect 16:9 -b 512000 -acodec libfaac -ab 128000 "output file.mp4"

I'm basing my guess that this'll work on the fact that the files created with these options will play back in Quicktime with no issues.  I'd love it if somebody could give it a try and e-mail me from my contact form to let me know how it works out.

Examples To Come

I'll be posting examples soon to demonstrate some of this work.  First though I've gotta shoot or find some non-copyrighted material that will work as a demonstration.

Update - I wasn't really able to find any decent high-quality video that was both free and royalty free.  So instead I'm going to use the Hancock trailer.  I know that this is technically a copyright violation, but I'm hoping that they'll forgive me since I'm essentially giving them free advertising for their film.

This video was converted using the processes described above, but with a little extra magic in AviSynth to achieve the split-screen effect.  On the left you'll see a point-resized version of the trailer (the same way the Zune software resizes the videos it automatically converts) and on the right you'll see the video with the reduce-by-2 resize.  You'll notice a lot of jagged edges and aliasing artifacts on the left that are eliminated on the right.

You can also download a copy below to use on your portable device so you can see what the difference is there.  Again, I'm just guessing that these formats will work on an iPod.  If anybody has any problems with them please let me know so that I can try something else.

Good luck, and I hope this helps you get the most out of your portable device.