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

From Wikibooks, open books for an open world
Jump to navigation Jump to search
[unreviewed revision][unreviewed revision]
Content deleted Content added
Jnavas2 (discuss | contribs)
m revert edit
Line 141: Line 141:
:To solve difficult audio sync or other problems, particularly with AC3 audio, it can be helpful to extract WAV audio from the AVI that can then be multiplexed with the video using AviSynth or VirtualDub. One way to do this is with '''VLC media player''', which does a good job of handling bad audio streams. (The downside of using VLC is that it only works in real time; ''i.e., ''is slow.) Sample commandline:
:To solve difficult audio sync or other problems, particularly with AC3 audio, it can be helpful to extract WAV audio from the AVI that can then be multiplexed with the video using AviSynth or VirtualDub. One way to do this is with '''VLC media player''', which does a good job of handling bad audio streams. (The downside of using VLC is that it only works in real time; ''i.e., ''is slow.) Sample commandline:
::<code>vlc ''INPUTFILE.AVI'' --aout=aout_file --audiofile-file=''OUTPUTFILE.WAV'' --novideo</code>
::<code>vlc ''INPUTFILE.AVI'' --aout=aout_file --audiofile-file=''OUTPUTFILE.WAV'' --novideo</code>

:Posted by Max
:A faster way to convert AVI audio streams to WAV format is to use Virtual Dub. Load the AVI video (File|Open Video File) then under the Audio Menu select Full Processing Mode (Audio|Full Processing Mode). Then under the File menu select Save WAV... (File|Save WAV...) and choose where to save the WAV file. VirtualDub will pop up a status window as the audio track is rapidly converted to a rather large WAV file. Note that you must have an appropriate codec installed or VirutalDub will complain about not being able to decompress the audio. Unfortunately the only way I've been able to automate this process is by using an [http://www.example.com AutoIt] script.


==== Adding Subtitles (Permanent) ====
==== Adding Subtitles (Permanent) ====

Revision as of 21:03, 21 December 2007

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.


Software Needed

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)

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.
AviSynth Filters
A wealth of powerful tools.
Media Player Classic (free)
A good way to test your AviSynth script.
Nero (commercial)
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.

Frameserving with AviSynth

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 isn't able to frameserve directly to TMPGEnc Xpress -- although TMPGEnc Xpress previewing works, TMPGEnc Xpress transcoding (conversion) fails with an error ( but it seems that there is a way how to solve that problem - read this forum thread ). On the other hand, VirtualDub is able to frameserve to AviSynth, which can in turn frameserve into TMPGEnc Xpress, so it is possible to use VirtualDub with TMPGEnc Xpress thusly:
 AVI 
 VirtualDub 
 AviSynth 
 TMPGEnc Xpress 
 MPG 

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:
  1. Dynamic Noise Reduction by Steven Don (MMX version)
    Try the Noise Threshold as high as 24 (or more) on really noisy sources.
  2. Smart Smoother High Quality by Klaus Post
    Try a Threshold of 100-140 on really noisy sources.

Basics

Sample AviSynth script:
# 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)

Optional

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%):
# 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, it may first be necessary to decompress the source audio with decompress (part of avi2vcd). This alone can overcome certain audio sync problem.
  • In difficult cases of audio sync, especially when the source audio is in AC3 format, it may be necessary to use VirtualDub on the output of decompress either to frame serve to AviSynth (see Why Not Use VirtualDub Alone?) or write a new (proper) AVI file (File → Save as AVI) with direct stream copy of video.
    • Audio skew (audio early or late) can be adjusted (under Audio → Interleaving) if necessary even in audio direct stream copy mode.
    • More difficult audio problems may require advanced filtering in full processing mode.

Extract WAV Audio

To solve difficult audio sync or other problems, particularly with AC3 audio, it can be helpful to extract WAV audio from the AVI that can then be multiplexed with the video using AviSynth or VirtualDub. One way to do this is with VLC media player, which does a good job of handling bad audio streams. (The downside of using VLC is that it only works in real time; i.e., is slow.) Sample commandline:
vlc INPUTFILE.AVI --aout=aout_file --audiofile-file=OUTPUTFILE.WAV --novideo
Posted by Max
A faster way to convert AVI audio streams to WAV format is to use Virtual Dub. Load the AVI video (File|Open Video File) then under the Audio Menu select Full Processing Mode (Audio|Full Processing Mode). Then under the File menu select Save WAV... (File|Save WAV...) and choose where to save the WAV file. VirtualDub will pop up a status window as the audio track is rapidly converted to a rather large WAV file. Note that you must have an appropriate codec installed or VirutalDub will complain about not being able to decompress the audio. Unfortunately the only way I've been able to automate this process is by using an AutoIt script.

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
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.
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
# Play PAL 25 fps video at NTSC 23.976 fps WITH audio sync
AssumeFPS(23.976, true)

# Restore correct pitch (OPTIONAL)
TimeStretch(pitch = (100.0*25.0)/23.976)
  • Method 2
# Play PAL 25 fps video at NTSC 23.976 fps WITHOUT audio sync
AssumeFPS(23.976)

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

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→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
Notes:
  1. MPEG2Source() is used even for MPEG1 formats.
  2. For .wav audio, WAVSource() would be used instead of MPASource(). Other forms of audio must be handled with AviSynth plugins.
  3. 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.

Transcoding with TMPGEnc Xpress

Clipboard

To do:


Troubleshooting

  • Video problems
Clipboard

To do:


Authoring and Burning to DVD recordable

Clipboard

To do:


Clipboard

To do:


See Also