AfterDawn.com

Opening Video and Audio Files


The first thing you need to know how to do to use AviSynth is open one or more files. Going back to our script in the previous section, there are three basic ways to open your video and audio. You can open a single file containing both (or only one). You can also get the streams from different files and combine them using the AudioDub filter.




AviSource


AVISource (string filename, [bool "audio" = true, string "pixel_type" = "YV12",
string "fourCC"])

ArgumentTypeDefaultReqDescription
filename string*Path and name of file open
"audio"BooleanTrueInclude audio from source in clip
pixel_typestringYV12Colorspace of source video. Valid values are YV12, YUY2, RGB32, and RGB24
"fourCC"stringfourCC to use when selecting video decoder. Valid values are any fourCC with the correct codec installed


Since AviSynth was designed around VfW it can open AVI files using nothing but internal filters. Typically opening AVI files requires that a codec of some kind be installed. Common codecs for AVI files are MPEG-4 ASP (DivX, XviD), DV, AVC, and HuffYUV. In addition to AVI files, AviSource can be used to open WAV audio or VirtualDub Frameserver (VDR) files.

Example Code

Open an AVI:
AviSource("D:\Folder\Filename.avi")

Open an AVI using the XviD codec:
AviSource("D:\Folder\Filename.avi", FourCC = "XVID")

Open an AVI but only use the video:
AviSource("D:\Folder\Filename.avi", audio = False)

Open an AviSynth script as an AVI file:
AviSource("D\Folder\Filename.avs")

Open a WAV audio file:
AviSource("D:\Folder\Filename.wav")

When To Use It


As the name implies, AviSource is primarily used to open AVI files, although AVS files (AviSynth scripts) can be opened with it, as can WAV audio files, which normally contain uncompressed audio.

Related Software

In order to decode video in an AVI file you must have an appropriate VfW codec installed. Common codecs include DivX and XviD for MPEG-4 ASP (MPEG-4 Part 4) video. Other popular codecs include the free HuffYUV for lossless compression and Cedocida for DV from digital camcorders.




MPEG2Source


 MPEG2Source(str "d2v", [int "idct", int "info"])

ArgumentTypeDefaultReqDescription
d2vstring*Path and name of D2V project file
"idct"integer0Algorithm to use when decoding MPEG video
"info"integer0Give additional for display over the video, writing to a file, or passing to other filters later in the script

p>Mpeg2Source is a filter designed to work with DGIndex to read MPEG-2 files like DVD-Video or HDTV Transport Streams. Mpeg2Source uses a project file created by DGIndex to deliver video only. Although most modern computers have the necessary DirectShow filters to open these files using DirectShowSource, it's recommended to use DGIndex/Mpeg2Source whenever possible. In addition to reading MPEG-2 files, MPEG2Source can supply other filters with information, as can the D2V files themselves.

The algorithm used to decode MPEG-2 frames is a function of your computer's CPU, and the amount of accuracy required vs. decoding speed. This algorithm, "idct", can be set between 0 and 7. Setting it to 0 forces the value specified in the D2V file by DGIndex. Values between 1 and 3 specify CPU instructions to use. Not all instructions are available on all CPUs. All processors from the Pentium II (actually Pentium with MMX) on for Intel, or K62 for AMD support MMX instructions. All Intel CPUs starting with the Pentium III and AMD processors from the AthlonXP forward support SSE. SSE2 is available on Pentium 4 Intel processors or any 64 bit AMD CPU. SSE2 is faster than SSE, which is faster than MMX.

A setting of 5 will give you the most accurate results because it's taken directly from the MPEG-2 specification. Since algorithms with slightly less precision are allowed, all the others make minor quality sacrifices to achieve faster decoding speed. It's usually best to set this in DGIndex and keep the same setting here. Be aware that SSEMMX (Skal) is faster than the other options, but has slightly more error than the MPEG-2 specs allow.
  • 1 - 32-bit MMX
  • 2 - 32-bit SSEMMX
  • 3 - 32-bit SSE2MMX
  • 4 - 64-bit Floating Point
  • 5 - 64-bit IEEE-1180 Reference
  • 6 - 32-bit SSEMMX (Skal)
  • 7 - 32-bit Simple MMX (XviD)


By using MPEG2Source's "info" option you can get additional information about the video from MPEG2Source. Setting it to 1 will give you information from DGIndex regarding the source MPEG-2 file. This information includes the name and location of the file, as well as extensive information on each frame.

An "info" setting of 3 will pass hints to the next filter in the script for use by another filter called ColorMatrix. These hints are wiped out by most filters, so it's generally advisable to directly follow MPEG2Source with ColorMatrix to use hints.

