Video Capture, Transcoding, and Authoring/Converting AVI to MPEG-2 with AviSynth and TMPGEnc Xpress

This is a how to guide for converting (transcoding) Windows AVI video files into high-quality DVD-compliant MPEG-2/AC3]format with AviSynth and TMPGEnc Xpress. Resulting MPEG-2/AC3 format files can be readily authored]and "burned" to DVD recordable. Founded by John Navas.
 * TMPGEnc Xpress can be a very good choice for transcoding, because:
 * It produces high quality MPEG-2 video output (e.g., smoother than NeroVision Express with 23.97 fps]video).
 * TMPGEnc Xpress supports high-quality AC3 audio encoding (2-channel as of this writing) as an optional plug-in for version 3, built-in to version 4.
 * AC3 (Dolby Digital) is a more compact and efficient audio format than the alternatives of Linear PCM (WAV) or MPEG-1 Audio Layer 2 (MP2) format (leaving more room for higher-quality video at a given file size).
 * Transcoding of audio into AC3 with other MPEG-2 encoders usually involves demultiplexing, separate transcoding, and remultiplexing, which can be a painful and problematic process.
 * However, TMPGEnc Xpress alone can be problematic:
 * It chokes on certain input formats; e.g.,
 * It incorrectly detects XviD video at 23.97 fps (and at 29.97 fps) as 30 fps, resulting in transcoding problems.
 * Its filters aren't as plentiful, powerful, good, and flexible as those for AviSynth and VirtualDub.
 * AviSynth frameserving of source material, with optional assistance from VirtualDub, can overcome these problems.
 * Note: TMPGEnc Xpress is a relatively slow encoder.

Essential

 * AviSynth (free) : Use version 2.5 (or higher).
 * AVSEdit (free) : Edit and preview AviSynth scripts.
 * TMPGEnc Xpress (commercial)
 * Plug-in AC-3 (commercial)

Codecs

 * Which codecs you need (in addition to those bundled with Windows) to have installed depend on which codecs were used to encode the AVI file you need to convert (transcode). (See Codec Utilities below)
 * XviD Free, excellent codec for a number of MPEG-4 formats (e.g., XviD, DivX) that are commonly used to compress video. Can be downloaded in ready-to-use install packages from:
 * Koepi's Media Development (Recommended)
 * Nic's XviD Binaries

Optional

 * Codec Utilities : Useful for detecting which codecs were used to encode a given AVI file and if compatible codecs are installed on your system.
 * AVIcodec (free)
 * GSpot Codec Information Appliance (free)
 * AviSynth Filters : A wealth of powerful tools.
 * Media Player Classic (free) : A good way to test your AviSynth script.
 * Nero (commercial)
 * NeroVision Express (DVD authoring)
 * Nero Express (DVD burning)
 * TMPGEnc DVD Author (commercial) : Commercial alternative to Nero.
 * VirtualDub (free) : Powerful tool that can be used to manipulate and modify AVI files
 * VirtualDub Filters : A wealth of powerful tools.
 * decompress : Part of avi2vcd; decompresses audio into a resulting AVI file that can then be processed by other tools which can't otherwise handle the audio.
 * VLC media player : In addition to playback, can be used to extract audio and/or video files.

What Is Frameserving and Why Use It?

 * Frameserving is using one program (the server) to decode (read) a video file and then sending it (serving) to another program to encode (write) a new video file. The program doing the 'serving' will typically generate a small (usually only one or two frames) dummy .avi file which is then loaded as the video input of the encoding program. The server sends the decoded frames one at a time to the dummy .avi file, essentially making the encoder think it's working with the original video file.


 * For example, we could decode a DVD Vob file using Flask Mpeg and then use AVISynth to 'frameserve' it to another utility such as TMPGEnc Xpress and convert it to an SVCD or a VCD. Or we could frameserve it to VirtualDub and add filters and effects before we encoded it into another format such as DivX. Frameserving is the holy grail of video converting because it allows the user to choose what encoder to use without the need to convert their video file into a huge uncompressed AVI format first!

