AviSynth

AviSynth
Chaitanya Ghone
[email protected]
Introduction
•
Website
– http://avisynth.org/mediawiki/Main_Page
– Downloads links available on above page
•
What is it?
– AviSynth is a powerful tool for video post-production. It provides ways of editing and
processing videos
– AviSynth works as a frameserver, providing instant editing without the need for temporary
files
– AviSynth itself does not provide a graphical user interface (GUI), but instead relies on a script
system that allows advanced non-linear editing
– Text-based scripts are human readable, projects are inherently self-documenting. The
scripting language is simple yet powerful, and complex filters can be created from basic
operations to develop a sophisticated palette of useful and unique effects.
Common applications
• Some typical video operations that can be
done using AviSynth
– Extracting individual fields
– Overlaying frame numbers
– Side-by-side stacking of videos
– Dropping / Duplicating frames
– Applying simple blur filters, noise
addition/removal filters
How to use?
• Create a script (text file)
– example.avs
• Using mplayer
– “mplayer example.avs”
– This will display the output of your script
• Using FFMPEG
– “ffmpeg -i example.avs output.yuv”
– The output of your script will be given as input to ffmpeg
– This may be dumped out as raw YUV using above command or
encoded into any other format that ffmpeg supports
AviSynth Filters
•
Internal Filters
–
–
–
http://avisynth.org/mediawiki/Internal_filters
This are part of default AviSynth installation
Provides very common functionalities required for video processing like
•
•
•
•
External Filters
–
–
–
–
http://avisynth.org/mediawiki/External_filters
http://avisynth.org/warpenterprises/
This are available separately for download from the link above
These provide more advanced features like
•
•
•
•
•
–
•
Resizing
Display frame numbers
Field operation for interlaced streams
Anti-aliasing filters
Noise filters
De-interlacers
Support for different RAW YUV video
Support for different containers
Some of these may not be free
Some important filters are available at
–
–
–
–
RawSource: http://avisynth.org/warpenterprises/#rawsource
ffmpegSource2: http://code.google.com/p/ffmpegsource/downloads/list
These need to be copied to your AviSynth plugins directory
These are required to run the examples in next slides
Example 1:
Using RAW video
#Read YUV: Specify width, height, format, offset in bytes to first data to read)
V1 = RawSource("test.yuv",176,144,"I420“, offset= 38016) #Read from second frame
#Display frame number or SMPTE style timing from any offset as required
V1 = ShowFrameNumber(V1, x=90, y=30, offset=100)
V1 = ShowSMPTE(V1,x=90,y=60,offset="00:01:00:00")
#Only sets the frame-rate property, no change to actual data
V1 = AssumeFPS(V1,30)
V1 = ShowFrameNumber(V1, x=90, y=90, offset=100)
#Duplicates or drops frames to get specified frame rate
V1 = ChangeFPS(V1, 60)
V1 = ShowFrameNumber(V1, x=90, y=120, offset=100)
#Changes frame rate by blending consecutive frames (Range originalFPS*0.66 to inf)
V1 = ConvertFPS(V1,120)
V1 = ShowFrameNumber(V1, x=90, y=150, offset=100)
#Return video to application
return V1
Example 1:
Output using mplayer
Frame 0
Frame 1
Frame 2
Resolution is
176x144
Frame 3
Frame 4
Example 2:
Using compressed data
#Reading encoded data, this needs to be in a container like AVI or MOV or MP4 or TS
V1 = ffmpegSource2("test.mp4")
V1 = ShowFrameNumber(V1, x=90, y=30, offset=100)
V1 = ShowSMPTE(V1,x=90,y=60,offset="00:01:00:00")
V1 = AssumeFPS(V1,30)
V1 = ShowFrameNumber(V1, x=90, y=90, offset=100)
#Duplicates or drops frames to get specified frame rate
V1 = ChangeFPS(V1, 60)
V1 = ShowFrameNumber(V1, x=90, y=120, offset=100)
#Changes frame rate by blending consecutive frames (Range originalFPS*0.66 to inf)
V1 = ConvertFPS(V1,120)
V1 = ShowFrameNumber(V1, x=90, y=150, offset=100)
return V1
Example 2:
Output using mplayer
Frame 0
Frame 1
Frame 2
Resolution is
176x144
Frame 3
Frame 4
Example 3:
Handling multiple inputs
#Reading encoded data, this needs to be in a container like AVI or MOV or MP4 or TS
V1 = RawSource("test.yuv",176,144,"I420")
V2 = ffmpegsource2("test.mp4")
#Cropping examples
#Extract left half
V1_L = crop(V1, 0, 0, -(V1.width/2), 0)
#Extract right half
V1_R = crop(V1, (V1.width/2), 0, 0, 0)
#Extract top half
V2_T = crop(V2, 0, 0, 0, -(V1.Height/2))
#Extract bottom half
V2_B = crop(V2, 0, (V1.Height/2), 0, 0)
#Stacking multiple frame data
V3 = StackHorizontal(V1_R, V1_L)
V4 = StackVertical(V2_B, V2_T)
V = StackVertical(V3,V4)
return V
Example 3:
Output using mplayer
Resolution is
176x288
Example 4:
Handling interlaced data
#Read input assuming Top Field and Bottom Field in interleaved in one frame data
V1 = RawSource("test.yuv",176,144,"I420")
V1 = ShowFrameNumber(V1, x=30, y=30, offset=100)
V1 = AssumeFrameBased(V1)
#Set property of original stream to TopFieldFirst (TFF)
V1 = AssumeTFF(V1)
#Extract individual fields
V_Fld = SeparateFields(V1)
V1_TF = V_Fld.SelectEven()
V1_TF = ShowFrameNumber(V1_TF, x=30, y=60, offset=100)
V1_BF = V_Fld.SelectOdd()
V1_BF = ShowFrameNumber(V1_BF, x=30, y=60, offset=100)
#Explicit swap fields by forcing BottomFieldFirst(BFF) on TFF stream
V2 = Interleave(V1_TF, V1_BF)
V2 = AssumeFieldBased(V2)
V2 = AssumeBFF(V2)
V2 = Weave(V2)
#Simple Swap Fields
V5 = SwapFields(V1)
V3
V4
V4
V4
=
=
=
=
StackVertical(V1_TF, V1_BF)
StackHorizontal(V3, V2)
StackHorizontal(V4, V5)
StackHorizontal(V1, V4)
return V4
Example 4:
Output using mplayer
Frame 0
Field Separated
Field merged
but swapped
Resolution is
704x144
Direct field
swapping using
original
Example 5:
Generating standard Inputs
#Generate Black Frame
V1 = BlankClip(width=176, height=144)
#Convert to YUV420P since default is RGB
V1 = ConvertToYV12(V1)
#Generate standard SMPTE color chart
V2 = ColorBars(width=176, height=144)
#Convert to YUV420P since default is RGB
V2 = ConvertToYV12(V2)
#Interleave the two data in time and return to application
V3 = Interleave(V1,V2)
V3 = ShowFrameNumber(V3, x=30, y=30)
return V3
Number of frames are controlled using application:
ffmpeg –vframes 10 –i example4.avs output.yuv
mplayer –frames 10 example4.avs
Example 5:
Output using mplayer
Frame 0
Frame 1
Frame 2
Resolution is
176x144
Frame 3
Frame 4
Example 6:
More filters!
V1 = RawSource("test.yuv",176,144,"I420")
#Resize to 352x288
V2 = Lanczos4Resize(V1, 352, 288)
#Resize back to 176x144
V2 = Lanczos4Resize(V2, 176, 144)
#Overlays comparison details on V1
V3 = Compare(V1,V2)
#Writes comparison data to file
V4 = Compare(V1,V2,"Y","compareY.log")
V5 = Compare(V1,V2,"U","compareU.log")
V6 = Compare(V1,V2,"V","compareV.log")
V8 = Compare(V1,V2,"","compare.log") #Compares all Y,U,V
#Generate Histogram
V7 = Histogram(V1)
#Need to use all output to actually dump the log files
return stackHorizontal(V3, FlipHorizontal(V4), FlipVertical(V5), V6, V7, V8)
Example 6:
Output using mplayer
Total Resolution is 1312x144
Histogram is 256x144
compareY.log
compareU.log
compareV.log
compare.log
Comparing channel(s) Y
Comparing channel(s) U
Comparing channel(s) V
Comparing channel(s) YUV
Mean
Max Max
Absolute Mean Pos. Neg.
Frame Dev.
Dev. Dev. Dev. PSNR (dB)
----------------------------------------------------0 0.5449 -0.0392 10 -15 46.9531
1 0.4162 -0.0278 10 -15 48.1197
2 0.4503 -0.0368 10 -15 47.8962
3 0.3937 -0.0344 11 -14 48.6711
4 0.4221 -0.0333 12 -13 48.3770
Mean
Max Max
Absolute Mean Pos. Neg.
Frame Dev.
Dev. Dev. Dev. PSNR (dB)
----------------------------------------------------0 0.0137 -0.0002 1 -1 66.7538
1 0.0199 -0.0022 1 -1 65.1452
2 0.0262 -0.0028 1 -1 63.9479
3 0.0238 +0.0024 1 -1 64.3592
4 0.0271 +0.0006 2 -1 63.7435
Mean
Max Max
Absolute Mean Pos. Neg.
Frame Dev.
Dev. Dev. Dev. PSNR (dB)
----------------------------------------------------0 0.0535 +0.0055 4 -4 59.0309
1 0.0461 +0.0076 4 -4 59.5976
2 0.0527 +0.0044 4 -4 59.1767
3 0.0456 +0.0077 4 -4 59.7441
4 0.0443 +0.0011 4 -5 59.9061
Mean
Max Max
Absolute Mean Pos. Neg.
Frame Dev.
Dev. Dev. Dev. PSNR (dB)
----------------------------------------------------0 0.3744 -0.0252 10 -4 48.6360
1 0.2884 -0.0177 10 -4 49.7829
2 0.3134 -0.0243 10 -4 49.5506
3 0.2741 -0.0213 11 -4 50.3194
4 0.2933 -0.0219 12 -5 50.0314
Debugging scripts
•
•
Everything is case-insensitive
Semi-colons at end of line cause scripts to crash
•
Arbitrarily cut files with containers may not work
–
–
–
•
To debug scripts use mplayer command as follows
–
–
•
In this case, use ffmpeg to sync to nearest key frame
ffmpeg –i input.avi –vcodec copy output.avi
Any other container may also be used instead of avi
mplayer input.avs –demuxer avs –v
This will point out where the script faced problems
Number of frame to process
–
Number of frames can be controlled by mplayer or ffmpeg in all scenarios
•
•
ffmpeg –vframes 100 –i input.avs output.yuv
mplayer –frames 100 –i input.avs
•
All mplayer and ffmpeg options works normally as it does for any other file
•
Handling elementary streams
–
–
RAW H264 streams are not supported by the default filters
These need to be embedded in a container format like AVI/MP4/MOV/MKV using ffmpeg or any other means
•
ffmpeg –i input.h264 –vcodec copy output.avi
Other features
•
•
Supports audio filters like resampling, equalizers, etc
Complex filters like increasing FPS using temporal interpolation for intermediate frames
•
Lot of plugins available
–
–
–
•
Scripting reference
–
•
http://avisynth.org/warpenterprises/
http://avisynth.org/mediawiki/External_filters
http://avisynth.org/mediawiki/Internal_filters
http://avisynth.org/mediawiki/Scripting_reference
Advanced features
–
Runtime filters
•
•
•
http://avisynth.org/mediawiki/Runtime_environment
Provides features like conditionally running filters
Editors for AVS script with features like auto-completion, etc..
–
http://avisynth.org/qwerpoi/