View Full Version : global variable within ScriptClip?
zee944
14th March 2011, 16:46
Hey,
In short, I'd like to store an information into a variable within the ScriptClip environment and query the variable at a (much) later point in the script.
A little longer explanation: in a video the first 10,000 frames are different (softer), so need different treatment. In the middle of the script, I do a Trim(), trimming down frames from both the beginning and end of the video, and after the Trim() command I have to apply a filter to the first segment (what remained from the originally 10,000 frames after trimming).
What I had in mind was this (rough example):
ScriptClip("""
arethesecredits=(current_frame<10000)?true:false
""")
...
Trim(2654,128609)
...
<lots of scripting here>
...
(arethesecredits == true ) ? Sharp(1.0) : Blur (0.25)
It turned out it doesn't work. Not even with putting the last line into another ScriptClip().
I know there are theoretical roundabouts to this, such as applying the filter before the trimming, or do everything within one ScriptClip() command, but in this case these wouldn't really work in the practice. Modifying the script to achieve this simple thing would make my life difficult as hell.
Does anyone know how to implement the original idea? It'd be very-very helpful to me.
-Vit-
14th March 2011, 17:03
Are you decimating or otherwise adding/removing frames after that trim? If not then this will do it:
Credits=10000
<process, process>
TrimStart=2654
Trim(TrimStart,128609)
result = <more processing>
TrimCredits=Credits-TrimStart
result.Trim(0,TrimCredits-1).Sharp(1.0) + result.Trim(TrimCredits,0).Blur (0.25)
Or you can use ApplyRange for a similar result.
Obviously you don't have to use variables (TrimStart etc.) you can just put the actual values. I find it easier to tweak in a single place
You would need to alter the trims on the result if that latter processing changed the number of frames in any way.
zee944
20th March 2011, 16:23
Thanks, it would work indeed, but it's still a roundabout. The Trim() lines are generated by an external software (pls don't ask me to go into the details, it's way too complicated why it's like this), I can't use a variable in it. I have to achieve my orginal idea, else everything will go upside down. No way? :(
Gavino
20th March 2011, 18:46
Which parts of the script are generated by the external program?
Can't you edit its output to produce the final script along the lines of -Vit-'s solution?
Or write another program to process the results of the first, if you want an automated solution.
zee944
20th March 2011, 20:36
I could certainly modify the script to use variables, but there are hundreds of scripts that partly generated by the program. I wrote the program, but I wanted to avoid modifying it because the whole process is very complicated already and the current program is tested hundreds of times, modifying it is quite risky.
I take it that it's just not possible what I want.
-Vit-
20th March 2011, 20:44
I think it's possible, just ugly. I have no way to test at the moment, so this is just a concept. Embed the conditional data into the clip itself:
src = WhateverSource("...")
credits=10000
selector = BlankClip(src,color=$000000,length=credits) + BlankClip(src,color=$ffffff,length=FrameCount(src)-credits)
result1 = <process, process>
h=Height()
StackVertical( result1, selector )
Trim(2654,128609) # Machine generated
selector = Crop(0,h,0,0)
Crop(0,0,0,-h)
result2 = <more processing>
mt_merge( result2.Sharp(1.0), result2.Blur(0.25), selector, U=3,V=3 )
I'm sure it could be made more efficient.
Gavino
20th March 2011, 21:19
One possibility based on your original idea would be to do this:
FrameEvaluate("arethesecredits=(current_frame<10000)")
...
Trim(2654,128609)
...
<lots of scripting here>
...
ScriptClip("arethesecredits ? Sharp(1.0) : Blur (0.25)", after_frame=true)
-Vit-'s solution is neat, but would have to be amended if any of the processing changes the clip width or does any kind of frame decimation.
-Vit-
21st March 2011, 02:48
That looks much better than my effort, although I imagine it would need sequential access or any caching will throw it off..?
Gavino
21st March 2011, 10:36
I imagine it would need sequential access or any caching will throw it off..?
It doesn't have to be sequential, but it does require that each output frame is derived from exactly one input frame, and no frame is repeated. So frames could be re-ordered, but any temporal filtering could throw it off (at least around the boundary between credits and the rest).
-Vit-
21st March 2011, 18:12
... and no frame is repeated..
Which is the situation I meant to describe. If you view that script as-is in Vdub and navigate the timeline around the sharp/blur transition, then it becomes confused about which frames are blurred/sharp. I imagine it's because the caching (of the Trim) means the FrameEvaluate is skipped. No problem for straight encoding though.
Gavino
21st March 2011, 19:44
That's right.
By "sequential access", I thought you were referring to the processing in the script, rather than manually jumping around on the timeline, but of course they both have the same effect as far as Avisynth is concerned.
Note that here you can jump around freely as long as you never jump backwards across the transition point.
zee944
23rd March 2011, 18:24
I went with Gavino's solution. Although there is temporal filtering in the script, it works fine on the test encode.
It's a big relief I don't have to modify the external software... huhh. Thank you very much for both solutions! :D
Mug Funky
25th March 2011, 05:06
you can have globals in the scriptclip environment.
you have to initialise them first in the main script.
so:
global foo=0
scriptclip(""" blankclip().subtitle(string(foo))""")
should work.
Gavino
25th March 2011, 12:15
The question wasn't really about global variables, more about how to communicate per-frame information from one part of the filter graph to another. As you can see, neither of the solutions posted by -Vit- or me involved using a global.
In your example, foo does not need to be global either, as run-time scripts (eg within ScriptClip) are evaluated at the same scope level as the main script.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.