Difference between revisions of "User:Mjb/AviSynth"

From Offset
Jump to navigationJump to search
(Using AviSynth with MPEG-2 files)
(+vertical stabilization)
 
(9 intermediate revisions by the same user not shown)
Line 17: Line 17:
 
Since you are reading raw video, you probably need to be sure to set the output aspect ratio. For example, 720x480 (3:2) input will stay 3:2 unless you specify <code>-aspect 4/3</code> or whatever.
 
Since you are reading raw video, you probably need to be sure to set the output aspect ratio. For example, 720x480 (3:2) input will stay 3:2 unless you specify <code>-aspect 4/3</code> or whatever.
  
==Using AviSynth with MPEG-2 files==
+
==Using AviSynth with MPEG-1 or MPEG-2 files==
  
An AviSynth script can only open AVI files natively. Other types require special plugins. For MPEG-2 and MPEG-1 files (including .vob, .ts, .mpg), it's a multi-step process:
+
An AviSynth script can only open AVI files natively. Other types require special plugins.
 +
 
 +
For MPEG-2 and MPEG-1 files (including .vob, .ts, .mpg), it's a multi-step process:
  
 
# Install the DGMPGDec package, which gives you the DGIndex app and the DGDecode AviSynth plugin.
 
# Install the DGMPGDec package, which gives you the DGIndex app and the DGDecode AviSynth plugin.
Line 27: Line 29:
  
 
<pre>LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
 
<pre>LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
MPEG2Source("input.d2v")
+
MPEG2Source("i:\path\to\the\file.d2v",cpu2="ooxxox")
 
...
 
...
 
</pre>
 
</pre>
  
