AnimeMusicVideos.org > Guide Index

Encoding DivX 5-Compatible video with XviD

Before you Encode

Many people think that encoding is all about encoder settings - if you have all the right settings then you will have a perfect encode and it couldn't get any better. This is not true. You can only have a good encode of a source if the source is good to start with.

A good codec is only a small percent of what you need in order to produce a good encode. The rest relies on you being able to prepare your footage to make it easier to encode and making it look prettier before you start losing data in the encoding process. This is the real skill of a good encoder.

So, if you've arrived at this page without looking at the sections on how to prepare your footage for encoding then you should stop reading right now and go back and read those pages. They are going to be more valuable to you than the information contained on this page - trust me.

Before you start to try encoding your video you need to make sure you have done the following - you can't expect to make a decent encode unless all of these things are true:
1) The footage is NOT interlaced.  looks terrible on monitors and causes havoc for encoders. You must remove interlacing before encoding - see this page on deinterlacing.

2) Your footage has been cropped. Having source footage with black borders on any side of the video is a waste of space that could be used to encode the actual active image. These should be trimmed no matter how small.

3) The video is resized to multiples of 16.
Your video should be resized to a resolution that fits the source aspect ratio (usually 4:3). It must be resized to multiples of 16 in order to be fully compatible - 320x240, 512x384, 640x480 are example of 4:3 resolutions that are multiples of 16.

4) Your video has been cleaned. Pretty much every source has some kind of noise. Even the prettiest DVD has this noise and it wastes a fair amount of data when encoding. Temporal and spatial smoothers reduce this noise and give you more bits to play with when encoding the parts you really want to look good. Even cleaning with very light settings that you can't even notice visually will potentially give you a much nicer encode.
The above rules are the absolute minimum. If you haven't done all of these then you can't get the best out of your encoder.


Quick link Index:

  1. Getting Started
  2. Setting up Audio Encoding Options
  3. XviD Compression Options - What's all this about?
  4. Setting up XviD Encoding Options
  5. Twopass Encoding
  6. The First Pass
  7. Saving the First Pass
  8. The Second Pass

Getting Started

This guide is based upon the XviD 1.2.1. This version of XviD is included in the AMVapp and can be downloaded from Koepi's Site.

Note well: Before installing a new copy of XviD you should close down any video programs like Premiere, Virtualdub and so on and uninstall the old one first. This is particularly important if you have a very old version of XviD as you can have many problems if a new version is installed on top of the old one.

Setting up Audio Encoding Options

For compressing to Xvid, we will be using VirtualDub, and we will be using the Lame ACM encoder to compress the audio.

When you have VirtualDub open, you can now go to File > Open Video File... to load your source footage. If you are following these guides correctly you should have an avisynth (.avs) file to load up which has your filtering and resizing options all ready and waiting.

Now you should also have exported a WAV file of your AMV's audio. In VirtualDub, go to the Audio menu, and select Audio from other file... and in the dialog box that pops up, select your WAV file.

Next, go back into the Audio menu and select Full Processing Mode. Finally, go into the Audio menu one more time, and select Compression... and the following window will pop up:

VirtualDub audio compression settings

On the left side, you want to select Lame MP3. If you do not see this option, then you need to install the Lame ACM codec, which is included in AMVapp. Then, we need to select a setting on the right side. There is a bit of info to go through here though, so let's take it one step at a time.

The first thing you see listed is the sample rate, such as 44100hz. Audio from CDs should have a sample rate of 44100hz while audio from DVD will probably be 48000hz. You may notice that as you go further down the list you will see lower frequencies listed, like 32000hz. Make sure you DONT select one of these, because it will downsample the audio and make it sound bad!

Next, we see the bitrate. Basically, this is how much you want the audio to be compressed. Higher values sound better, but will result in your audio using up more filesize. I would recommend a bitrate of either 160 or 192, though some people go with 128.

The next thing listed is CBR or ABR. I would suggest you use the CBR option, because the AVI container was only designed to use CBR audio.

Lastly, you will see Stereo or Mono. Your source is most likely stereo, so you definitely want to choose stereo here (unless of course you used a mono source).

So to sum it up, if I ripped my audio from a CD, I would probably choose the option that says 44100 hz, 160kbits CBR, Stereo.

