AviSynth cookbook

  • Published 2006-04-25 (2 years ago)
  • Updated 2006-10-09 (1 year, 7 months ago)

AviSynth is a powerful, script based system for video post-processing. This notebook entry is an attempt to document some of the tings I have learned using this tool. It is not an in-depth guide, just a growing collection of simple recipes and some of my experiences using this tool.

Contents

Most of the example screen shots in this notebook entry are from a visualization project I am working on. The aim of the project is to create an animation that illustrates the principles of underwater navigation. The graphics are created and rendered with Blender. To generate trajectories and motion I have used NavLab, a generic tool for navigation system research and development

Video input and output

Loading image sequences

It's common practice to render an animation to disk as a series of image files. This gives maximum flexibility when composing the final video. Here's how you can load such an images sequence with AviSynth:

# Load image sequence with filenames 0001.png, 0002.png, ..., 1999.png
video = ImageSource("%04d.png", start=1, end=1999, fps=25,pixel_type = "rgb32")

Note that I have explicitly set the pixel type to rgb32. That's because my png images have an alpha channel. Loading the images this way preserves the alpha channel.

Video compositing

AviSynth can do many interesting compositing effects with just a few lines of code.

Video mosaic

Sometimes it is useful to create a mosaic of different video sources. The image below shows a scene seen from four different camera angles.

A video mosaic  of four different camera angles

Figure 1. A video mosaic of four different camera angles.

# Create a mosaic of four video sources

# Load video sources.
# The video sources have to be of the same size and length.
a = AviSource("sourceA.avi")
b = AviSource("sourceB.avi")
c = AviSource("sourceC.avi")
d = AviSource("sourceD.avi")

#
a = a.subtitle("A")
b = b.subtitle("B")
c = c.subtitle("C")
d = d.subtitle("D")

row1 = StackVertical(a,c)
row2 = StackVertical(b,d)

mosaic = stackhorizontal (row1, row2)
return mosaic

Display frame number

Video with frame number

Figure 2. Current frame number.

# Overlay current frame number on video
video = AviSource("sourceA.avi")
ScriptClip(video,"subtitle(string(current_frame))")

Picture in picture

The picture in picture effect

Figure 3. The picture in picture effect

# Create a picture in picture effect

# If you can open a video with Windows media player, but not with
# AviSource, use the DirectShowSource command.
a = DirectShowSource("cameraaction.wmv" ,fps=25)
b = DirectShowSource("cameraortho.wmv" ,fps=25)

# Reduce the size of B by 1/4
b = b.ReduceBy2().reduceBy2()
# Calculate cooridnates for putting B in the
# lower right corner with a 10 px offset
xpos = Width(a)-Width(b)-10
ypos = Height(a)-Height(b)-10
# overlay video clip B on A
Overlay(a, b,x=xpos, y=ypos)

Fade in overlay

# Fade in an overlay

# If you can open a video with Windows media player, but not with
# AviSource, use the DirectShowSource command.
a = DirectShowSource("cameraaction.wmv" ,fps=25)
b = DirectShowSource("cameraortho.wmv" ,fps=25)

# Reduce the size of B by 1/4
b = b.ReduceBy2().reduceBy2()
# Calculate cooridnates for putting B in the
# lower right corner with a 10 px offset
xpos = Width(a)-Width(b)-10
ypos = Height(a)-Height(b)-10

# Overlay video clip B on A using a fade in effect
c = b.ConvertToRGB32()
c = c.FadeIn(10)
Overlay(a, c, mode="blend", mask=showalpha(c),x=xpos, y=ypos)

Compositing layers

A common technique when creating videos and animations is to use layers. An example of this is shown below. For my animation I have rendered a scene using four different layers. This gives me the freedom to afterwards select which elements i want to use in my final video.

The picture in picture effect

Figure 4. Four different layers

All four layers combined are shown below.

The picture in picture effect

Figure 5. All the four layers combined

# Load four different layers and combine them
sf = 1 # sta frame
ef = 1900 # end frame
# images are pngs with alpha channel
l1 = ImageSource("err_p_kf\%04d.png", sf, ef, 25,pixel_type = "rgb32")
l2 = ImageSource("Hugin\%04d.png", sf, ef, 25,pixel_type = "rgb32")
l3 = ImageSource("p_true\%04d.png", sf, ef, 25,pixel_type = "rgb32")
l4 = ImageSource("p_kf\%04d.png", sf, ef, 25,pixel_type = "rgb32")

# Combine the image sources with the layer command
# l1 is the bottom layer and l4 is the top layer
video= l1.Layer(l2).Layer(l3).layer(l4)
return video

Comments

Post a comment

Markdown syntax enabled

(required)

(required, but will not be published)