User Defined Function help

This forum is for questions and discussion of all the aspects of handling and cleaning up your footage with Avisynth.
Locked
User avatar
blaku92
Joined: Mon Feb 07, 2005 11:27 pm
Location: Los Angeles, CA
Org Profile

User Defined Function help

Post by blaku92 » Sun Jan 18, 2009 7:49 am

Ok, I'm at the point where I really want to learn how to make slightly more complicated scripts, but user defined functions confused me a little. You probably all know this from the Guides:

Code: Select all

"But wait!" you cry. "What of your dreaded bumper and it's impending doom?" Ah yes I'd almost forgotten the bumper! Well, since I'm trying to show all the superneato stuff you can do with AVISynth, I think I'll write a function to remove the bumper. Yep - you can actually write functions within AVISynth. No, this isn't just for show, it can actually be very useful.

So I'll write a function that returns a clip after Trimming the first 484 frames off of it (since that's how long my bumper is in 29.97fps). I'll shorten TrimBumper to TB for ease of typing later:

    function TB(clip input)
    {
    return input.Trim(484,0)
    }

Now that I've written this function, I can apply it to each of the video sources and splice them all together, then output the result:

    return a.TB() + b.TB() + c.TB() + d.TB()

and tada! My script is done. For a complete rundown, here is the final script in its entirity:

    function TB(clip input)
    {
    return input.Trim(484,0)
    }
    a = AVISource("D:\ErMaC Studios - Closer to God 2.avi")
    b = AVISource("D:\ErMaC Studios - Cowboy Blues.avi")
    c = AVISource("D:\ErMaC Studios - Go Speed Go.avi")
    d = AVISource("D:\ErMaC Studios - Evil Light.avi")
    b = b.BicubicResize(512,384)
    c = c.BicubicResize(512,384)
    a = a.AddBorders(0,56,0,56)
    d = d.AddBorders(0,56,0,56)
    b = b.ChangeFPS(29.97)
    d = d.ChangeFPS(29.97)
    return a.TB() + b.TB() + c.TB() + d.TB()

Note that I moved the function up to the top since that's the best place for defining functions, but it could go anywhere in the script as long as it's before the first time TB() is used.

If you'd like to see the nastiest AVISynth script I've ever written, here it is. It was a script which I fed into an MPEG2 encoder to make my own Read or Die subtitled, progressive DVD. It looks fantastic, BTW :D

    function Sub(clip clip, string filename)
    {
    LoadVirtualdubPlugin("C:\program files\virtualdub\plugins\subtitler.vdf", "_VD_Subtitler")
    return clip._VD_Subtitler(1, filename, 0, 0)
    }
    LoadPlugin("D:\MPEG2DEC.DLL")
    LoadPlugin("D:\Decomb.DLL")
    rod1 = MPEG2Source("E:\ReadOrDie1DVD\rod1.d2v").Telecide(chroma=true,post=false,threshold=35).Decimate(cycle=5).ConvertToRGB().Sub("E:\ReadOrDie1DVD\Read Or Die Ep 01 [FINAL 230601].ssa")
    rod2 = MPEG2Source("E:\rod2.d2v").Telecide(chroma=true,post=false,threshold=35).Decimate(cycle=5).ConvertToRGB().Sub("E:\ReadOrDie2DVD\RoD2.v5.Edit.silencer.ssa")
    rod3 = MPEG2Source("E:\ReadOrDie3DVD\rod3.d2v").Telecide(chroma=true,post=false,threshold=35).Decimate(cycle=5).ConvertToRGB().Sub("E:\ReadOrDie3DVD\ROD3-Edit4.ssa")

    return rod1 + BlankClip(length=3,height=480,width=720,fps=23.976,audio_rate=0) + rod2 + rod3
Alright so, in the first script, "function TB(clip input)" is the function created by the user, and the "return input.Trim(484,0)" is what we want that function to do to a variable we choose right? What I don't understand about these two scripts is the difference between "TB(clip input)" and "Sub(clip clip, string filename)." Should I be assuming that "input" represents the one letter variable, and from the other script the second "clip" represents the actual File name? I'm so confused. Also, what's the deal with "(1, filename, 0, 0)?"

thanks -- Point me in the right direction and I'll gladly do some more reading.
Image

"I've dated uglier girls than you for breakfast."

User avatar
Scintilla
(for EXTREME)
Joined: Mon Mar 31, 2003 8:47 pm
Status: Quo
Location: New Jersey
Contact:
Org Profile

Re: User Defined Function help

Post by Scintilla » Sun Jan 18, 2009 9:54 am

blaku92 wrote:Alright so, in the first script, "function TB(clip input)" is the function created by the user, and the "return input.Trim(484,0)" is what we want that function to do to a variable we choose right? What I don't understand about these two scripts is the difference between "TB(clip input)" and "Sub(clip clip, string filename)." Should I be assuming that "input" represents the one letter variable, and from the other script the second "clip" represents the actual File name?
In each case, the word that comes first is the data type of the parameter being declared, and the word that comes second is the name of the parameter (as used internally by the function).