XviD Compression Options - What's all this about?

Now choose Video > Compression, scroll down and select Xvid MPEG-4 Encoder then click the button labelled "Configuration". You will be greeted with the following which will now be referred to as the main page.

Xvid Opening Configuration PageLet's look at each part individually so you can work out what it all does and what parts you need to know about.

Profile @ Level: This pulldown menu allows you to select MPEG-4 encoding levels. These levels are often specified by decoder devices and define what sort of mpeg-4 content they can play. AS means Advanced Simple and L5 is level 5. AS @ L5 is the setting we will use in this guide. You can choose "unrestricted profile" but your encodes are not guaranteed to be MPEG-4 compatible. The more... button will take us to the settings related to Quantizers and so on - we will come back to this later.

Encoding Type: You will want to select Twopass (1st Pass). The more... button next to this menu will contain different options depending on the type of encoding selected. Again, we will look at these later.

Target Quantizer: This area allows you to specify what quantizer you would like to aim for. "Target quantizer" is actually a button and if clicked it will switch to its alternative mode. In Single Pass this can be changed to Target Bitrate. In Twopass (First Pass) this button and the window will be greyed out. In Twopass (Second Pass) it will allow us to specify a Target Bitrate or a Target Size. The calc... button will open up the XviD bitrate calculator, which we will use in this guide later on.

Zones: This is a confusing area, but luckily the majority of you won't need to use it anyway. This area allows you to specify areas of your video to encode with different properties. The main option you can set is "weight" (think of it as a measure of importance in how good a section of the video needs to look). So, if you were compressing a movie, you could set the credits to have a really low "weight" so they don't take up too much space and are encoded at low quality. You can have many zones - they start at the frame specified and end at either the end of the file or the start of the next zone, whichever is first. By default a single zone is set starting at frame 0 with a Weight of 1. If you set a new zone with a Weight of 0.5 it would be, essentially, half the average quality, 'W 2' would be double and so on. You can set fixed quantizers too - they appear listed as Q 1 etc instead of W 1. Zones are only required if you want to make sure a certain section of the amv is encoded in a certain way (making one part smaller or more detailed as required). Zones are clever but only useful in certain situations - you can read about all the zone options later in the guide.

Quality Preset: Here are a few presets you can choose from. The main encoding options can be accessed by pressing the "more" button. For this guide, we will be using the (User Defined) preset.

Load Defaults: Resets all settings to their default configuration. You should always press this after installing a new version of xvid.

Other Options: This is fairly self explanatory. The options themselves will be discussed later.

Setting up XviD Encoding Options

With all these menus and sub-menus it is easy to be confused, but luckily many of these options will only need to be set once and the rest you will probably never use. In this guide, the settings will be chosen specifically for encoding AMVs. Live Action footage and full length features may benefit from different settings but the ones in this guide are reasonably universal - however, when testing new options starting with the defaults is always a good option.

The XviD encode this guide will recommend is going to be compatible with XviD, Divx 5 and ffdshow decoders on windows and mplayer 1.0rc3 and above on GNU/Linux (and miscellaneous other UNIX) systems. For hardware players your mileage will vary due to the benefits and limitations of various chipsets. You can probably find good info on the 'net as to which options work with your player.

Notes about Twopass Encoding

XviD allows you to encode in either one or two pass mode. Two pass mode has a huge advantage over one pass mode in terms of quality as it is able to distribute the bitrate sensibly from scene to scene to make sure that everything looks as good as it can at that bitrate. It can also help make sure your file doesn't get too big.

In two pass mode, you encode your video and whilst you encode it the codec records all sorts of data about how much information is contained in each part of the video. When it's done you select the "second pass" option in the codec configuration page and encode the same source file again, just like before but with 2nd pass options selected. The difference is that this time the codec knows how easy or hard each scene is to encode so tries its best to keep the quality high by distributing the bits depending on what a scene needs. Twopass is NOT about compressing a file once and then compressing the compressed file again - that is a really bad thing to do. It's just a method that processes the same source twice and the second time around the codec knows how to compress it well.

The First Pass

The first pass is used by a codec to find out how many bits are needed in each scene of your video. This procedure (the way we do it in this guide) will produce a file that is at the maximum quality the codec can do with these settings and this source footage. Most of the time, this file is a little too big to distribute so we then do a second pass and tell the codec to aim for a smaller filesize.

