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.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:
XviD is an mpeg-4 codec which is developed for educational use by programmers in their free time. The reason you are reading an XviD guide here instead of a DivX5 guide, WMV9 or RealPlayer guide etc. is as follows:
The other good thing about XviD is that although you have to download a new codec to encode your videos, people who have DivX 5 do not need to download a new codec to see them, provided you use the options in this guide :) Also, as it is mpeg4 compliant it can be later muxed into a true standards compatible MPEG4 file or played back on MPEG4 hardware players.
If you want to encode XviD avis with extra options, other
people will need either the XviD codec or FFDShow installed to watch
This guide is based upon the XviD 1.1.2. 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.
When you have VirtualDubMod 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 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.
Let'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.
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.
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.
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.
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.
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
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
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
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
Max consecutive BVOPs specifies the maximum
amount of these B-Frames that can appear in a row. DivX 5 only encodes
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
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.
Stats 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.
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:
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
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.
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:
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
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.
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 VirtualDubMod, 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...'
You will get something like this:
Now, it is possible in VirtualDubMod to save your encode in different containers. You can save it in an avi container, an ogg media container or a Matroska container.
Although the other two have all sorts of cool things about them, we will be saving our video in the good old fashioned AVI container. It's the one most compatible with what people have installed.
You will also notice that you can choose Fast Recompress here in case you forgot to earlier :)
Enter the title of your video and keep it memorable so you know it's the first pass encode, then click Save.
As you encode you will get two status windows pop up:
This is the VirtualDubMod status window that you should be familiar with if you've ever used VirtualDubMod.
The thing to keep an eye out for here is the projected video size - if by the end of the video this starts to go much higher than 120mb then you have a hard to compress source and it may not look that great after the second pass. If you haven't filtered it much then you probably should.
Also, you may not be encoding audio like I am here (actually it's not being compressed at all, but like I care - it was just for a test picture).
If you need to use your computer for other things while encoding, setting the Processing thread priority to idle will only use spare cpu cycles.
The next window is the XviD status window in all it's glory:
This window contains all sorts of lovely facts and figures. As this is a first pass everything is fairly predictable: I-frames are at quantizer 2, B-Frames are at quantizer 4 and so on.
Colored bars show how much of each quantizer is being used - as you can see all 25 keyframes (red) are quantizer 2 as are all the 280 P-Frames (blue) and the 598 B-Frames (green) are quantizer 4.
This will become much more interesting in the second pass when you can see the distribution of the quantizers during the encode.
Again, you have the average bitrate and also the total size in kbytes (in the bottom right box - 9415 on this image). This number is useful to know as it will tell you how big the video stream is once the encode is finished. This can help you decide on a final target size.
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 (between 70 and 120mb) 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 trail and error.
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:
In 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. If you are encoding a 24fps source you will have to choose 23.976 as there is no 24fps option. It's close enough.
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:
Again 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.
For 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.