Why Not Use VirtualDub Alone?

 * GUI-based VirtualDub is easier to use than script-based AviSynth, but normally isn't able to frameserve directly to TMPGEnc Xpress -- although TMPGEnc Xpress previewing works, TMPGEnc Xpress transcoding (conversion) fails with an error (although there may be a way to solve that problem - see this forum thread, particularly posts by bunyip).


 * On the other hand, VirtualDub is able to frameserve to AviSynth, which can in turn frameserve to TMPGEnc Xpress, so it is possible to use VirtualDub with TMPGEnc Xpress thus:

Cleanup of Noisy Video

 * VirtualDub can be particularly useful for cleanup of noisy source video files (e.g., VHS video). Two VirtualDub filters that are both easy to use and effective on cleaning up noise (when used in this order) are:


 * Dynamic Noise Reduction by Steven Don (MMX version) Try the Noise Threshold as high as 24 (or more) on really noisy sources.
 * Smart Smoother High Quality by Klaus Post Try a Threshold of 100-140 on really noisy sources.

Basics

 * Sample AviSynth script:

file = "SampleXviD.avi" # use actual filename AVISource(file, audio = false) frate = Framerate DirectShowSource(file, fps = frate)
 * 1) source XviD AVI file
 * 1) source XviD AVI file
 * 1) open AVI video-only to get correct frame rate
 * 1) save correct frame rate for DirectShowSource
 * 1) reopen in DirectShow for better audio compatibility
 * }

Normalize audio

 * To ensure that audio volume isn't too low and/or to roughly match the audio volume of different clips, add the following to the Basics script:

Normalize(0.98)
 * }

Correct Audio Sync Errors

 * Audio can be out of sync with video if:


 * The audio starts too early or too late. To fix this (e.g., delay audio by 0.25 seconds):

DelayAudio(0.25) # float seconds; negative is earlier
 * }
 * The audio playback speed doesn't exactly match the frame rate of the video. To fix this (e.g., stretch audio by 0.01%):

TimeStretch(tempo = 100.015)
 * 1) less than 100.0 slows/stretches
 * 2) more than 100.0 speeds/shortens
 * 1) more than 100.0 speeds/shortens
 * }
 * Troubleshooting Audio Sync
 * If AviSynth complains about source audio compression, or if audio is difficult to sync, especially when source audio is in AC3 format, it may first be necessary to decompress (and possibly fix) source audio. Options:
 * VirtualDub, either frame serve to AviSynth (see Why Not Use VirtualDub Alone?) or write a new (proper and much larger) AVI file (File &rarr; Save as AVI) with direct stream copy of video and full processing mode of audio. Note that you must have an appropriate codec installed or VirutalDub will complain about not being able to decompress the audio.
 * Audio skew (audio early or late) can be adjusted (under Audio &rarr; Interleaving) if necessary.
 * Advanced filtering may be needed for more difficult audio problems.
 * decompress (part of avi2vcd). Simple and easy, but not as effective as VirtualDub.

Extracting WAV audio

 * To solve difficult audio sync or other audio problems, particularly with AC3 audio, it can be helpful to extract WAV audio from the AVI, which can then be multiplexed with the video using AviSynth or VirtualDub. Note that the WAV file will be relatively large due to decompression.


 * Extracting WAV audio with VLC media player


 * One way to extract WAV audio is with VLC media player, which does a good job of handling difficult audio streams. The downside to using VLC is that it only works in real time (i.e., slowly). Sample commandline:


 * Extracting WAV audio with VirtualDub


 * A faster but less universal way to extract WAV audio is with VirtualDub:
 * Load the AVI video (File &rarr; Open Video File)
 * Under the Audio Menu select Full Processing Mode (Audio &rarr; Full Processing Mode)
 * Under the File menu select Save WAV... (File &rarr; Save WAV...), and choose a name and location for the WAV file.
 * VirtualDub will pop up a status window as the audio track is rapidly extracted and converted to a WAV file. Note that you must have an appropriate codec installed or VirutalDub will complain about not being able to decompress the audio.

