DirectShowSource

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(mention how to use it with live capture)
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:FuncDef|DirectShowSource(string ''filename'' [, float ''fps'', bool ''seek'', bool ''audio'', bool ''video'', bool ''convertfps'', bool ''seekzero'', int ''timeout'', string ''pixel_type'', int ''framecount'', string ''logfile'', int ''logmask''])}}
+
<div style="max-width:62em" >
  
== Description ==
+
<div {{BlueBox2|40|0|3px solid purple}} >
 +
{{AvsPlusFullname}}<br>
 +
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/directshowsource.html https://avisynthplus.readthedocs.io]
 +
</div>
  
'''filename''': DirectShowSource reads the file ''filename'' using MS DirectShow, the same multimedia playback system which Windows Media Player uses. It can read most formats which Media Player can play, including MPEG, MP3, and some [[QuickTime]] files, as well as AVI files that [[AviSource|AVISource]] doesn't support (like DV type 1, or files using DirectShow-only codecs). Try reading AVI files with AVISource first, and if that doesn't work then try this filter instead. Since v2.53 there is also support for [[GraphEdit]] (grf) files.
+
 
 +
'''DirectShowSource''' reads media files using Microsoft [[Wikipedia:DirectShow|DirectShow]], the same multimedia playback system that WMP ''(Windows Media Player)'' uses. It can read most formats that WMP can, including MP4, MP3, most MOV (QuickTime) files, as well as AVI files that [[AviSource|AVISource]] doesn't support (like DV type 1, or files using DirectShow-only codecs). There is also support for [[GraphEdit]] (grf) files, including live capture.  
  
 
There are some caveats:  
 
There are some caveats:  
 +
* Some decoders (notably MS MPEG-4) will produce upside-down video. You'll have to use [[Flip|FlipVertical]].
 +
* DirectShow video decoders are not required to support frame-accurate seeking. In most cases seeking will work, but on some it might not.
 +
* DirectShow video decoders are not even required to tell you the frame rate of the incoming video. Most do, but the ASF decoder doesn't. You have to specify the frame rate using the {{FuncArg|fps}} parameter, like this: <code>DirectShowSource("video.asf", fps=15).</code>
 +
* This version automatically detects the Microsoft DV codec and sets it to decode at full (instead of half) resolution. I guess this isn't a caveat. :-)
 +
* Also this version attempts to disable any decoder based deinterlacing.
  
# Some decoders (notably MS MPEG-4) will produce upside-down video. You'll have to use [[Flip|FlipVertical]].  
+
Try reading AVI files with [[AviSource]] first. For non-AVI files, try [[FFmpegSource]] or [[LSMASHSource]]. If that doesn't work then try this filter instead.
# DirectShow video decoders are not required to support frame-accurate seeking. In most cases seeking will work, but on some it might not.  
+
</div>
# DirectShow video decoders are not even required to tell you the frame rate of the incoming video. Most do, but the ASF decoder doesn't. You have to specify the frame rate using the fps parameter, like this: DirectShowSource("video.asf", fps=15).
+
# This version automatically detects the Microsoft DV codec and sets it to decode at full (instead of half) resolution. I guess this isn't a caveat. :-)
+
# Also this version attempts to disable any decoder based deinterlacing.
+
  
'''fps''': This is sometimes needed to specify the framerate of the video. If the framerate or the number of frames is incorrect (this can happen with asf or mov clips), use this option to force the correct framerate.  For live sources, this is like "max fps" that will be displayed.
 
  
'''seek''' = true (in v2.53): There is full seeking support (available on most file formats). If problems occur try enabling the '''seekzero''' option first, if seeking still cause problems completely disable seeking. With seeking disabled the audio stream returns silence and the video stream the last rendered frame when trying to seek backwards. Note the Avisynth cache may provide limited access to the previous few frames, beyond that the last frame rendered will be returned.
+
__TOC__
  
