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.
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!
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:
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.
# source XviD AVI file
file = "SampleXviD.avi" # use actual filename
# open AVI video-only to get correct frame rate
AVISource(file, audio = false)
# save correct frame rate for DirectShowSource
frate = Framerate()
# reopen in DirectShow for better audio compatibility
DirectShowSource(file, fps = frate)
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%):
# less than 100.0 slows/stretches
# more than 100.0 speeds/shortens
TimeStretch(tempo = 100.015)
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 → 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 → Interleaving) if necessary.
Advanced filtering may be needed for more difficult audio problems.
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:
A faster but less universal way to extract WAV audio is with VirtualDub:
Load the AVI video (File → Open Video File)
Under the Audio Menu select Full Processing Mode (Audio → Full Processing Mode)
Under the File menu select Save WAV... (File → 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 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
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.
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:
# Play PAL 25 fps video at NTSC 23.976 fps WITH audio sync
# Restore correct pitch (OPTIONAL)
TimeStretch(pitch = (100.0*25.0)/23.976)
# Play PAL 25 fps video at NTSC 23.976 fps WITHOUT audio sync
# Restore audio sync (ESSENTIAL)
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→Open and File→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
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.