Let's start from the main page as shown in the picture above except this time Twopass (1st Pass) will be selected.

Now choose the more... button that is next to Profile @ Level. This opens the main options area of XviD which contains options that determine the compatibility of the file you are encoding by enabling or disabling advanced encoding options. These options are true for both One and Twopass encoding but here we are using Twopass. Note that for ease of reference, the images show all the preferred options recommended by the guide.

Profile Options
As before the Profile @ Level selected is AS @ L5 which is what we want. Different levels would change what options are available to you.

Underneath this we have the Quantization Type. After a vast amount of testing I have found that H.263 gives the best results for anime encoding pretty much every time. Under some circumstances a custom mpeg quantizer matrix can be used to good effect (such as the hvs-best matrix) but on the whole H.263 is the most reliable quality-wise for anime.

Adaptive Quantization compresses very dark and very light areas more because in theory your eyes are less sensitive to these, but you may find it makes things look blockier. It doesn't offer a large advantage - it's compatible so you can use it if you like but I don't really recommend it.

Interlaced Encoding is an option you will never need to use. If you have followed these guides then you should not have an interlaced video to compress, nor should you be compressing an interlaced video if you are going to distribute it on the Internet. This option encodes the footage in fields instead of frames to retain any interlacing. It does not remove interlacing, it is designed to keep it. This function is not always compatible with some players and is not recommended.

Quarter Pixel is a neat feature that isn't always helpful. It's designed to improve the accuracy of motion detection (it can calculate motion to 1/4 of a pixel in theoretical movement accuracy instead of just 1/2 like normal) and this can improve compression in some sources. However, storing information at this greater accuracy takes up more space straight away so you have a Catch 22 situation because it will only save bits if there is a lot of quarter pixel movement in the source. You cannot tell beforehand, it's all in the maths and so it will not help every encode which means you may end up with a bigger file with no benefit. It also takes much more processing power to decode (up to 60% more sometimes) so because of these issues it is not recommended for amv files for mass distribution.

Global Motion Compensation takes the idea of calculating motion and applying it to the whole image - this can save bits overall and is a good thing. Unfortunately, decoders aren't entirely up to speed with this so it is common for them to not know how to properly handle XviD's GMC. For compatibility reasons this option is not recommended.

B-VOPs are B-Frames, a feature that DivX 5 touted but in their early versions were pretty ugly. XviD's new B-Frames are pretty nice and very useful as I shall explain. You may recall from the theory guides that there are Intra-frames (I-Frames),  Predicate Frames (P Frames) and Bi-directially Interpolated Frames (B-Frames). B-Frames are used to save even more space by referring to data both in the last frame and one that is coming later in a sequence. They are incredibly useful in saving space as they can be compressed more while the loss in quality is less than or equal to the loss in bits which means that overall you have more bits to use which improves quality overall and everyone's a winner. You don't have to use B-Frames (particularly if you aren't interested in saving space and just want the best quality) but they are good for getting the best quality in high compression. It is recommended to keep this option checked unless size is really not a problem for your encoding.

Max consecutive BVOPs specifies the maximum amount of these B-Frames that can appear in a row. DivX 5 only encodes 1 B-Frame in a row. XviD's default is 2 which is the highest you should go (as each extra B-Frame increases the processing power needed to decode). However, there is a compatibility concern. DivX5 can decode 2 B-Frames in a row but not when Packed bitstream is selected. This guide recommends keeping 2 B-Frames and deselecting Packed bitstream but if you want to keep things really compatible with DivX 5 and some hardware players then you may want to reduce the max BVOPs to 1.

Quantizer ratio defines how much higher the B-Frame quantizers will be than the P-frames. Higher quantizers are lower quality but the B-Frame savings make them worthwhile. The default setting of 1.50 is fine and recommended.

Quantizer offset: adds this value to the B-Frame quantizer number calculation. Keep this at the default of 1.00.

Packed bitstream is a function established by DivX 5.1 which in theory allows better decoding and reduces bad audio sync. This is true until you start using more than 1 B-Frame. If you use 2 B-Frames this should be deselected in order to ensure that people will be able to watch this encode correctly using the DivX 5 decoder.