'''audio''' = true (in v2.53): There is audio support in DirectShowSource. DirectShowSource is able to open formats like WAV/DTS/AC3/MP3, provided you can play them in WMP for example (more exact: provided they are rendered correctly in graphedit). The channel ordening is the same as in the [http://www.cs.bath.ac.uk/~jpff/NOS-DREAM/researchdev/wave-ex/wave_ex.html wave-format-extensible format], because the input is always decompressed to WAV. For more information, see also [[GetChannel]]. AviSynth loads 8, 16, 24 and 32 bit int PCM samples, and float PCM format, and any number of channels.
 
  
'''video''' = true (in v2.52): When setting it to false, it lets you open the audio only.
+
== Syntax and Parameters ==
 +
<div style="max-width:62em" >
 +
{{Template:FuncDef
 +
|DirectShowSource(string ''filename'' [, float ''fps'', bool ''seek'', bool ''audio'', bool ''video'', <br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool ''convertfps'', bool ''seekzero'', int ''timeout'', string ''pixel_type'', int ''framecount'', string ''logfile'', int ''logmask'' ] )
 +
}}
  
'''convertfps''' = false (in v2.56): When setting it to true, it turns variable framerate video (vfr) into constant framerate video (cfr) by adding frames. This is useful when you want to open vfr video (for example mkv, rmvb, mp4, asf or wmv with hybrid video) in AviSynth. It is most useful when the fps parameter is set to the least common multiple of the component vfr rates, e.g. 120 or 119.880.
+
:{{Par2|filename|string|}}
 +
::The path of the source file; path can be omitted if the source file is in the same directory as the AviSynth script (*.avs).
  
'''seekzero''' = false (in v2.56): An option to restrict seeking only back to the beginning. It allows limited seeking with unindexed ASF. Seeking forwards is of course done the hard way (by reading all samples).
+
:{{Par2|fps|float|(auto)}}
 +
::Frames Per Second of the resulting clip. This is sometimes needed to specify the framerate. If the framerate or the number of frames is incorrect (this can happen with ASF or MOV clips for example), use this option to force the correct framerate. For live sources, this is like "max fps" that will be displayed.
  
'''timeout''' = 60000 (in milliseconds; 60000 ms = 1 min) (in v2.56): To set time to wait when DirectShow refuses to render. Positive values return blank frames and/or silence. Negative values cause a runtime AviSynth exception to be thrown.
+
:{{Par2|seek|bool|true}}
 +
::There is full seeking support available on most file formats. If problems occur, try setting {{FuncArg|seekzero}}=true first. If seeking still causes problems, disable seeking completely with {{FuncArg|seek}}=false. With seeking disabled and trying to seek backwards, the audio stream returns silence, and the video stream returns the most recently rendered frame. '''Note '''the Avisynth cache ''may'' provide limited access to the previous few frames, but beyond that the most recently frame rendered will be returned.  
  
'''pixel_type''' (in v2.56): The mnemonic for the videomedia subtype negotiated for the IPin connection. It can be "YV24", "YV16", "YV12", "I420", "YUY2", "AYUV", "Y41P", "Y411", "NV12", "ARGB", "RGB32", "RGB24", "YUV", "YUVex", "RGB", "AUTO" or "FULL". By default, upstream DirectShow filters are free to bid all of their supported media types in the order of their choice. A few DirectShow filters get this wrong. The '''pixel_type''' argument limits the acceptable video stream subformats for the IPin negotiation. Note the graph builder may add a format converter to satisfy your request, so make sure the codec in use can actually decode to your chosen format. The M$ format converter is just adequate. The "YUV" and "RGB" pseudo-types restrict the negotiation to all official supported YUV or RGB formats respectively. The "YUVex" also includes YV24, YV16, I420 and NV12 non-standard pixel types. The "AUTO" pseudo-type permits the negotiation to use all relevant official formats, YUV plus RGB. The "FULL" pseudo-type includes the non-standard pixel types in addition to those supported by "AUTO". The full order of preference is YV24, YV16, YV12, I420, NV12, YUY2, AYUV, Y41P, Y411, ARGB, RGB32, RGB24. Many DirectShow filters get this wrong, which is why it is not enabled by default. The option exists so you have enough control to encourage the maximum range of filters to serve your media. (See [http://forum.doom9.org/showthread.php?t=143321 discussion].)
+
:{{Par2|audio|bool|true}}
 +
::Enable audio on the resulting clip. The channel ordering is the same as in the [http://www.cs.bath.ac.uk/~jpff/NOS-DREAM/researchdev/wave-ex/wave_ex.html wave-format-extensible format], because the input is always decompressed to WAV. For more information, see also [[GetChannel]]. AviSynth loads 8, 16, 24 and 32 bit int PCM samples, and float PCM format, and any number of channels.
 +
 
 +
:{{Par2|video|bool|true}}
 +
::Enable video on the resulting clip.
 +
 
 +
:{{Par2|convertfps|bool|false}}
 +
::If true, it turns [[VFR]] (variable framerate) video into CFR (constant framerate) video by adding frames. This allows you to open VFR video in AviSynth. It is most useful when {{FuncArg|fps}} is set to the least common multiple of the component frame rates, e.g. 120 or 119.880.
 +
 
 +
:{{Par2|seekzero|bool|false}}
 +
::If true, restrict backwards seeking only to the beginning, and seeking forwards is done the hard way (by reading all samples). Limited backwards seeking is allowed with non-indexed [http://en.wikipedia.org/wiki/Advanced_Systems_Format ASF].{{Dubious|Talk:SegmentedAviSource}}
 +
 
 +
:{{Par2|timeout|int}}
 +
::For positive values DirectShowSource waits for up to {{FuncArg|timeout}} milliseconds for the DirectShow graph to start. {{FuncArg|timeout}} is clamped between [5000,300000] milliseconds. If the graph fails to start a compile time exception is thrown. Once the graph starts, each GetFrame/GetAudio call will wait for up to the timeout value and then return a grey frame or silence for the audio. No runtime exceptions are ever thrown because of time-outs.
 +
::For negative values DirectShowSource waits for up to 2000 milliseconds for the DirectShow graph to start. If the graph fails to start it is ignored at that point and the initial graph start wait is deferred until the first GetFrame/GetAudio call. If any GetFrame/GetAudio call experiences a timeout a runtime exception is then thrown.
 +
 
 +
:{{Par2|pixel_type|string|(auto)}}
 +
::Request a color format from the decompressor. Valid values are:
 +
::{|border="0"
 +
|[[YV24]],
 +
|[[YV16]],
 +
|[[YV12]],
 +
|[[I420]],
 +
|NV12,
 +
|[[YUY2]],
 +
|AYUV,
 +
|Y41P,
 +
|[[Y411]],
 +
|ARGB,
 +
|[[RGB32]],
 +
|[[RGB24]],
 +
|[[YUV]],
 +
|YUVex,
 +
|[[RGB]],
 +
|AUTO,
 +
|FULL
 +
|}
 +
<div {{NotaBeneWidthIndent|56|5|2px solid blue}} >
 +
By default, upstream DirectShow filters are free to bid all of their supported media types in the order of their choice. A few DirectShow filters get this wrong. The {{FuncArg|pixel_type}} argument limits the acceptable video stream subformats for the [http://en.wikipedia.org/wiki/DirectShow#Architecture IPin negotiation]. Note the graph builder may add a format converter to satisfy your request, so make sure the codec in use can actually decode to your chosen format. The MS format converter is just adequate. The "YUV" and "RGB" pseudo-types restrict the negotiation to all official supported YUV or RGB formats respectively. The "YUVex" also includes YV24, YV16, I420 and NV12 non-standard pixel types. The "AUTO" pseudo-type permits the negotiation to use all relevant official formats, YUV plus RGB. The "FULL" pseudo-type includes the non-standard pixel types in addition to those supported by "AUTO". The full order of preference is YV24, YV16, YV12, I420, NV12, YUY2, AYUV, Y41P, Y411, ARGB, RGB32, RGB24. Many DirectShow filters get this wrong, which is why it is not enabled by default. The option exists so you have enough control to encourage the maximum range of filters to serve your media. (See [http://forum.doom9.org/showthread.php?t=143321 discussion].)
  
The non-standard pixel types use the following GUID's respectively :- <br>
+
The non-standard pixel types use the following GUID's respectively :-  
MEDIASUBTYPE_I420 = {'I420', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}; <br>
+
<div style="font-family:Courier;font-size:85%;margin-left:1em;">
MEDIASUBTYPE_YV24 = {'42VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}; <br>
+
MEDIASUBTYPE_I420 = {'024I', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};<br>
MEDIASUBTYPE_YV16 = {'61VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}; <br>
+
MEDIASUBTYPE_YV24 = {'42VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};<br>
 +
MEDIASUBTYPE_YV16 = {'61VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};<br>
 
MEDIASUBTYPE_NV12 = {'21VN', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
 
MEDIASUBTYPE_NV12 = {'21VN', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
 +
</div></div>
  
Adding a '+' to the start of the pixel_type for planar formats assumes the lines are DWORD padded in memory instead of packed. See the Troubleshooting section below.
+
::In other words, if {{FuncArg|pixel_type}}="AUTO", it will try to output [[YV24]]; if that isn't possible it tries [[YV16]], and if that isn't possible it tries [[YV12]], etc...
  
'''framecount''' (in v2.57): This is sometimes needed to specify the framecount of the video. If the framerate or the number of frames is incorrect (this can happen with asf or mov clips), use this option to force the correct number of frames. If fps is also specified the length of the audio stream is also adjusted.  For live sources, specify a very large number.
+
::For [[Planar|planar]] color formats, adding a '''<tt>'+'</tt>''' prefix, ''e.g.'' <code>DirectShowSource(..., pixel_type="'''+'''YV12")</code>, tells AviSynth the video rows are DWORD aligned in memory instead of packed. '''This can fix skew or tearing of the decoded video''' when the width of the picture is not divisible by 4.
  
'''logfile''' (in v2.57): Use this option to specify the name of a debugging logfile.
+
:{{Par2|framecount|int|(auto)}}
 +
::Sometimes needed to specify the [[Clip_properties|frame count]] of the video. If the framerate or the number of frames is incorrect (this can happen with ASF or MOV clips for example), use this option to force the correct number of frames. If {{FuncArg|fps}} is also specified, the length of the audio stream is adjusted.  For live sources, specify a very large number.
  
'''logmask''' = 35 (in v2.57): When a logfile is specified, use this option to select which information is logged.
+
:{{Par2|logfile|string|}}
 +
:: Use this option to specify the name of a log file for debugging.
  
{| border="1"
+
:{{Par2|logmask|int|35|}}
|-
+
::When a {{FuncArg|logfile}} is specified, use this option to select which information is logged.
| Value
+
 
| Data
+
::{| class="wikitable"
 +
! Value
 +
! Data
 
|-
 
|-
 
| 1
 
| 1
Line 79: Line 135:
 
|}
 
|}
  
Add the values together of the data you need logged. Specify -1 to log everything. The default, 35, logs Format Negotiation, Received samples and Errors. i.e 1+2+32
+
::Add the values together of the data you need logged. Specify -1 to log everything.  
 +
::The default, 35, logs 1+2+32, or Format Negotiation, Received samples and Errors.
 +
</div>
 +
 
  
 
== Examples ==
 
== Examples ==
 
+
<div style="max-width:62em" >
Opens an avi with the first available RGB format (without audio):
+
Opens an AVI with the first available RGB format (without audio):
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  DirectShowSource("F:\TestStreams\xvid.avi",fps=25, audio=false, pixel_type="RGB")
 
  DirectShowSource("F:\TestStreams\xvid.avi",fps=25, audio=false, pixel_type="RGB")
</code>
+
</div>
  
Opens a DV clip with the MS DV decoder:
+
Opens a DV clip with the default DV decoder:
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  DirectShowSource("F:\DVCodecs\Analysis\Ced_dv.avi") # MS-DV
 
  DirectShowSource("F:\DVCodecs\Analysis\Ced_dv.avi") # MS-DV
</code>
+
</div>
  
Opens a variable framerate mkv as 119.88 by adding frames (ensuring sync):
+
Opens a variable framerate MKV as 119.88 by adding frames (ensuring sync):
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  DirectShowSource("F:\Guides\Hybrid\vfr_startrek.mkv", fps=119.88, convertfps=true)
 
  DirectShowSource("F:\Guides\Hybrid\vfr_startrek.mkv", fps=119.88, convertfps=true)
</code>
+
</div>
  
 
Opens a realmedia *rmvb clip:
 
Opens a realmedia *rmvb clip:
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  DirectShowSource("F:\test.rmvb", fps=24, convertfps=true)
 
  DirectShowSource("F:\test.rmvb", fps=24, convertfps=true)
</code>
+
</div>
 
+
Opens a GraphEdit file:
+
  
<code>
+
Opens a [[GraphEdit]] file:
 +
<div {{BoxWidthIndent|67|1}} >
 
  V=DirectShowSource("F:\vid_graph.grf", audio=False) # video only (audio renderer removed)
 
  V=DirectShowSource("F:\vid_graph.grf", audio=False) # video only (audio renderer removed)
 
  A=DirectShowSource("F:\aud_graph.grf", video=False) # audio only (video renderer removed)
 
  A=DirectShowSource("F:\aud_graph.grf", video=False) # audio only (video renderer removed)
 
  AudioDub(V, A)
 
  AudioDub(V, A)
</code>
+
</div>
 +
 
 +
See [[#Down-mixing_AC3_to_stereo|below]] for an audio example.
 +
</div>
  
See below for some audio examples.
 
  
 
== Troubleshooting video and audio problems ==
 
== Troubleshooting video and audio problems ==
 
+
<div style="max-width:62em" >
 
AviSynth will by default try to open only the media it can open without any problems. If one component cannot be opened it will simply not be added to the output. This will also mean that if there is a problem, you will not see the error. To get the error message to the missing component, use audio=false or video=false and disable the component that is actually working. This way AviSynth will print out the error message of the component that doesn't work.
 
AviSynth will by default try to open only the media it can open without any problems. If one component cannot be opened it will simply not be added to the output. This will also mean that if there is a problem, you will not see the error. To get the error message to the missing component, use audio=false or video=false and disable the component that is actually working. This way AviSynth will print out the error message of the component that doesn't work.
  
Line 128: Line 184:
  
 
Some DirectShow components incorrectly pad the lines of planar data to be DWORD aligned as is done for RGB24 DIB format. This is incorrect, but it is a fairly common mistake. By adding a '+' to the start of the pixel_type string you can inform DirectShowSource to treat planar data formats as padded DWORD aligned. This problem shows up when the width of the picture is not divisible by 4.
 
Some DirectShow components incorrectly pad the lines of planar data to be DWORD aligned as is done for RGB24 DIB format. This is incorrect, but it is a fairly common mistake. By adding a '+' to the start of the pixel_type string you can inform DirectShowSource to treat planar data formats as padded DWORD aligned. This problem shows up when the width of the picture is not divisible by 4.
 
+
<div {{BoxWidthIndent|56|1}} >
 
  DirectShowSource("NonMod4Video.mp4", pixel_type="+YV12") # Bad DWORD aligned planar
 
  DirectShowSource("NonMod4Video.mp4", pixel_type="+YV12") # Bad DWORD aligned planar
 +
</div>
  
=== The samplerate is wrong ===
+
=== The sample rate is wrong ===
  
Some filters might have problems reporting the right samplerate, and then correct this when the file is actually playing. Unfortunately there is no way for AviSynth to correct this once the file has been opened. Use [[AssumeSampleRate]] and set the correct samplerate to fix this problem.
+
Some filters might have problems reporting the right sample rate, and then correct this when the file is actually playing. Unfortunately there is no way for AviSynth to correct this once the file has been opened. Use [[AssumeSampleRate]] and set the correct sample rate to fix this problem.
  
 
=== My sound is choppy ===
 
=== My sound is choppy ===
  
Unfortunately Directshow is not required to support sample exact seeking. Open the sound another way, or demux your video file and serve it to AviSynth another way. Otherwise you can specify "seekzero = true" or "seek = false" as parameters or use the [[EnsureVBRMP3Sync]] filter to enforce linear access to the Directshow audio stream.
+
Unfortunately Directshow is not required to support sample exact seeking. Open the sound another way, or demux your video file and serve it to AviSynth another way. Otherwise you can specify {{FuncArg|seekzero}}=true or {{FuncArg|seek}}=false as parameters or use the [[EnsureVBRMP3Sync]] filter to enforce linear access to the DirectShow audio stream.
  
 
=== My sound is out of sync ===
 
=== My sound is out of sync ===
  
This can happen especially with WMV, apparently due to variable frame rate video being returned. Determine what the fps should be and set it explicitly, and also "ConvertFPS" to force it to remain constant. And [[EnsureVBRMP3Sync]] reduces problems with variable rate audio.
+
This can happen especially with WMV, apparently with variable frame rate video. Determine what the fps should be and set it explicitly, and use [[ConvertFPS]] to force it to a constant framerate. [[EnsureVBRMP3Sync]] reduces problems with variable rate audio.
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  DirectShowSource("video.wmv", fps=25, ConvertFPS=True)
 
  DirectShowSource("video.wmv", fps=25, ConvertFPS=True)
 
  EnsureVBRMP3Sync()  
 
  EnsureVBRMP3Sync()  
</code>
+
</div>
  
 
=== My ASF renders start fast and finish slow ===
 
=== My ASF renders start fast and finish slow ===
Line 152: Line 208:
 
Microsoft in their infinite wisdom chose to implement ASF stream timing in the ASF demuxer. As a result it is not possible to strip ASF format files any faster than realtime. This is most apparent when you first start to process the streams, usually after opening the AviSynth script it takes you a while to configure your video editor, all this time the muxer is accumulating credit time. When you then start to process your stream it races away at maximum speed until you catch up to realtime at which point it slows down to the realtime rate of the source material. This feature makes it impossible to use AviSynth to reclock 24fps ASF material up to 25fps for direct PAL playback.
 
Microsoft in their infinite wisdom chose to implement ASF stream timing in the ASF demuxer. As a result it is not possible to strip ASF format files any faster than realtime. This is most apparent when you first start to process the streams, usually after opening the AviSynth script it takes you a while to configure your video editor, all this time the muxer is accumulating credit time. When you then start to process your stream it races away at maximum speed until you catch up to realtime at which point it slows down to the realtime rate of the source material. This feature makes it impossible to use AviSynth to reclock 24fps ASF material up to 25fps for direct PAL playback.
  
 +
=== Windows7 users ===
  
== Common tasks ==
+
Windows 7 forces its own DirectShow filters for decoding several audio and video formats. Changing their merits or physically removing those filters doesn't help. '''clsid''' made the tool "[http://forum.doom9.org/showthread.php?t=146910 Win7DSFilterTweaker]" to change the preferred filters. However new decoders need to be added each time so it's not the perfect solution.
 +
</div>
  
 +
 +
== Common tasks ==
 +
<div style="max-width:62em" >
 
This section will describe various tasks that might not be 100% obvious. :)
 
This section will describe various tasks that might not be 100% obvious. :)
  
 
=== Opening GRF files ===
 
=== Opening GRF files ===
  
[[GraphEdit]] GRF-files are automatically detected by a .grf filename extension and directly loaded by DirectShowSource. For AviSynth to be able to connect to it, you must leave a pin open in GraphEdit of a media types that AviSynth is able to connect to. AviSynth will not attempt to disconnect any filters, so it is important that the output type is correct. DirectShowSource only accepts YV24, YV16, YV12, YUY2, AYUV, Y41P, Y411, NV12, ARGB, RGB32 and RGB24 video formats and 32, 24, 16 and 8 bit PCM and IEEE FLOAT audio formats.
+
[[GraphEdit]] GRF files are automatically detected by a .grf filename extension and directly loaded by DirectShowSource. For AviSynth to be able to connect to it, you must leave a pin open in GraphEdit of a media types that AviSynth is able to connect to. AviSynth will not attempt to disconnect any filters, so it is important that the output type is correct. DirectShowSource only accepts YV24, YV16, YV12, YUY2, AYUV, Y41P, Y411, NV12, ARGB, RGB32 and RGB24 video formats and 32, 24, 16 and 8 bit PCM and IEEE FLOAT audio formats.
  
A given GRF-file should only target one of an audio or video stream to avoid confusion when directshowsource attempts the connection to your open pin(s). From version 2.57 this single stream restriction is enforced.  
+
A given GRF file must only target one of an audio or video stream to avoid confusion when DirectShowSource attempts the connection to your open pin(s). This single stream restriction is enforced.  
  
=== Downmixing AC3 to stereo ===
+
=== Down-mixing AC3 to stereo ===
  
There are essentially two ways to do this. The first is to set the downmixing in the configuration of your AC3 decoder itself, and the second one is to use the external downmixer of "Trombettworks":
+
There are essentially two ways to do this. The first is to set the down-mixing in the configuration of your AC3 decoder itself, and the second one is to use the external down-mixer of "Trombettworks":
  
 
1) Install AC3filter.  
 
1) Install AC3filter.  
Line 186: Line 247:
  
 
Make the following script:
 
Make the following script:
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
  v = [[DGDecode/MPEG2Source|MPEG2Source]]("e:\movie.d2v")
  v = [[Mpeg2Source]]("e:\movie.d2v")
+
 
  a = DirectShowSource("e:\Temp\Test2\test.ac3")
 
  a = DirectShowSource("e:\Temp\Test2\test.ac3")
 
  [[AudioDub]](v,a)
 
  [[AudioDub]](v,a)
</code>
+
</div>
  
Finally, open the script in vdub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).
+
Finally, open the script in VirtualDub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).
  
 
2) Register the directshow filter [http://www.trombettworks.com/directshow.php Channel Downmixer by Trombettworks] (under start -> run):
 
2) Register the directshow filter [http://www.trombettworks.com/directshow.php Channel Downmixer by Trombettworks] (under start -> run):
Line 201: Line 261:
 
Open the AC3 file in WMP6.4 and select the file properties. Set the output of AC3Filter on '''3/2+SW 5.1 channels''' (this downmixer can't handle PCM Float, thus PCM 16 bit is selected here). In the properties of the downmixer, the number of input and output channels should be detected automatically. Check whether this is indeed correct.
 
Open the AC3 file in WMP6.4 and select the file properties. Set the output of AC3Filter on '''3/2+SW 5.1 channels''' (this downmixer can't handle PCM Float, thus PCM 16 bit is selected here). In the properties of the downmixer, the number of input and output channels should be detected automatically. Check whether this is indeed correct.
  
{| border=0 cellspacing=0 cellpadding=5
+
{| border=0 cellspacing=0 cellpadding=5
 
| [[Image:ac3downmix2a.jpg]]
 
| [[Image:ac3downmix2a.jpg]]
 
|-
 
|-
Line 210: Line 270:
  
 
Make the following script:
 
Make the following script:
 
+
<div {{BoxWidthIndent|56|1}} >
<code>
+
 
  v = Mpeg2Source("e:\movie.d2v")
 
  v = Mpeg2Source("e:\movie.d2v")
 
  a = DirectShowSource("e:\Temp\Test2\test.ac3")
 
  a = DirectShowSource("e:\Temp\Test2\test.ac3")
 
  AudioDub(v,a)
 
  AudioDub(v,a)
</code>
+
</div>
  
 
Finally, open the script in vdub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).
 
Finally, open the script in vdub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).
  
For some reason, I can't get this to work with DTS streams :(
+
For some reason, I {{Who}} can't get this to work with DTS streams :(
 +
</div>
  
== Windows7 users ==
 
 
Windows 7 forces its own DirectShow filters for decoding several audio and video formats. Changing their merits or physically removing those filters doesn't help. clsid made the tool "[http://forum.doom9.org/showthread.php?t=146910 Win7DSFilterTweaker]" to change the preferred filters. However new decoders need to be added each time so it's not the perfect solution.
 
  
 
== Changes ==
 
== Changes ==
 
+
<div style="max-width:62em" >
 
{| border=1 cellspacing=1 cellpadding=4
 
{| border=1 cellspacing=1 cellpadding=4
 
| v2.60
 
| v2.60
Line 252: Line 309:
 
| pixel_type specifies/restricts output video pixel format
 
| pixel_type specifies/restricts output video pixel format
 
|}
 
|}
 +
</div>
 +
  
 
== See also ==
 
== See also ==
 +
<div style="max-width:62em" >
 +
Haali media splitter also comes with an (unrelated) DirectShow input plugin [[External_filters#Source_Filters|DirectShowSource2]], aka [[DSS2]]
 +
</div>
  
Haali media splitter also comes with an (unrelated) directshow input plugin [[External_filters|DirectShowSource2]]
 
  
 
{{FilterCat|Internal filters|Media file filters|Source_filters}}
 
{{FilterCat|Internal filters|Media file filters|Source_filters}}

Latest revision as of 01:09, 17 July 2024

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


DirectShowSource reads media files using Microsoft DirectShow, the same multimedia playback system that WMP (Windows Media Player) uses. It can read most formats that WMP can, including MP4, MP3, most MOV (QuickTime) files, as well as AVI files that AVISource doesn't support (like DV type 1, or files using DirectShow-only codecs). There is also support for GraphEdit (grf) files, including live capture.

There are some caveats:

  • Some decoders (notably MS MPEG-4) will produce upside-down video. You'll have to use FlipVertical.
  • DirectShow video decoders are not required to support frame-accurate seeking. In most cases seeking will work, but on some it might not.
  • DirectShow video decoders are not even required to tell you the frame rate of the incoming video. Most do, but the ASF decoder doesn't. You have to specify the frame rate using the fps parameter, like this: DirectShowSource("video.asf", fps=15).
  • This version automatically detects the Microsoft DV codec and sets it to decode at full (instead of half) resolution. I guess this isn't a caveat. :-)
  • Also this version attempts to disable any decoder based deinterlacing.

Try reading AVI files with AviSource first. For non-AVI files, try FFmpegSource or LSMASHSource. If that doesn't work then try this filter instead.


Contents


[edit] Syntax and Parameters

DirectShowSource(string filename [, float fps, bool seek, bool audio, bool video,
      bool convertfps, bool seekzero, int timeout, string pixel_type, int framecount, string logfile, int logmask ] )

string  filename =
The path of the source file; path can be omitted if the source file is in the same directory as the AviSynth script (*.avs).
float  fps = (auto)
Frames Per Second of the resulting clip. This is sometimes needed to specify the framerate. If the framerate or the number of frames is incorrect (this can happen with ASF or MOV clips for example), use this option to force the correct framerate. For live sources, this is like "max fps" that will be displayed.
bool  seek = true
There is full seeking support available on most file formats. If problems occur, try setting seekzero=true first. If seeking still causes problems, disable seeking completely with seek=false. With seeking disabled and trying to seek backwards, the audio stream returns silence, and the video stream returns the most recently rendered frame. Note the Avisynth cache may provide limited access to the previous few frames, but beyond that the most recently frame rendered will be returned.
bool  audio = true
Enable audio on the resulting clip. The channel ordering is the same as in the wave-format-extensible format, because the input is always decompressed to WAV. For more information, see also GetChannel. AviSynth loads 8, 16, 24 and 32 bit int PCM samples, and float PCM format, and any number of channels.
bool  video = true
Enable video on the resulting clip.
bool  convertfps = false
If true, it turns VFR (variable framerate) video into CFR (constant framerate) video by adding frames. This allows you to open VFR video in AviSynth. It is most useful when fps is set to the least common multiple of the component frame rates, e.g. 120 or 119.880.
bool  seekzero = false
If true, restrict backwards seeking only to the beginning, and seeking forwards is done the hard way (by reading all samples). Limited backwards seeking is allowed with non-indexed ASF.[dubious – discuss]
int  timeout =
For positive values DirectShowSource waits for up to timeout milliseconds for the DirectShow graph to start. timeout is clamped between [5000,300000] milliseconds. If the graph fails to start a compile time exception is thrown. Once the graph starts, each GetFrame/GetAudio call will wait for up to the timeout value and then return a grey frame or silence for the audio. No runtime exceptions are ever thrown because of time-outs.
For negative values DirectShowSource waits for up to 2000 milliseconds for the DirectShow graph to start. If the graph fails to start it is ignored at that point and the initial graph start wait is deferred until the first GetFrame/GetAudio call. If any GetFrame/GetAudio call experiences a timeout a runtime exception is then thrown.
string  pixel_type = (auto)
Request a color format from the decompressor. Valid values are:
YV24, YV16, YV12, I420, NV12, YUY2, AYUV, Y41P, Y411, ARGB, RGB32, RGB24, YUV, YUVex, RGB, AUTO, FULL

By default, upstream DirectShow filters are free to bid all of their supported media types in the order of their choice. A few DirectShow filters get this wrong. The pixel_type argument limits the acceptable video stream subformats for the IPin negotiation. Note the graph builder may add a format converter to satisfy your request, so make sure the codec in use can actually decode to your chosen format. The MS format converter is just adequate. The "YUV" and "RGB" pseudo-types restrict the negotiation to all official supported YUV or RGB formats respectively. The "YUVex" also includes YV24, YV16, I420 and NV12 non-standard pixel types. The "AUTO" pseudo-type permits the negotiation to use all relevant official formats, YUV plus RGB. The "FULL" pseudo-type includes the non-standard pixel types in addition to those supported by "AUTO". The full order of preference is YV24, YV16, YV12, I420, NV12, YUY2, AYUV, Y41P, Y411, ARGB, RGB32, RGB24. Many DirectShow filters get this wrong, which is why it is not enabled by default. The option exists so you have enough control to encourage the maximum range of filters to serve your media. (See discussion.)

The non-standard pixel types use the following GUID's respectively :-

MEDIASUBTYPE_I420 = {'024I', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
MEDIASUBTYPE_YV24 = {'42VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
MEDIASUBTYPE_YV16 = {'61VY', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};
MEDIASUBTYPE_NV12 = {'21VN', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71};

In other words, if pixel_type="AUTO", it will try to output YV24; if that isn't possible it tries YV16, and if that isn't possible it tries YV12, etc...
For planar color formats, adding a '+' prefix, e.g. DirectShowSource(..., pixel_type="+YV12"), tells AviSynth the video rows are DWORD aligned in memory instead of packed. This can fix skew or tearing of the decoded video when the width of the picture is not divisible by 4.
int  framecount = (auto)
Sometimes needed to specify the frame count of the video. If the framerate or the number of frames is incorrect (this can happen with ASF or MOV clips for example), use this option to force the correct number of frames. If fps is also specified, the length of the audio stream is adjusted. For live sources, specify a very large number.
string  logfile =
Use this option to specify the name of a log file for debugging.
int  logmask = 35
When a logfile is specified, use this option to select which information is logged.
Value Data
1 Format Negotiation
2 Receive samples
4 GetFrame/GetAudio calls
8 Directshow callbacks
16 Requests to Directshow
32 Errors
64 COM object use count
128 New objects
256 Extra info
512 Wait events
Add the values together of the data you need logged. Specify -1 to log everything.
The default, 35, logs 1+2+32, or Format Negotiation, Received samples and Errors.


[edit] Examples

Opens an AVI with the first available RGB format (without audio):

DirectShowSource("F:\TestStreams\xvid.avi",fps=25, audio=false, pixel_type="RGB")

Opens a DV clip with the default DV decoder:

DirectShowSource("F:\DVCodecs\Analysis\Ced_dv.avi") # MS-DV

Opens a variable framerate MKV as 119.88 by adding frames (ensuring sync):

DirectShowSource("F:\Guides\Hybrid\vfr_startrek.mkv", fps=119.88, convertfps=true)

Opens a realmedia *rmvb clip:

DirectShowSource("F:\test.rmvb", fps=24, convertfps=true)

Opens a GraphEdit file:

V=DirectShowSource("F:\vid_graph.grf", audio=False) # video only (audio renderer removed)
A=DirectShowSource("F:\aud_graph.grf", video=False) # audio only (video renderer removed)
AudioDub(V, A)

See below for an audio example.


[edit] Troubleshooting video and audio problems

AviSynth will by default try to open only the media it can open without any problems. If one component cannot be opened it will simply not be added to the output. This will also mean that if there is a problem, you will not see the error. To get the error message to the missing component, use audio=false or video=false and disable the component that is actually working. This way AviSynth will print out the error message of the component that doesn't work.

[edit] Renderfile, the filter graph won't talk to me

This is a common error that occurs when DirectShow isn't able to deliver any format that is readable to AviSynth. Try creating a filter graph manually and see if you are able to construct a filter graph that delivers any output AviSynth can open. If not, you might need to download additional DirectShow filters that can deliver correct material.

[edit] The picture is skewed or torn and the colors are wrong

Some DirectShow components incorrectly pad the lines of planar data to be DWORD aligned as is done for RGB24 DIB format. This is incorrect, but it is a fairly common mistake. By adding a '+' to the start of the pixel_type string you can inform DirectShowSource to treat planar data formats as padded DWORD aligned. This problem shows up when the width of the picture is not divisible by 4.

DirectShowSource("NonMod4Video.mp4", pixel_type="+YV12") # Bad DWORD aligned planar

[edit] The sample rate is wrong

Some filters might have problems reporting the right sample rate, and then correct this when the file is actually playing. Unfortunately there is no way for AviSynth to correct this once the file has been opened. Use AssumeSampleRate and set the correct sample rate to fix this problem.

[edit] My sound is choppy

Unfortunately Directshow is not required to support sample exact seeking. Open the sound another way, or demux your video file and serve it to AviSynth another way. Otherwise you can specify seekzero=true or seek=false as parameters or use the EnsureVBRMP3Sync filter to enforce linear access to the DirectShow audio stream.

[edit] My sound is out of sync

This can happen especially with WMV, apparently with variable frame rate video. Determine what the fps should be and set it explicitly, and use ConvertFPS to force it to a constant framerate. EnsureVBRMP3Sync reduces problems with variable rate audio.

DirectShowSource("video.wmv", fps=25, ConvertFPS=True)
EnsureVBRMP3Sync() 

[edit] My ASF renders start fast and finish slow

Microsoft in their infinite wisdom chose to implement ASF stream timing in the ASF demuxer. As a result it is not possible to strip ASF format files any faster than realtime. This is most apparent when you first start to process the streams, usually after opening the AviSynth script it takes you a while to configure your video editor, all this time the muxer is accumulating credit time. When you then start to process your stream it races away at maximum speed until you catch up to realtime at which point it slows down to the realtime rate of the source material. This feature makes it impossible to use AviSynth to reclock 24fps ASF material up to 25fps for direct PAL playback.

[edit] Windows7 users

Windows 7 forces its own DirectShow filters for decoding several audio and video formats. Changing their merits or physically removing those filters doesn't help. clsid made the tool "Win7DSFilterTweaker" to change the preferred filters. However new decoders need to be added each time so it's not the perfect solution.


[edit] Common tasks

This section will describe various tasks that might not be 100% obvious. :)

[edit] Opening GRF files

GraphEdit GRF files are automatically detected by a .grf filename extension and directly loaded by DirectShowSource. For AviSynth to be able to connect to it, you must leave a pin open in GraphEdit of a media types that AviSynth is able to connect to. AviSynth will not attempt to disconnect any filters, so it is important that the output type is correct. DirectShowSource only accepts YV24, YV16, YV12, YUY2, AYUV, Y41P, Y411, NV12, ARGB, RGB32 and RGB24 video formats and 32, 24, 16 and 8 bit PCM and IEEE FLOAT audio formats.

A given GRF file must only target one of an audio or video stream to avoid confusion when DirectShowSource attempts the connection to your open pin(s). This single stream restriction is enforced.

[edit] Down-mixing AC3 to stereo

There are essentially two ways to do this. The first is to set the down-mixing in the configuration of your AC3 decoder itself, and the second one is to use the external down-mixer of "Trombettworks":

1) Install AC3filter.

a) Open AC3Filter Config
On tab "Main" in section "Output format" select "2/0 - stereo".
[Nothing else is needed.]

-OR-

b) Open the AC3 file in WMP6.4 and select the file properties. Set the output of AC3Filter on 2/0 - stereo. If you want the best possible quality, select PCM Float as Sample format.

Ac3downmix1a.jpg
Ac3downmix1b.jpg

Make the following script:

v = MPEG2Source("e:\movie.d2v")
a = DirectShowSource("e:\Temp\Test2\test.ac3")
AudioDub(v,a)

Finally, open the script in VirtualDub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).

2) Register the directshow filter Channel Downmixer by Trombettworks (under start -> run):

regsvr32 ChannelDownmixer.ax

Open the AC3 file in WMP6.4 and select the file properties. Set the output of AC3Filter on 3/2+SW 5.1 channels (this downmixer can't handle PCM Float, thus PCM 16 bit is selected here). In the properties of the downmixer, the number of input and output channels should be detected automatically. Check whether this is indeed correct.

Ac3downmix2a.jpg
Ac3downmix2b.jpg
Ac3downmix2c.jpg

Make the following script:

v = Mpeg2Source("e:\movie.d2v")
a = DirectShowSource("e:\Temp\Test2\test.ac3")
AudioDub(v,a)

Finally, open the script in vdub and convert the audio stream to MP3 (of course you can also demux the downmixed WAV stream if needed).

For some reason, I (who?) can't get this to work with DTS streams :(


[edit] Changes

v2.60 Added pixel_types "YV24", "YV16", "I420", "NV12", "AYUV", "Y41P", "Y411".
Add '+' to pixel_type for padded planar support.
v2.57 framecount overrides the length of the streams.
logfile and logmask specify debug logging.
v2.56 convertfps turns vfr into constant cfr by adding frames
seekzero restricts seeking to beginning only
timeout controls response to recalcitrant graphs
pixel_type specifies/restricts output video pixel format


[edit] See also

Haali media splitter also comes with an (unrelated) DirectShow input plugin DirectShowSource2, aka DSS2

Personal tools