Adding Subtitles (Permanent)

 * Adding permanent subtitles to output MPEG-2 video can be done by merging the .AVI video with text subtitles from a separate file in .SRT,  .SSA, or other subtitle format. This can be done with the TextSub plugin (named VSFilter.dll) for AviSynth; e.g.,



LoadPlugin("C:\Program Files\VobSub\VSFilter.dll") # program location TextSub("subtitles.ssa") # merge subtitles
 * }


 * Notes:
 * Subtitle sync errors can be fixed with the included SubResync utility. (It may be necessary to separately install VobSub in the standalone VSFilter package for SubResync to work.) To modify on-screen formatting (e.g., font, text size, color) of subtitles, use SubResync to Edit... as desired.
 * More serious subtitle editing is best done with free Subtitle Workshop.
 * When  converting PAL to NTSC (as described below) with subtitles, place TextSub before AssumeFPS to ensure that subtitles stay in sync.

Convert PAL to NTSC

 * Any conversion of PAL framerate to NTSC framerate will inevitably result in loss of motion smoothness (jerkiness). However, smoothness can be preserved by playing 25 fps PAL video at the NTSC film framerate of 23.976 fps by means of AssumeFPS, slowing action by about 4%, a difference so small as to be unnoticeable, and using 3:2 pulldown to achieve an NTSC playback framerate of 29.97 fps. Use audio_sync true on AssumeFPS to keep the audio in sync. That will slightly change (lower) the pitch of the audio, but again the difference is so small as to be unnoticeable, although you can correct that with TimeStretch as shown in the following example:
 * Method 1

AssumeFPS(23.976, true)
 * 1) Play PAL 25 fps video at NTSC 23.976 fps WITH audio sync
 * 1) Play PAL 25 fps video at NTSC 23.976 fps WITH audio sync

TimeStretch(pitch = (100.0*25.0)/23.976)
 * 1) Restore correct pitch (OPTIONAL)
 * }
 * Method 2

AssumeFPS(23.976)
 * 1) Play PAL 25 fps video at NTSC 23.976 fps WITHOUT audio sync
 * 1) Play PAL 25 fps video at NTSC 23.976 fps WITHOUT audio sync

TimeStretch((100.0*23.976)/25.0)
 * 1) Restore audio sync (ESSENTIAL)
 * }

Transcode MPEG formats

 * To frameserve various MPEG formats with AviSynth, use the DGMPGDec package, which includes the DGDecode plugin for AviSynth. (Supported formats include elementary streams, program streams, VOBs, VCDs, SVCDs, PVA files, and transport streams.)


 * Before frameserving, run DGIndex (also included in the DGMPGDec package), and use File&rarr;Open and File&rarr;Save Project to extract audio to a file (.wav, .mpa, etc.) and create a .d2v video index file. Then load video and audio in AviSynth, and dub them together. For example, for AVSEQ01.DAT (VideoCD MPEG1 file), processed with DGIndex:

LoadPlugin("C:\Program Files\DGMPGDec\DGDecode.dll") # Location of DGMPGDec package video = MPEG2Source("AVSEQ01.d2v") # video index file from DGIndex audio = MPASource("AVSEQ01 T01 DELAY 21ms.mpa") # audio file extracted by DGIndex AudioDub(video,audio) # Dub video and audio together
 * }
 * Notes:
 * MPEG2Source is used even for MPEG1 formats.
 * For .wav audio, WAVSource would be used instead of MPASource. Other forms of audio must be handled with AviSynth plugins.
 * To convert PAL to NTSC, the above method can be used:

AssumeFPS(23.976, true) # play PAL at NTSC frame rate with audio sync
 * }

Testing

 * A good way to test an AviSynth script is to play it with Media Player Classic.

Troubleshooting

 * Video problems