These are the main options that you will need to set when making anime encodes and will work perfectly fine for live action encodes too. These options will also make sure that your encode can be easily played by a variety of decoders including DivX 5 and ffdshow.

When you are confident that all of these options are set correctly, you may click OK to return to the main encoder options page.

You may be wondering what the other tabs were for. The Level tab gives you some stats on the MPEG-4 profile and level you have selected - these are theoretical values and won't have any effect on your encode. The Aspect Ratio tab is, presently, only useful if you are going to use a special container for the video (such as MPEG-4 or Matroska) as it allows you to set an option to tell a decoder to resize to a particular aspect ratio on decoding. This is designed to encode anamorphic footage in mpeg-4 containers. Your video should not use this option as it will be already resized to square pixels which is the most compatible method for decoding as the avi container is not really used for specifying aspect ratio and many players will ignore this setting.

Now that you are back on the main page, you should make sure that Twopass - 1st pass is selected in the Encoding Type pulldown menu.

Click the more... button that is next to Encoding Type and you will be greeted with a simple menu with a file location window and two options.

First Pass - Encoding Type optionsStats filename: The statistics that are recorded during your first pass are kept in a log file. You can, if you like, specify a particular file to save this to. By default it is saved in the directory of the file you are encoding, so if you encode something to C:\encode\amv.avi then the stats file will be C:\encode\video.pass - you don't need to change this option.

Full Quality First Pass:
This is an option that almost wasn't included until myself and several others asked for it to be put in. By default, XviD does a quick 1st pass that doesn't really create a very stable encode - it's just done to make the calculations for the second pass. You can (and should) select that the first pass creates a file that is the full quality of the encoding settings. The reason is twofold: 1) The first pass may be small enough to distribute and so you know you have the best quality encode you can get with these options and this source. 2) It's useful to know how big the maximum quality is to help you calculate how small you could make your file without losing too much quality. You should select this option - it will be a little slower but it's much more useful. However, if you know that the first pass is definitely going to be too big, disabling full quality first pass will be a little faster.

Discard First Pass: This sets XviD so that it doesn't really save any video data to the file during first pass. Again, we actually want to keep the first pass in case it is good enough. Uncheck this option.

You can now press OK to return to the main page.

We are almost done with our settings (I know there's a lot of them but you get used to it after an encode or two). You won't need to specify Zones - if you think you do then you probably already know what they are used for. 99% of the time you won't need them from amv encoding. The only thing I will mention is that in the Zone options you can set a Zone to use a Chroma Optimizer which is fabled to help blocking in dark areas and on things like walls in anime. You should probably only select this if you are trying to get rid of a problem you have noticed with an encode you made already.

The next thing we need to do is make sure to set the Quality Preset to (User Defined), and then click on the "more" button to the right of it and you should see something like this:

XviD - Advanced SettingsThe first tab is the Motion tab which allows us to alter how xvid calculated things that are moving in the video.

Motion Search Precision: This tells XviD how hard it should look to see if parts of the video are moving around. You will want to keep this set at 6 for maximum quality.

VHQ mode: This goes into even greater detail when analysing motion. Higher values will increase quality at the cost of speed. You should set this to 4 - Wide Search.

Use VHQ for bframes too: This uses the extra motion analysis for bframes as well. You should enable this.

Use Chroma Motion: This is yet another motion searching option that will also take into account the chroma information when deciding how things have moved. This is another recommended setting.

Turbo ;-) This ditches certain mathematical functions to speed up the encode. Not advised, we are in no hurry.

Frame Drop Ratio: You do not want to change this. Unless you really really know why you should, you don't want to change this value. Keep it at 0.

Maximum I-Frame Interval: This sets how far apart keyframes are allowed to be. The default value of 300 is fine.

Once you have set up the options as show above click along to the next tab, Quantization.

Advanced Options - Quantization
This menu allows you to select what range of Quantizers are to be used in your XviD encodes. The default settings have all Minimum quantizers set to 1 and all maximum set to 31 - this is the full range. A Quantizer used by a frame will usually dictate the quality, with 1 being best and 31 being worst.