So, the TB function takes one parameter. It is a video clip called "input".
The Sub function takes two. The first one is a video clip called "clip" (it could have been called anything else and it would still work -- the fact that this parameter has the same name as its data type is irrelevant), and the second is a string called "filename".

In the case of Sub, the clip argument is the video clip to have subtitles overlaid on it, and the filename argument is the name of the text file from which to take the subs.
Absolute Destiny calls this function with only the filename argument given explicitly, because the clip argument is supplied implicitly by using the dot operator -- whatever comes before the period ( .Sub(blah blah blah) ) is treated as the clip parameter. Most AVISynth functions work the same way in that regard.
blaku92 wrote:I'm so confused. Also, what's the deal with "(1, filename, 0, 0)?"
Apparently the _VD_Subtitler function takes five parameters (including the input clip). You'd have to hunt down the text of this function, or documentation for it, to figure out what they all mean. (I don't have it myself.)
ImageImage
:pizza: :pizza: Image :pizza: :pizza:

User avatar
blaku92
Joined: Mon Feb 07, 2005 11:27 pm
Location: Los Angeles, CA
Org Profile

Re: User Defined Function help

Post by blaku92 » Sun Jan 18, 2009 6:33 pm

Ok, it all makes more sense to me now. Im in no way a programmer, so I didn't realize the first word was a data type and the second word was the name. I had a feeling that the _VD_Subtitler was a function with parameters, but I've never seen a parameter of "filename." That's pretty cool that all you need to type in later is the actual directory\filename of the subtitle file and it knows what you're talking about. Btw, is there a list of all the avisynth data types somewhere? Just in case I want to write a script and need to know some of them?

Thanks -- very helpful
Image

"I've dated uglier girls than you for breakfast."

User avatar
Scintilla
(for EXTREME)
Joined: Mon Mar 31, 2003 8:47 pm
Status: Quo
Location: New Jersey
Contact:
Org Profile

Re: User Defined Function help

Post by Scintilla » Sun Jan 18, 2009 6:50 pm

A full explanation of script variables, including all six supported data types, can be found here:
http://avisynth.org/mediawiki/Script_variables

Glad to be of service. :)
ImageImage
:pizza: :pizza: Image :pizza: :pizza:

User avatar
Qyot27
Surreptitious fluffy bunny
Joined: Fri Aug 30, 2002 12:08 pm
Status: Creepin' between the bullfrogs
Location: St. Pete, FL
Contact:
Org Profile

Re: User Defined Function help

Post by Qyot27 » Mon Jan 19, 2009 1:23 am

blaku92 wrote:but I've never seen a parameter of "filename." That's pretty cool that all you need to type in later is the actual directory\filename of the subtitle file and it knows what you're talking about.
How do you mean? Some regular filters (like TextSub) are called simply by way of TextSub("subtitles.ass") and it'll do the same thing - in that case, overlaying X subtitle file on whatever video source you're using.

Generally speaking, I've always seen the purpose of functions in AviSynth to be very complex processing tasks, not a way to streamline (obfuscate would be a better word in some cases) the script code. Think functions like FastLineDarken or BeforeAfterDiff.

But some of the ideas about variables do come in handy. Using AudioDub() requires them in many instances, or if you want to quickly Trim sections of a file and splice them together it can save the frustration of recalling the source line over and over again like so:

Code: Select all

v1 = DirectShowSource("file.mkv")
Trim(v1,500,1000) ++ Trim(v1,2000,2500) ++ Trim(v1,6500,7845) ++ yadda yadda yadda
I simply don't see the point in using a lot of the deep programming calls if you're trying to do something very simply accomplished with less exotic-looking code - it'd take more time for me to set up the fancy quick references than it would to just type all the actual code out. But of course it doesn't stop it from happening.
My profile on MyAnimeList | Quasistatic Regret: yeah, yeah, I finally got a blog

User avatar
blaku92
Joined: Mon Feb 07, 2005 11:27 pm
Location: Los Angeles, CA
Org Profile

Re: User Defined Function help

Post by blaku92 » Mon Jan 19, 2009 6:05 am

I never knew how user-defined functions worked until now. What I mean is, I knew that commands used filenames like TextSub("filename.ass"), but I didn't know you could make a user-defined function like return clip._VD_Subtitler(1, filename, 0, 0) in the sub-function and then later on in the script use the actual filename like rod2 = MPEG2Source("E:\rod2.d2v").Telecide(chroma=true,post=false,threshold=35).Decimate(cycle=5).ConvertToRGB().Sub("E:\ReadOrDie2DVD\RoD2.v5.Edit.silencer.ssa"). I hope that explanation is less confusing. Half the time I confuse myself with terminology. I've been editing for a few years now and it's been difficult, as I'm sure you folks probably know from experience, trying learn and keep up with new technology/programs -- Not to mention dealing with everyday responsibilities. However, this week I've decided to change my life by studying more about everything I should know as a post-production expert. To my eyes, the Org is a gold mine of useful info and people. Thanks for all the help and for fueling the fire to my very own case of Goldenboy Syndrome.
Image

"I've dated uglier girls than you for breakfast."

Locked

Return to “AviSynth Help”