Monday, January 26, 2009

Animating Cartoon Mouths with AviSynth

Animating a cartoon mouth can take a lot of time. To do it well, you can get a program that lets you select one of nine (or more) mouth positions, and go through your dialogue syllable by syllable to match it up. But what if you don't want to spend all that time? What if it's not worth all that labor for a quick joke? I've developed a way to automatically animate your mouth from the amplitude, which is kind of sort of the "loudness," of your WAV file. The method was "cleaned up" a bit with the help of some folks at Doom9's Forum, to make it usable for the slightly less tech-minded masses.

First, you need to install AviSynth. The current version is 2.5.8. Next, you need the MinMaxAudio plugin. Here is a link where you can download it. From that zip file, you need to get MinMaxAudio.dll and put it in your plugins folder (Start > Programs > AviSynth 2.5 > Plugins Directory). Next you need this script, which puts it all together. Just copy the following and paste it into Notepad:

#Load Sources
Aud=WavSource("mouthaudio.wav")#.Normalize(1) #If you need Normalizing.
m00=ImageSource("00.png",pixel_type="rgb32",0,0)
m25=ImageSource("25.png",pixel_type="rgb32",0,0)
m50=ImageSource("50.png",pixel_type="rgb32",0,0)
m75=ImageSource("75.png",pixel_type="rgb32",0,0)
m100=ImageSource("100.png",pixel_type="rgb32",0,0)

#Set Parameters
FRateNumerator = 30000
FRateDenominator = 1001
High = -6
Low = -36
Steps = 5

#Set up clip
Gap = (Low-High)/Steps
AudLength=AudioLengthF(Aud)/AudioRate(Aud)
Slug=BlankClip(m00).AssumeFPS(FRateNumerator,FRateDenominator).loop()\
.trim(0,Ceil(AudLength*FRateNumerator/FRateDenominator))
Mouth=AudioDub(slug,aud)

#"Animate" Clip
ScriptClip(Mouth,"AM=AudioMax(1)
AM >= High+Gap*0 ? m100 \
: AM >= High+Gap*1 ? m75 \
: AM >= High+Gap*2 ? m50 \
: AM >= High+Gap*3 ? m25 \
: m00")

#Fix ScriptClip Audio Glitch
AudioDub(last, WavSource("mouthaudio.wav"))

Save it as "MouthMover.avs" - yes, put the filename in quotes.

Now, you're ready to get going. You need five images, of your character and its mouth 0, 25, 50, 75, and 100 percent open. They should be the same size, in width and height. They should be named 00.png, 25.png, 50.png, 75.png, and 100.png. You need a WAV of your character's dialogue. Call it MouthAudio.wav. So at this point, the five images, MouthAudio.wav, and MouthMover.avs should be in the same folder. Now, just drag that AVS into VirtualDub and your mouth should be animated and ready to go!

If you only want the mouth with alpha transparency (and not the character too), you can design the mouth only and use AVS2AVI to save an AVI with transparency. Paste the following into Notepad, save as a .BAT file in the same directory as the other files, and AVS2AVI.exe:
avs2avi mouthmover.avs mouth.avi -w

Then double click the BAT file, and it will ask you for the codec you want. Pick Lagarith or HuffYUV and be sure to configure to RGB32. It must be 32 bit to have an alpha channel. Then OK everything and it will do its magic.

1 comment:

harib said...

Thank you for the post it helped me a
lot. I need to know if, I can avoid the popup to select the compressor, i,e can I set a compressor of my choice by default inside the bat file.