The old XviD used to have the default minimum set at 2 and this is the option I suggest. Although quantizer 1 is the mathematical highest quality, it is not vastly better quality *visually* than quantizer 2... but it is MUCH bigger in bytes.

As XviD is used mostly for Twopass encoding, quantizer 1 was added to defaults to allow people to fill up all the bits when fitting in a certain size such as a CD. For amvs this isn't really required as every bit saved by using quantizer 2 instead of 1 is precious - especially if you host your own videos.

These settings will also mean that our full quality first pass will be constant quantizer 2 just like the old XviD.

Trellis quantization uses a neat bit of mathematics that simplifies certain parts of the Discrete Cosine Transform and gives bits to others etc etc... in practice this settings pretty much always increases quality, so it's worth selecting.

The final thing we need to do is go into the Other Options.

The first tab in these Other Options is the Encoder tab which looks like this:

Advance Settings - Debug

FourCC used: This selects what Four Character Code is put on the avi. This code tells whatever program you use what codec it needs to play it. The default is XVID but as our encode is DIVX5 compatible you can select DX50 so it works on machines that do not have an XVID decoder installed. DIVX works too but people with DivX 4 installed will probably have trouble playing the files.

Number of threads is for you lucky multi-processor people.

Print debug info on each frame: This will dump all sorts of programming info on your video, which you probably don't want.

Display encoding status: XviD has a new encoder window that gives all sorts of clever info about what has been encoded so far. If you don't like it, you can turn it off here.

Next is the Decoder tab. This allows you to specify how the XviD decoder will playback XviD files... this does not change any of the actual encoding process, it is just for the player on your machine. We won't be needing to change anything in that tab.

Finally we have the Common tab, which you also shouldnt need to change anything on. Firstly on this tab there is the CPU optimizations which are automatically detected. You can choose Force optimizations if you really want, but ones that your chipset can't do will only cause crashes, and removing optimizations is only useful if you are getting some odd instability.

OutputDebugString debug level: Do you know what this means? If you do I have no more to tell you, if you don't then don't worry about it. Leave it as it is.

All done. Press OK to go back to the main page.

That's all the settings you need for your First Pass. Now it's time to make the encode.

Saving the First Pass

Once you have all of your XviD encoder settings ready, it's time to save the first pass.

This part of the guide is going to focus purely on the video part of your encode - you can attach the audio later by following the audio guide. When you've got the hang of the software it's easy to do these at the same time.

With your file loaded into VirtualDub, go to the Video menu and choose fast recompress. This will mean that you will not be able to use any internal VirtualDub filters but all of your filtering should be already done in avisynth. Fast recompress will make sure that no unnecessary color conversions are made before encoding.

Now you can go to the File Menu and choose 'Save As...'

Enter the title of your video and keep it memorable so you know it's the first pass encode, then click Save.


Checking out the First Pass Encode

At the end of the encode, you should load the avi into your favorite media player and see how it looks. If you've followed the guides diligently then everything should look great.

Other playback problems may occur if you don't have an up-to-date DivX decoder installed or your decoding settings are in some way messed up. This is usually not a problem with the encode.

OK, if you are sure it looks good then we can continue. What was the final size of the video? If the size is small enough for you to go ahead and distribute (40mb is the average size for a 3 or 4 min simple amv) then your encode is complete!

However, if your first pass encode is on the big side then you will need to do a second pass to reach a desired filesize. If your first pass is bigger than 120mb you should consider reducing the frame size of your video or doing heavier filtering. The question is, what should that filesize be?

Of course it is up to you, but if you want something to still look pretty decent then you should aim to have your final encode be around 70% of the first pass size. This is not always possible as some videos are hard to encode but don't be a quality whore about it; really you should never go above 30mb for each minute of video as that's getting excessive. You can also go much much smaller than 70% first pass if you like, but if you start getting major blocking then reducing the resolution and doing better filtering may well be a better option than just reducing the target size. Encoding well requires a lot of trial and error.

The Second Pass

If you have decided you need to do a second pass, go back to VirtualDub and open up the Video > Compressor... XviD Configuration Settings again. Do not... I repeat - Do Not load your first pass encode into VirtualDubMod - we are compressing the original source for a second time, not recompressing the one we just made.