Example Code

Open a D2V project:
Mpeg2Source("D:\Folder\Filename.d2v")

Open a D2V project using the IEEE-1180 reference iDCT algorigthm:
Mpeg2Source("D:\Folder\Filename.d2v", idct = 5)

Open a D2V project and provide hints for ColorMatrix:
Mpeg2Source("D:\Folder\Filename.d2v", info = 3)

Open a D2V project and provide clip and frame information similar to DGIndex:
Mpeg2Source("D:\Folder\Filename.d2v", info = 1)

When To Use It

Frameserving to AviSynth via D2V project is generally accepted as the best method for reading MPEG-2 files. If you have a suitable DirectShow decoder installed you can also open them that way. One advantage to a D2V project is that it allows you to treat multiple files that need to be seamlessly joined (like DVD VOB files) as a single input file.

Related Software

Mpeg2Source isn't capable of reading video files directly. It requires another program called DGIndex to create the D2V projects. You can find more information on obtaining and using DGIndex in our guide titledUsing DGIndex.




DirectShowSource


DirectShowSource (string filename, float "fps", bool "seek", bool "audio",
bool "video", bool "convertfps", bool "seekzero",
int "timeout", string "pixel_type")

ArgumentTypeDefaultReqDescription
filenamestring*Path and name of file open
"fps"floatThe video framerate of the source
"seek"BooleanTrueAllow seeking
"audio"BooleanTrueInclude source audio in clip
"video"BooleanTrueInclude source video in clip
"convertfps"BooleanFalseConvert VFR (Variable Framerate) video to CFR (Constant Framerate) by duplicating or skipping frames to achieve the value of fps
"seekzero"BooleanFalseEnforces restriction to seek only to first frame
"pixel_type"stringYV12Colorspace of source video. Valid values are YV12, YUY2, RGB32, and RGB24


DirectShow is the DirectX framework that replaced VfW as Windows primary multimedia architecture. Programs for playback of most modern video formats, including MPEG-1, MPEG-2, MPEG-4 (ASP and AVC), and VC-1 is implemented in commercial software using DirectShow filters. Sometimes even VfW files are opened through the DirectShow interface. Using the DirectShowSource filter it's possible to tap into that interface to open files. Opening a file with DirectShowSource requires you to first have whatever filters are necessary installed.
Since DirectShow filters aren't required to provide all the information AviSynth may need to decode them properly, sometimes you must include additional information that wouldn't be necessary with an AVI file. The most common optional argument to use is fps. This ensures playback at the correct framerate, which some DirectShow filters don't reveal. It's always recommended to include a value for "fps" when opening a file with DirectShowSource.

If you have problems with seeking forward or backward in a file you may want to try setting "seekzero" to True. If seeking is still broken it's best to set "seek" to False.

Audio or video streams in the source can be enabled (included in the clip returned) or disabled, leaving a clip with only audio or video. This is controlled by the values for "video" and "audio", which both default to True.

"pixel_type" works similarly to the same option in AviSource. In this case, however, if AviSynth asks for a colorspace the DirectShow filter used to decode the video can't supply, the default (DirectX) code converts it to the requested colorspace. Since this code produces results inferior to AviSynth's own internal filters, make sure to specify this value for any non-YV12 video.

Example Code

Open an MP4 file with film source encoded at 23.976fps:
DirectShowSource("D:\Folder\Filename.mp4", fps = 23.976)

Open an MP4 file with PAL video, keeping just the video:
DirectShowSource("D:\Folder\Filename.mp4", fps = 25, audio = False)

Open a NTSC DV encoded AVI using a DirectShow DV decoder:
DirectShowSource("D:\Folder\Filename.avi", fps = 29.97)

Open a WMV file containing film source encoded at 24fps:
DirectShowSource("D:\Folder\Filename.wmv", fps = 24)

When To Use It

In some cases the only way to open a file is using a DirectShow filter. Although AviSynth is a VfW oriented application, it can use DirectShow filters to open source files. Although some DirectShow filters are specifically designed to only work with a particular application, most can be used by AviSynth to read audio and video.

Related Software

In order to open a file with DirectShowSource you must have the appropriate DirectShow decoder installed. Sometimes these are commercial decoders, but there are also free DirectShow decoders, most notably ffdshow. Since Windows has the internal capability to read streams from the AVI and MPEG containers (.AVI and .MPG/.MPEG files) only a decoder is required for them. Newer formats like MP4 (for MPEG-4 video and audio) require additional stream splitting software like the Haali Media Splitter in order to read the video and audio streams muxed in them.
Written by: Rich Fiscus