For better quality, at some loss of detail, I suggest enabling all the standard MPEG-1/2 deblock/dering post-processing options (<code>cpu2="xxxxxx"</code>) and maybe also add the "mosquito" noise reduction filter (to enable, [http://avisynth.nl/index.php/MosquitoNR get MosquitoNR plugin] and extract the DLL to the AviSynth plugins directory):
+
I find that quality is best when enabling the standard MPEG-1/2 dering post-processing options (<code>cpu2="ooxxox"</code>). If the quality of encoding was poor and the result looks blocky, then also enable the deblocking options (<code>cpu2="xxxxxx"</code>), although this does result in loss of some detail.
 
 
<pre>LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
 
MPEG2Source("input.d2v",cpu2="xxxxxx")
 
MosquitoNR(strength=16, restore=128, radius=2, threads=0)
 
...
 
</pre>
 
  
 
==Using AviSynth with MPEG-4 files==
 
==Using AviSynth with MPEG-4 files==
Line 43: Line 39:
 
Use [http://avisynth.nl/index.php/DirectShowSource DirectShowSource] with the input framerate set, e.g. this works for me with a 1080i VC-1 file demuxed to MKV from a Blu-Ray disc:
 
Use [http://avisynth.nl/index.php/DirectShowSource DirectShowSource] with the input framerate set, e.g. this works for me with a 1080i VC-1 file demuxed to MKV from a Blu-Ray disc:
  
     DirectShowSource("foo.mkv",fps=30000/1001)
+
     DirectShowSource("foo.mkv",fps=30000.0/1001.0)
  
 
Another option, but painfully slow, is [http://avisynth.nl/index.php/FFmpegSource FFmpegSource]:
 
Another option, but painfully slow, is [http://avisynth.nl/index.php/FFmpegSource FFmpegSource]:
Line 64: Line 60:
 
# Get the [http://avisynth.nl/index.php/Vinverse vInverse plugin].
 
# Get the [http://avisynth.nl/index.php/Vinverse vInverse plugin].
 
# Extract vinverse.dll to "%ProgramFiles(x86)%\AviSynth\plugins".
 
# Extract vinverse.dll to "%ProgramFiles(x86)%\AviSynth\plugins".
# In your AViSynth Script, you can now refer to the field-matcher TFM(), the duplicate frame-remover TDecimate(), and the residual comb artifact remover vInverse():
+
# In your AViSynth Script, you can now refer to the field-matcher TFM, the duplicate frame-remover TDecimate, and the residual comb artifact remover vInverse or improved(?) vInverse2:
  
 
<pre>LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
 
<pre>LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
MPEG2Source("input.d2v",cpu2="xxxxxx")
+
MPEG2Source("I:\path\to\the\input.d2v",cpu2="ooxxox")
MosquitoNR(strength=16, restore=128, radius=2, threads=0)
+
TFM(d2v="I:\path\to\the\input.d2v")
TFM(d2v="I:\video workspace\relax.d2v")
 
 
TDecimate()
 
TDecimate()
vInverse()
+
vInverse2()
 +
</pre>
 +
 
 +
I prefer this method to [[User:Mjb/FFmpeg#Inverse_telecine|the pure-FFmpeg method I documented]] elsewhere. Image quality and reliability is the same as in FFmpeg, but having the vInverse to blend stray combed frames is quite nice.
 +
 
 +
The above is for NTSC (3:2 pulldown) sources. If your source is PAL and is progressive with a duplicate frame every 25 frames, then all you have to do is <code>TDecimate(cycle=25)</code>, no TFM or vInverse necessary.
 +
 
 +
Another option, which is only for soft telecine NTSC sources (i.e. 24 fps progressive frames encoded with pulldown flags that instruct the player to repeat certain fields, thus doing the telecine on the fly), is to have DGIndex process the fields with its "Force Film" option. Even when DGIndex identifies the content as 95%+ film, it does not always lock onto the 3:2 pattern as well as it should, in which case it's best to just stick with Honor Pulldown Flags. But when Force Film works, you get 23.976 fps from MPEG2Source and thus you can skip the TFM/TDecimate step. I would still do vInverse because it seems to be harmless on any non-combed frames.
 +
 
 +
If you can use DGIndex, then for content that is a mix of telecined film and 29.97 fps interlaced video, this method works really well to convert to 59.94 fps deinterlaced & IVTC'd ([https://forum.doom9.org/showpost.php?p=1913417&postcount=418 source]):
 +
 
 +
<pre>A=QTGMC().selecteven()
 +
B=QTGMC().selectodd()
 +
C=TFM(field=1,mode=0,slow=2,mchroma=false,cthresh=-1,clip2=A,d2v="i:\path\to\the\file.d2v",flags=1,micmatching=0)
 +
D=TFM(field=0,mode=0,slow=2,mchroma=false,cthresh=-1,clip2=B,d2v="i:\path\to\the\file.d2v",flags=1,micmatching=0)
 +
interleave(C,D)
 
</pre>
 
</pre>
  
So far I am not finding this to be all that much better than [[User:Mjb/FFmpeg#Inverse_telecine|the pure-FFmpeg method I documented]] elsewhere.
+
==Best deinterlace==
  
==Best deinterlace & IVTC==
+
Supposedly the best deinterlace (but not IVTC) option is [http://avisynth.nl/index.php/QTGMC QTGMC](). It's good, and has a number of other features, like reducing shimmer.
  
Supposedly the best deinterlace & IVTC option is [http://avisynth.nl/index.php/QTGMC QTGMC]. I have yet to try it.
+
==Vertical stabilization==
 +
 
 +
VHS transfers can sometimes have a lot of vertical jitter. There aren't a lot of scripts for this, but Stab() works well enough ''after'' deinterlacing. It requires YV12 (4:2:0) input. Example:
 +
 
 +
<pre>AssumeTFF()
 +
ConvertToYV12(matrix="Rec601")
 +
QTGMC()
 +
Stab()
 +
</pre>
  
  
 
[[Category:Video]]
 
[[Category:Video]]

Latest revision as of 17:00, 22 October 2020

AviSynth is a script-based "frame server" (raw video streamer) with impressive, highly configurable filtering abilities. It is normally used as a preprocessor for a general-purpose encoder/muxer like VirtualDub or FFmpeg.

Basic usage

To use AviSynth, first install the AviSynth package. This gives you the ability to process videos via an AviSynth script (.avs file) which you must author yourself. The script takes care of making AviSynth load the input video and apply whatever filtering you specify. The output will be raw video which you must further encode in other software.

You edit your AviSynth script in a text editor. To test it, load the script into VirtualDub or other AviSynth-aware editor (instead of having those apps load the video directly). Another way of testing is to use the Windows GUI app AvsPmod, which is a nice little editor plus a preview pane, but no ability to save the processed video.

Using AviSynth with FFmpeg

If FFmpeg was built with --enable-avisynth (as the Zeranoe builds are), an AviSynth script can be the input file for the video (only the video, not audio):

   ffmpeg -i myscript.avs -i input.ac3 ...

If you get "Unknown error occurred", it is probably because FFmpeg is 64-bit and AviSynth is 32-bit, or vice-versa; they must match.

Since you are reading raw video, you probably need to be sure to set the output aspect ratio. For example, 720x480 (3:2) input will stay 3:2 unless you specify -aspect 4/3 or whatever.

Using AviSynth with MPEG-1 or MPEG-2 files

An AviSynth script can only open AVI files natively. Other types require special plugins.

For MPEG-2 and MPEG-1 files (including .vob, .ts, .mpg), it's a multi-step process:

  1. Install the DGMPGDec package, which gives you the DGIndex app and the DGDecode AviSynth plugin.
  2. Load the VOB in the DGIndex app. You can tweak some options at this point, but probably shouldn't.
  3. Save Project [F4]. This will generate a .d2v file, which is a frame-by-frame index DGDecode can use. It also demuxes the audio stream(s) into separate files you can use later.
  4. In your AviSynth script, load the DGDecode.dll plugin (unless you copied DGDecode.dll to the AviSynth plugins directory), and now you can use MPEG2Source() with the .d2v file:
LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
MPEG2Source("i:\path\to\the\file.d2v",cpu2="ooxxox")
...

I find that quality is best when enabling the standard MPEG-1/2 dering post-processing options (cpu2="ooxxox"). If the quality of encoding was poor and the result looks blocky, then also enable the deblocking options (cpu2="xxxxxx"), although this does result in loss of some detail.

Using AviSynth with MPEG-4 files

Use DirectShowSource with the input framerate set, e.g. this works for me with a 1080i VC-1 file demuxed to MKV from a Blu-Ray disc:

   DirectShowSource("foo.mkv",fps=30000.0/1001.0)

Another option, but painfully slow, is FFmpegSource:

   FFmpegSource2("foo.mkv")

I have not yet tried these:

  • DSS2mod, which is another DirectShow frameserver.
  • LSMASHSource, which is FFmpeg-based but apparently not so slow.
  • QTSource, which is for QuickTime 6 & 7 formats in MOV containers.

Several more are listed at http://avisynth.nl/index.php/DSS2#Source_Filters.

Inverse telecine

One way of doing IVTC via AviSynth is to use the TIVTC plugin.

  1. Get the TIVTC package.
  2. Extract tivtc.dll to "%ProgramFiles(x86)%\AviSynth\plugins".
  3. Get the vInverse plugin.
  4. Extract vinverse.dll to "%ProgramFiles(x86)%\AviSynth\plugins".
  5. In your AViSynth Script, you can now refer to the field-matcher TFM, the duplicate frame-remover TDecimate, and the residual comb artifact remover vInverse or improved(?) vInverse2:
LoadPlugin("e:\apps\DGMPGDec\DGDecode.dll")
MPEG2Source("I:\path\to\the\input.d2v",cpu2="ooxxox")
TFM(d2v="I:\path\to\the\input.d2v")
TDecimate()
vInverse2()

I prefer this method to the pure-FFmpeg method I documented elsewhere. Image quality and reliability is the same as in FFmpeg, but having the vInverse to blend stray combed frames is quite nice.

The above is for NTSC (3:2 pulldown) sources. If your source is PAL and is progressive with a duplicate frame every 25 frames, then all you have to do is TDecimate(cycle=25), no TFM or vInverse necessary.

Another option, which is only for soft telecine NTSC sources (i.e. 24 fps progressive frames encoded with pulldown flags that instruct the player to repeat certain fields, thus doing the telecine on the fly), is to have DGIndex process the fields with its "Force Film" option. Even when DGIndex identifies the content as 95%+ film, it does not always lock onto the 3:2 pattern as well as it should, in which case it's best to just stick with Honor Pulldown Flags. But when Force Film works, you get 23.976 fps from MPEG2Source and thus you can skip the TFM/TDecimate step. I would still do vInverse because it seems to be harmless on any non-combed frames.

If you can use DGIndex, then for content that is a mix of telecined film and 29.97 fps interlaced video, this method works really well to convert to 59.94 fps deinterlaced & IVTC'd (source):

A=QTGMC().selecteven()
B=QTGMC().selectodd()
C=TFM(field=1,mode=0,slow=2,mchroma=false,cthresh=-1,clip2=A,d2v="i:\path\to\the\file.d2v",flags=1,micmatching=0)
D=TFM(field=0,mode=0,slow=2,mchroma=false,cthresh=-1,clip2=B,d2v="i:\path\to\the\file.d2v",flags=1,micmatching=0)
interleave(C,D)

Best deinterlace

Supposedly the best deinterlace (but not IVTC) option is QTGMC(). It's good, and has a number of other features, like reducing shimmer.

Vertical stabilization

VHS transfers can sometimes have a lot of vertical jitter. There aren't a lot of scripts for this, but Stab() works well enough after deinterlacing. It requires YV12 (4:2:0) input. Example:

AssumeTFF()
ConvertToYV12(matrix="Rec601")
QTGMC()
Stab()