On the XviD main page you can now select Encoder Type as Twopass (2nd Pass) and you will see the Target Bitrate box and window appear (probably at the default of 700). If you click Target Bitrate, you will see that it transforms into Target Size. It is size, not bitrate, that we are going to specify to make sure our final encode comes out small enough.

Calculating the Second Pass Bitrate

So, have you decided the size for your video? If you are using the 70% rule then all you have to do is take the number of the final video frame size from the XviD status window (as described earlier) and multiply by 0.7 and put this into the Target Filesize box on the XviD main page.

If, on the other hand, you want to reach a particular size (such as small enough to fit on your web host) or you want to make it even smaller then you are best using the XviD bitrate calculator, which has now appeared as a calc... button next to the Target Filesize box. Click on it to get this following:

XviD Bitrate CalcIn the Target size (kbytes) box, I have put a target of 40mb (to work it out in kbytes multiply by 1028).

There are no subtitles being muxed in this video, so that stays at 0.

Format will be AVI-OpenDML. There are options for other containers which have different header sizes, so select them if you need to.

In the Video section, put the total minutes and seconds of your video in the boxes and choose the framerate.

Finally in the Audio section you need to choose your audio options. If you follow the guide this will be MP3-CBR with a bitrate of 192 but if you choose to encode your mp3 using an external variable bitrate method you should choose MP3-VBR and then load the mp3 into the calculator by pressing the "..." button next to Size (kbytes). This will work out the total size of your audio stream.

Once all of this info is entered you can copy and past the Size (kbytes) in the grey box in the middle. This is your Target Filesize for your second pass.


Final Second Pass Settings

Go back to the main page and paste your Target Filesize into the window.

You are almost ready to encode your second pass - there's just one last set of options to choose. If you click the more... button next to Twopass (2nd Pass) you will find a menu that is different from the one in the 1st pass - it looks something like this:

XviD Second Pass OptionsAgain we have a Stats Filename box, in case you want to specify a specific file. This should be the same as the setting in 1st pass.

Intra-frames tuning is a section that you probably won't ever need to change. I-frame boost  shows how much more bits are given to keyframes. The default is 10% and this is fine for your needs.

The I-frames closer than part is to change how things such as consecutive or nearly consecutive keyframes are handled. Default settings has the bits used by a consecutive keyframe reduced by 20% - there's little reason to change these options.

Overflow treatment on the other hand, will have a noticeable effect on your encode. The default settings for control strength, improvement and degradation are all 5%. These are good settings when encoding full length movies. The Overflow control strength dictates how agressively the codec should force scenes to have higher or lower bitrates - with higher values getting more accurate file size but not as steady quantizers.

Although the default is 5, amvs (being usually only 4 mins long) need a slightly more aggressive algorithm in order to have enough overflow control to make a difference. After much testing, it was found that for amvs it is better to have an Overflow Control Strength of 20%. Keeping the default of 5 is perfectly fine too, it's just that 20 came up best in my tests.

Max overflow improvement and Max overflow degradation tell the codec how much to add or take away if an area is over or undersized respectively. It's incredibly rare that you will want to change these settings - in tests having these much higher made a really uneven bitrate distribution and ended up with most the video looking awful.

Curve compression will adjust the default bitrate curve that XviD will decide as the best to encode the source in its second pass. It is unlikely that you will want to change these as the XviD curve compression has really been optimised to the max. If, however, you think that the curve compression is giving too many bits to either high or low bitrate scenes then you can reduce that by setting either the High bitrate degradation or the Low bitrate degradation.

Once these options are set and finalised you are ready to do your second encode. Chose 'Save As...' just as before and encode your new 2nd pass encode. You should see an XviD window that will have the blue and green bars spread across a greater range of quantizers.

XviD Status Window - 2nd PassFor a good looking encode you'd expect the blue to be massing mostly between quantizers 2 and 4 and the green to be mostly between 4 and 8. A wide range of quantizers is fine but large amounts of frames with low quantizer will always look awful. The closer the average P-Frame quantizer is to 2 and the average B-Frame quantizer is to 4 the better.

When it's finished, it should look great. If you realise that you are really compressing too much for your desired filesize then you either have to increase it, reduce the resolution of your encode or do some more smoothing on your source file.


I hope this compression guide has been useful. If you haven any questions that aren't covered here, please ask in the Video Software Forum.