Log in

View Full Version : GRunT - Easier run-time scripting


Pages : [1] 2

Gavino
9th July 2008, 02:00
Avisynth's run-time environment (http://avisynth.nl/index.php/Runtime_environment) is very powerful, supporting complex video processing that would be difficult or impossible to perform in a normal script. But it's not easy to use - the behaviour of run-time scripts (especially in combination) can be hard to understand and there are usability problems concerning scope and lifetime of variables (http://avisynth.nl/index.php/The_script_execution_model/Scope_and_lifetime_of_variables#Runtime_scripts).

"Cond. filters are quite complex - and sometimes I am even surprised of the outcome" - sh0dan

GRunT (Gavino's Run-Time ;)) is a plugin which addresses these and other problems, making the run-time system much easier to use.

Features:
- Simple, natural and robust way to pass variables into a run-time script from 'outside'
- A run-time script can be evaluated in its own independent scope
- Run-time functions can be called from a user function
- Run-time functions can be applied to any frame (relative to the current one)
- Additional variant of ConditionalFilter with single boolean expression
- Fixes a fairly serious bug in the run-time system
- Lightweight plugin extending the standard run-time environment, minimal time and memory overhead
- 100% backwards compatible with existing scripts

GRunT should be useful to anyone who uses the run-time filters, from those who make occasional use of ScriptClip to those who write complex functions based on run-time features (such as Restore24 or MRestore).

Details

The plugin provides extended versions of the following run-time filters:
- ScriptClip
- FrameEvaluate
- ConditionalFilter
- WriteFile
- WriteFileIf

The alternative names GScriptClip, GFrameEvaluate, GConditionalFilter, GWriteFile and GWriteFileIf may also be used. However, if running on version 2.57 or earlier of Avisynth, then only the alternative names may be used.
(This restriction is necessary for technical reasons - sorry about that.)

Each filter is 100% backwards compatible with its standard equivalent, but has two additional optional arguments:
string args: the variables whose values are to be imported into the run-time script, written as a list of names separated by commas. The given variable names are evaluated in the current (compile-time) context, so can include function parameters or local variables.
Each value becomes the initial value of the corresponding variable at each invocation of the run-time script.
bool local: if true, the filter will evaluate its run-time script in a new variable scope, avoiding unintended sharing of variables between run-time scripts.
Default is true if args is also specified, otherwise false (to preserve backwards compatibility).
A short example (based on the original (http://avisynth.nl/index.php/The_script_execution_model/Scope_and_lifetime_of_variables#A_variables_scope_and_lifetime_example)) shows how this greatly simplifies passing function parameters into a run-time script.

function bracket_luma(clip c, float th1, float th2) {
Assert(0 <= th1 && th1 < th2 && th2 <= 255, "Invalid thresholds!")
ScriptClip(c, """
avl = AverageLuma()
avl <= th1 ? last.BlankClip() : avl >= th2 ? last.BlankClip(color=color_white) : last
""", args="th1,th2", local=true)
}
This is much easier than the standard approach of dynamically building the runtime script using string concatenation, or passing the values via global variables.

"I really do not like this global variable business with ScriptClip ..." - stickboy

And because the run-time script is evaluated in its own scope, there is now no problem in calling bracket_luma more than once in the same script (previously the variables th1 and th2 of different instances could interfere with each other).

Elements of the args string can also take the form 'name=expression' - the expression is evaluated in the current context and is used to set the value of the named variable in the run-time script.
Example: args="x, y=n+1, c=c.Trim(2, 0)" will provide values for the variables x, y and c.
Here y need not even exist in the current environment (although x, n and c must).

The plugin also provides the following extensions to the run-time functions (eg AverageLuma):
these functions can now be called inside a user function, when the user function is called from a run-time script
each function has an new optional int argument, which can be used to get the value from another frame, relative to the current one. For example, AverageLuma(-1) returns the value for the previous frame. (No more assigning to current_frame ...)
For added convenience, there is a new variant of ConditionalFilter which takes a single boolean expression instead of three separate parameters as at present. This is useful when the condition to be tested is a compound one or is already available in boolean form. For example,
ConditionalFilter(c, c1, c2, \
"AverageLuma(c1) > AverageLuma() && AverageLuma(c1) > AverageLuma(c2)")
where previously you would have to add (...,"=", "true")

Finally, the plugin fixes a fairly serious bug I discovered in the run-time system.
This fix is needed if you are running a version of Avisynth prior to build 080620 of 2.58.

More detailed documentation and examples are provided in the attached download.

EDIT 27-SEP-08: Updated to v1.0.1 to work with Avisynth 2.5.7

Comments and suggestions would still be very welcome.

martino
9th July 2008, 12:41
Oh my God! I love you!!! Finally I can do something on which I wasted 2 days trying to achieve (and countless headaches)... I think... xD

Gavino
9th July 2008, 13:54
Thanks, martino! I hope you find it solves your problem.

If it does, could you post your resulting script here as an example?
Alternatively, if you still have problems, explain what you're trying to do and I'll see if I can help.

backtohell
20th July 2008, 16:16
i am not an expert in avisynth but still will try your GRt

Thanks Gavino for sharing

Gavino
5th August 2008, 10:00
Previously, I wrote:
For added convenience, there is a new variant of ConditionalFilter which takes a single boolean expression instead of three separate parameters as at present. This is useful when the condition to be tested is a compound one or is already available in boolean form.

I have now noticed that the standard ConditionalFilter, as well as forcing you to use 3 parameters, does not even support all the boolean operators - you can only use "=", "<" or ">", and not "<=", ">=" or "!=". Of course, you can express ">=" by using "<" and switching round the 'then' and the 'else' clips. But GRunT's version (as well as being slightly shorter) allows you to express the condition in the way you choose, whichever is more convenient/natural to you.

MOmonster
25th September 2008, 21:19
@Gavino
I donīt get ScriptClip running.
I get allways the error:
ScriptClip does not have the named argument "show"
I tested your sample scripts and some of my own, but allways the same error (no error without your plugin).
What is wrong?

Gavino
25th September 2008, 21:33
@MOmonster: I'm sorry about this - it is a problem with GRunT and Avisynth v2.5.7 that I am still looking into.

At the moment, the workaround is to use v2.5.8, where it works fine.

MOmonster
25th September 2008, 21:49
Thanks for the fast answer.
I will do some testing when I adapt srestore to avisynth 2.5.8.

Edit:
Another question. Is it possible to shift values without using global values?

Gavino
26th September 2008, 02:09
Is it possible to shift values without using global values?
What do you mean by 'shift values'?
Can you give me an example?

MOmonster
26th September 2008, 08:04
I use global vars for performance reasons.
Instead of something like this:
ScriptClip(last, """
prev = AverageLuma(last.loop(2,0,0))
curr = AverageLuma(last)
next = AverageLuma(last.trim(1,0))
...outputclip...
""")
I can write this:
ScriptClip(last, """
global prev = curr
global curr = next
global next = AverageLuma(last.trim(1,0))
...outputclip...
""")

Gavino
26th September 2008, 11:23
OK, I should have realised you meant that, having seen similar code in MRestore.

The variables in your example don't need to be global, as they are at the outer script-level scope (and hence persistent). But of course, if you need to see them in functions called from within the ScriptClip, they would have to be global (or passed as parameters).

If you are using GRunT, the example could also be written as
ScriptClip(last, """
prev = AverageLuma(-1)
curr = AverageLuma()
next = AverageLuma(1)
...outputclip...
""")

which would be slightly faster than the standard ScriptClip, though clearly still slower than your optimisation.
(As you know, the disadvantage of the optimised version is that it does not support seeking, since it relies on linear access.)

MOmonster
26th September 2008, 12:03
The variables donīt have to be global? I don`t know anymore why I think so.
The seeking problem can be solved with RequestLinear and some additional conditions, so speed is more important for me.

Thanks.

Gavino
26th September 2008, 13:13
The variables donīt have to be global? I don`t know anymore why I think so.
Perhaps you started out thinking (long ago) that each ScriptClip runs in a separate variable scope.

They don't - that's why you can get unexpected interference between different ScriptClip instances, and why GRunT provides the local=true option.

Didée
26th September 2008, 13:33
The variables donīt have to be global? I don`t know anymore why I think so.
Well, I can remember why ... iirc it was Restore24 which introduced this kind of variable-shifting in the conditional env. :)

In the 1st example with 3times AverageLuma, globals are not needed because all 3 variables are computed "now".
In the 2nd example, the globals are needed because only one variable is computed "now" (the "next" one), and the other two are derived from what the conditional environment had computed during processing the previous frame. If you need "now" the content of a variable from one frame before, then you're jumping out of ScriptClip's local context, and need a global variable to "save" that variable's content while stepping from one frame on to the next one.

The variant with independent calculation of all needed variables is of course slower (doing AverageLuma several times), and it's robust against seeking. The variant with globals is faster (only one time AverageLuma), but it's not robust against seeking and needs linear access.

When I did Restore24, the choice was easy:

a) RequstLinear() did not exist yet

b) GRunT did not exist yet

c) R24 uses not only the 3 variables 'previous', 'current', 'next'. Because of its kind of pattern reckognition, it uses six (or seven?) of such variables: previous(3), previous(2), previous(1), current, next(1), next(2). (Not sure anymore if the released versions used next(3), too.)

It's obvious why the variant with globals was chosen. The choices were to do only one time AverageLuma for every frame, or to do six (seven?) times AverageLuma for every single frame (with all except one of them being basically superfluous.)
So I went for the route that requires linear access, but does not do 500% (600%) of re-doing frame sampling operations that already had been done.


It's been quite some time since then. Knowledge/practice and available tools have noticeably developed in the meantime. I'm really looking forward to see Mrest..., erh, SRestore to do right what I had b0rked back then. :)

(Lord, make I don't ever have to touch Restore24 again!)

MOmonster
26th September 2008, 14:00
Perhaps you started out thinking (long ago) that each ScriptClip runs in a separate variable scope.

They don't - that's why you can get unexpected interference between different ScriptClip instances, and why GRunT provides the local=true option.

Yes, this and the need to initialise the variables are the reasons.
This script indeed works well:
ScriptClip (last, """ cfr=current_frame
now = cfr<2 ? 0 : next
next = AverageLuma(last)
last.subtitle(string(now)).subtitle(string(next),y=16)""")
I will test GRunT soon. Maybe next srestore release wonīt use global variables anymore.

@Didee
Yes I remember, first Cdeint releases use practical the same variables as restore24.:eek:

Gavino
26th September 2008, 14:06
In the 2nd example, the globals are needed because only one variable is computed "now" (the "next" one), and the other two are derived from what the conditional environment had computed during processing the previous frame. If you need "now" the content of a variable from one frame before, then you're jumping out of ScriptClip's local context, and need a global variable to "save" that variable's content while stepping from one frame on to the next one.
No, as I pointed out, and as MOmonster has demonstrated, there is no 'local' context in (the builtin) ScriptClip - it's all at outer script level.

Didée
26th September 2008, 14:41
Its probably related to the fact that R24 uses user defined functions within scriptclip -- Like

function ShiftBackVars() { do_the_shifting_stuff }
...
ScriptClip( c, "ShiftBackVars()" )

When referring to conditional variables in the do_the_shifting_stuff section, they have to be global, don't they?
(Sorry, I'm currently not in the mental "flow" of ScriptClip & Co. ... usually need some run-up when I have to.)

If so, well ... I would NEVER have done R24 without that style of using (lots of) defined functions, called from within the conditional envoronment. That way it was easily possible to get a reasonable level of "modularisation". When having to put all that stuff within one gigantic

ScriptClip(c, """
...
put several hundred lines of code here ...
...
""" )

then Restore24 never would have come into existance. (Which would be a pity ... think the butterfly effect.;) )

Howeveritmightbe. The important point is you guys now are heading to do it right, without me needing to get knots in the brain. :)

Gavino
26th September 2008, 15:18
When referring to conditional variables in the do_the_shifting_stuff section, they have to be global, don't they?
Yes, to be visible in a function, they do need to be global, as I mentioned above.
And if you're writing to them, you don't have the option of passing them as parameters.
I would NEVER have done R24 without that style of using (lots of) defined functions, called from within the conditional envoronment. That way it was easily possible to get a reasonable level of "modularisation".
GRunT helps in that respect because it allows calling of the run-time functions (such as AverageLuma) from within a user function.

Gavino
27th September 2008, 17:53
Version 1.0.1: Bug fix for Avisynth 2.5.7

As MOmonster reported above (#6), and also as reported here, there are problems using GRunT with Avisynth v2.5.7. In fact, I'm embarrassed to say that it just doesn't work on anything prior to v2.5.8. :(

I have produced a new version (GRunT 1.0.1) that solves the problem. The fix requires users running on 2.5.7 (and earlier) to use alternative names for the run-time filters (GScriptClip instead of ScriptClip and so on). The run-time functions, such as AverageLuma, are not affected.

Users already on 2.5.8 can continue to use the old names - the alternative names may also be used if preferred.

Go to the first post in this thread to download the new version.
Updated documentation, reflecting this change and minor editing, is also included.

MOmonster
28th September 2008, 11:58
Thanks for update, but I canīt download it.

Gavino
28th September 2008, 15:00
@MOmonster: I assume your problem was that the attachment was still awaiting approval. It should be OK now.

MOmonster
28th September 2008, 17:50
Yes, thanks.

canuckerfan
17th September 2009, 18:28
Currently I'm using AviSynth 2.5.8's built-in scriptclip and conditionalreader. anyway I can adapt this code so that I can use Gavino's versions?

Filtered = RemoveNoiseMC(rdlimit=18,rgrain=1,denoise=0,sharp=true)
ScriptClip("ABC1 ? Filtered : Last")
ConditionalReader("seriously.txt","ABC1",false)

Gavino
17th September 2009, 19:06
If you just load the GRunT plugin (or install it in the plugin folder), it will use my version automatically (if running on Avisynth 2.58).

For your example, where the run-time script is very simple, you wouldn't actually gain very much.
However, it would allow you to replace the ScriptClip call by
ConditionalFilter(Filtered, last, "ABC1")
which would be slightly faster.

Even without GRunT, it could also be changed, although with the standard ConditionalFilter you would have to write
ConditionalFilter(Filtered, last, "ABC1", "==", "true")

GRunT does not have its own version of ConditionalReader, so you would continue to use the standard version.

canuckerfan
17th September 2009, 20:03
^Thank you :)

Forensic
13th March 2016, 04:56
I have tried everything that I can think of. What I need is a single pass solution to step through a video and only retain frames where AverageLuma(a)>16 (IOW, retain only non-blank frames). Scriptclip can step through the video and conditionally test the AverageLuma value, but I can't get the new video out of the Scriptclip, even with GRunT. Any suggestions?

StainlessS
13th March 2016, 06:26
Yo dude, long time no see.

So far as I know, Scriptclip must produce same number of output frames as input. (+ same size, colorspace etc).
So 1 single pass solution is unlikely. Although could do it as realtime 1st pass, with auto second pass.
No idea what IOW means.

But see here:- http://forum.doom9.org/showthread.php?t=172904

You might want to use YPlaneMinMaxDifference (Threshold=eg 0.2) to establish 'blank frame', and YPlaneMin or AverageLuma to establish level.

StainlessS
13th March 2016, 07:05
Something like this (writing frames to reject in realtime pass, as likely fewer than writing all selected frames)


Avisource("D:\V\StarWars.avi").Trim(0,10000)

GSCript("""
Function Test(Clip c, int n,int v1,Float th,String File,Bool Show) {
c
mx = RT_YPlaneMax(n,Threshold=th)
if(mx < v1) {
RT_WriteFile(File,"%d",n,Append=True) # Write unwanted frames (fewer than wanted)
(Show) ? RT_SubTitle("%d] mx=%d - DELETING",n,mx) : NOP
} else {(Show) ? RT_SubTitle("%d] mx=%d",n,mx) : NOP}
Return Last
}
""")

OutFile = "Frames.txt"
RT_FileDelete(OutFile) # Delete existing
V1 = 18 # Was not deleteing anything for me at 16
th = 0.2 # Avoid 0.2% of noise above v1
Show=True # CHANGE to FALSE when happy with numbers
SSS = "Test(current_frame,V1,th,OutFile,Show)"
ARGS= "v1,th,OutFile,Show"
ScriptClip(SSS,args=ARGS,after_frame=True)

(!Show) ? ForceProcessAVI() : NOP # Force Pass 1 (From TWriteAVI v2.0) ie force writing of frames file
(!Show&&Exist(OutFile)) ? RejectRanges(Cmd=Outfile) : NOP # If Outfile does not exist, then none to Reject.

Return last

Function RejectRanges(clip c,String "SCmd",String "Cmd",Bool "TrimAudio",Float "FadeMS") {
# RejectRanges() by StainlessS. Required:- FrameSel, Prune, RT_Stats
# Wrapper to delete frames/ranges along with audio, can supply frames/ranges in SCmd string And/Or Cmd file.
# The wrapper makes for easier usage of Prune() which supports up to 256 input clips, but requires a clip index,
# eg '3, 100,200' would specify clip 3, range 100 to 200. The wrapper does away with the necessity for the clip index as we
# are only using a single clip here. Prune also does not have a 'reject' arg to delete specified frames rather than select them,
# this wrapper also converts a list of frames to delete into a list of frames to select so that we can use Prune and its audio
# capability.
#
# SCmd: Frames/Ranges specified in String (Frames/Ranges either Chr(10) or ';' separated, infix ',' specifies range, eg 'start,end').
# Cmd: Frames/Ranges specified in file (one frame/range per line, comments also allowed, see FrameSel for Further info).
# TrimAudio:
# True(default), deletes audio belonging to deleted frames
# False, returns original audio, probably out of sync.
# FadeMS: (default 1.0 millisec). Linear Audio Fade duration at splices when TrimAudio==true, 0 = dont fade (might result in audio 'clicks/cracks').
c
TrimAudio=Default(TrimAudio,True) # default true trims audio, false returns original audio (audiodubbed, as Framesel returns no audio)
FadeMS=Float(Default(FadeMS,1.0)) # 1 millisecond linear fadeout/fadein at splices
PruneCmd = (TrimAudio) ? "~Prune_"+RT_LocalTimeString+".txt" : ""
(!TrimAudio)
\ ? FrameSel(scmd=SCmd,cmd=Cmd,reject=true)
\ : FrameSel_CmdReWrite(PruneCmd,scmd=SCmd,cmd=Cmd,reject=true,Prune=True,range=true)
(TrimAudio) ? Prune(Cmd=PruneCmd,FadeIn=True,FadeSplice=True,FadeOut=True,Fade=FadeMS) : NOP
# If TrimAudio==true then delete Prune temp file, Else restore original Audio to the now audio-less clip
(TrimAudio)
\ ? RT_FileDelete(PruneCmd)
\ : (c.HasAudio) ? AudioDub(c) : NOP
Return Last
}


and to view rejected frames only

Avisource("D:\V\StarWars.avi").Trim(0,10000)

OutFile = "Frames.txt"
SelectRanges(Cmd=Outfile) # or change to RejectRanges to view kept after 1st pass script
Return last

# From Prune [req FrameSel]
Function SelectRanges(clip c,String "SCmd",String "Cmd",Bool "TrimAudio",Float "FadeMS",Bool "Ordered") {
# SelectRanges() by StainlessS. Required:- FrameSel, Prune, RT_Stats
# Wrapper to Select frames/ranges along with audio, can supply frames/ranges in SCmd string And/Or Cmd file.
# The wrapper makes for easier usage of Prune() which supports up to 256 input clips, but requires a clip index,
# eg '3, 100,200' would specify clip 3, range 100 to 200. The wrapper does away with the necessity for the clip index as we
# are only using a single clip here.
#
# SCmd: Frames/Ranges specified in String (Frames/Ranges either Chr(10) or ';' separated, infix ',' specifies range, eg 'start,end').
# Cmd: Frames/Ranges specified in file (one frame/range per line, comments allowed, see FrameSel for Further info).
# *** NOTE ***, If both Cmd and SCmd supplied AND Ordered == False, then will process Cmd file and then SCmd string afterwards, ie
# Will select ranges in Cmd file and in order specified (rather than auto ordering ranges) and then append ranges specified in
# SCmd string (and in order specified).
# TrimAudio:
# True(default), selects audio belonging to selected frames/ranges
# False, returns original audio, probably out of sync (maybe totally out of whack if Ordered == false and selected ranges out of order).
# FadeMS: (default 1.0 millisec). Linear Audio Fade duration at splices when TrimAudio==true, 0 = dont fade (might result in audio 'clicks/cracks').
# Ordered:
# True(default), all frames/ranges are returned in sequencial order. Any frame specified more than once will return only 1 instance.
# False, All frames/Ranges are returned in specified order, Cmd processed first and then SCmd. Frames/ranges specified more than once
# will return multiple instances. Allows out-of-order trimming of clip, eg re-sequencing of scenes in movie.
#
# Does not make much sense to select individual frames with audio, best used with ranges.
# Will coalesce individually selected adjacent frames/ranges before any Fade, ie only audio fade where sensible to do so.
# TrimAudio==false with non Ordered selection will result in completely out of sync audio.
c
TrimAudio=Default(TrimAudio,True) # default true trims audio, false returns original audio (audiodubbed, as Framesel returns no audio)
FadeMS=Float(Default(FadeMS,1.0)) # 1 millisecond linear fadeout/fadein at splices
Ordered=Default(Ordered,True) # True (default) frames/ranges will be Ordered and selected only once even if specified more than once.
# False, frames/ranges returned in specified order, Cmd processed 1st and then SCmd.
PruneCmd = (TrimAudio) ? "~Prune_"+RT_LocalTimeString+".txt" : ""
(!TrimAudio)
\ ? FrameSel(scmd=SCmd,cmd=Cmd,Ordered=Ordered)
\ : FrameSel_CmdReWrite(PruneCmd,scmd=SCmd,cmd=Cmd,Ordered=Ordered,Prune=True,range=true)
(TrimAudio) ? Prune(Cmd=PruneCmd,FadeIn=True,FadeSplice=True,FadeOut=True,Fade=FadeMS) : NOP
# If TrimAudio==true then delete Prune temp file, Else restore original Audio to the now audio-less clip
(TrimAudio)
\ ? RT_FileDelete(PruneCmd)
\ : (c.HasAudio) ? AudioDub(c) : NOP
Return Last
}


You can rearrange logic however you will, above was just a quick knock-up.

Requires RT_Stats, FrameSel, Prune, and TWriteAVI v2.0. http://forum.doom9.org/showthread.php?t=172837&highlight=twriteavi

EDIT:: Oops, and GSCript and Grunt too.

EDIT: This is more like what you asked for, set Show = False to do it for real, when true only shows metric
On my clip it was not deleting anything as all frames were above Ave luma 16.

Avisource("D:\V\StarWars.avi").Trim(0,10000)

GSCript("""
Function Test(Clip c, int n,Float th,String File,Bool Show) {
c
Ave = RT_AverageLuma(n)
if(Ave < th) {
RT_WriteFile(File,"%d",n,Append=True) # Write unwanted frames (fewer than wanted)
(Show) ? RT_SubTitle("%d] %f - DELETING",n,Ave) : NOP
} else {(Show) ? RT_SubTitle("%d] %f",n,Ave) : NOP}
Return Last
}
""")

OutFile = "Frames.txt"
RT_FileDelete(OutFile) # Delete existing
th = 18.0 # Keep >= 18.0
Show = True # CHANGE to FALSE when happy with numbers
SSS = """Test(current_frame,th,OutFile,Show)"""
ARGS="th,OutFile,Show"
ScriptClip(SSS,args=ARGS,after_frame=True)

(!Show) ? ForceProcessAVI() : NOP # Force Pass 1 (From TWriteAVI v2.0) ie force writing of frames file
(!Show&&Exist(Outfile)) ? RejectRanges(Cmd=Outfile) : NOP # If Outfile does not exist, then none to Reject.

Return last

Function RejectRanges(clip c,String "SCmd",String "Cmd",Bool "TrimAudio",Float "FadeMS") {
# RejectRanges() by StainlessS. Required:- FrameSel, Prune, RT_Stats
# Wrapper to delete frames/ranges along with audio, can supply frames/ranges in SCmd string And/Or Cmd file.
# The wrapper makes for easier usage of Prune() which supports up to 256 input clips, but requires a clip index,
# eg '3, 100,200' would specify clip 3, range 100 to 200. The wrapper does away with the necessity for the clip index as we
# are only using a single clip here. Prune also does not have a 'reject' arg to delete specified frames rather than select them,
# this wrapper also converts a list of frames to delete into a list of frames to select so that we can use Prune and its audio
# capability.
#
# SCmd: Frames/Ranges specified in String (Frames/Ranges either Chr(10) or ';' separated, infix ',' specifies range, eg 'start,end').
# Cmd: Frames/Ranges specified in file (one frame/range per line, comments also allowed, see FrameSel for Further info).
# TrimAudio:
# True(default), deletes audio belonging to deleted frames
# False, returns original audio, probably out of sync.
# FadeMS: (default 1.0 millisec). Linear Audio Fade duration at splices when TrimAudio==true, 0 = dont fade (might result in audio 'clicks/cracks').
c
TrimAudio=Default(TrimAudio,True) # default true trims audio, false returns original audio (audiodubbed, as Framesel returns no audio)
FadeMS=Float(Default(FadeMS,1.0)) # 1 millisecond linear fadeout/fadein at splices
PruneCmd = (TrimAudio) ? "~Prune_"+RT_LocalTimeString+".txt" : ""
(!TrimAudio)
\ ? FrameSel(scmd=SCmd,cmd=Cmd,reject=true)
\ : FrameSel_CmdReWrite(PruneCmd,scmd=SCmd,cmd=Cmd,reject=true,Prune=True,range=true)
(TrimAudio) ? Prune(Cmd=PruneCmd,FadeIn=True,FadeSplice=True,FadeOut=True,Fade=FadeMS) : NOP
# If TrimAudio==true then delete Prune temp file, Else restore original Audio to the now audio-less clip
(TrimAudio)
\ ? RT_FileDelete(PruneCmd)
\ : (c.HasAudio) ? AudioDub(c) : NOP
Return Last
}

Forensic
13th March 2016, 08:25
StainlessS. Thank you for this. I have been quite busy with my lab and creating FOSS (Free Open Source Software) that now has over 30,000 downloads and is relied upon by law enforcement worldwide. This would never have been possible without amazing programmers like you and Gavino. Your work has helped to solve crimes and has saved lives!!!! As for this script, it requires two functions I didn't have. I found "ForceProcessAVI" in your TWriteAVI_dll DLL but where do I find "RejectRanges"? Also IOW is a mostly American expression short for "In Other Words" which just means that something is being restated in another way.

StainlessS
13th March 2016, 08:40
Select/Reject Ranges is in the Prune AVS directory. (also requires FrameSel to do the clever Select to Reject conversion for Prune).

IOW, I thought it could not possibly stand for "Isle Of White" :)

Prune: http://forum.doom9.org/showthread.php?t=162446&highlight=Prune
FrameSel: http://forum.doom9.org/showthread.php?t=167971&highlight=FrameSel

EDIT: Or Select/Reject Ranges both posted in above code snippits, but still need the Prune and FrameSel plugins.

Gavino
13th March 2016, 09:33
I have tried everything that I can think of. What I need is a single pass solution to step through a video and only retain frames where AverageLuma(a)>16 (IOW, retain only non-blank frames). Scriptclip can step through the video and conditionally test the AverageLuma value, but I can't get the new video out of the Scriptclip, even with GRunT. Any suggestions?
As an alternative to StainlessS's solution, you could use my DeleteFrames() function, like this:
DeleteFrames("AverageLuma() <= 16")

(It works out at compile-time which frames to delete, so the script will take a noticeable time to load for a long video.)

BTW Nice to hear my code is being used in real practical applications!

StainlessS
13th March 2016, 09:37
Yes indeed big G.
Although, an advantage of the frames file method is that it is easily repeatable without a second compile time run.
Also, RT_AverageLuma is faster than built-in so would cut down overhead on longer clips.

Forensic
13th March 2016, 18:19
Thank you StainlessS & Gavino. DeleteFrames("RT_AverageLuma() <= 16") works like a charm. Simple single pass, and just what I wanted! Much appreciated.

yesmanitsbearman
16th May 2016, 21:26
Hey,

Does anyone have a x64 version of this? Need it for srestore and it's the only one I can't find a x64 build for.

yesmanitsbearman
17th May 2016, 20:02
I've taken the liberty to compile the .dll myself. Had to also change some stuff to make it compile with latest avs+ headers. Hopefully this comes useful to someone. srestore now tested working on a fully x64 setup.

here it is (http://rgho.st/7blM5TMJP)

real.finder
18th May 2016, 03:24
I've taken the liberty to compile the .dll myself. Had to also change some stuff to make it compile with latest avs+ headers. Hopefully this comes useful to someone. srestore now tested working on a fully x64 setup.

here it is (http://rgho.st/7blM5TMJP)

can we have one work with 2.5 x64?

Reel.Deel
18th May 2016, 03:38
can we have one work with 2.5 x64?

Why? 2.5 is outdated and there's no reason to be using it.

... Perhaps someone can add it to the wiki.

Thanks for the build, I'll add it to the wiki.

real.finder
18th May 2016, 03:49
Why? 2.5 is outdated and there's no reason to be using it.


yes, but I use it some times (for setmtmode) and 2.5 plugin work with avs+ too

yesmanitsbearman
20th May 2016, 10:36
I could never say no to you for all the times you helped me :)

here (http://rgho.st/6Zst4FPkx)

Not tested though. Let me know if it's fine.

real.finder
20th May 2016, 15:51
I could never say no to you for all the times you helped me :)

here (http://rgho.st/6Zst4FPkx)

Not tested though. Let me know if it's fine.

thank you

with srestore(frate=23.976) I get http://i.imgur.com/yHVtNvM.png in the top of video frame

but with same GrunT-x64-25 in avs+ it's fine!

yesmanitsbearman
20th May 2016, 22:21
I am guessing something to do with avisynth.h I used. Perhaps someone can point a stable 2.5 repo to use. There's loads of repos around with various forks and the include file is different all over.

real.finder
24th May 2016, 10:38
I am guessing something to do with avisynth.h I used. Perhaps someone can point a stable 2.5 repo to use. There's loads of repos around with various forks and the include file is different all over.

I think this is the reason


static const AVS_Linkage* AVS_linkage = nullptr;

extern "C" __declspec(dllexport) const char* __stdcall
AvisynthPluginInit3(IScriptEnvironment* env, AVS_Linkage* vectors) {
AVS_linkage = vectors;

StainlessS
24th May 2016, 17:22
yesmanitsbearman,
v2.5 header has this at beginning (after copyright)

#ifndef __AVISYNTH_H__
#define __AVISYNTH_H__

enum { AVISYNTH_INTERFACE_VERSION = 3 };


The header snippet given by Real.Finder containing "AvisynthPluginInit3" is one of the v2.6 headers.

EDIT: Here link to ApparentFPS dll+source with both v2.58 header and STANDARD Avisynth v2.6 FINAL:- http://www.mediafire.com/download/x4e8ad215xd8z04/ApparentFPS_25%2626_dll_v1.03_20151217.zip

~148KB

EDIT: where AVISYNTH_INTERFACE_VERSION

=4, does not exist
=5, OLD Pre-final v2.6, will error/crash if dll compiled with this and used via v2.6Alpha3 and prior (I think, anyway, dont use).
=6, v2.6 FINAL.

real.finder
10th June 2016, 18:49
I am guessing something to do with avisynth.h I used. Perhaps someone can point a stable 2.5 repo to use. There's loads of repos around with various forks and the include file is different all over.

ok, it's avisynth64_8-29-10 fault, avisynth64_4-16-10 work fine

real.finder
19th January 2018, 01:12
hi Gavino

is there some way to avoid crash in theses cases with GRunT?


function SFrameBlendX(clip C, int "blendfactor", string "blend_mode", float "blend_opacity")
{
global SFrameBlendX_blend_mode=blend_mode
global SFrameBlendX_blend_opacity=blend_opacity
C
limit = (blendfactor<=0) ? 1 : Max(1, FrameCount/blendfactor)
global SFrameBlendX_limit = limit
FrameCount!=1 ? ScriptClip("""
try{bb=isclip(b)} catch(error_msg) {bb=false}
b = bb ? SFrameBlendX_limit>1 ? Overlay(b.Loop(2,0,0),last,opacity=float(SFrameBlendX_limit)/FrameCount) : b.Loop(2,0,0) : nop()
b = bb ? b.Overlay(last, mode=SFrameBlendX_blend_mode, opacity=SFrameBlendX_blend_opacity) : last
return b
""") : last
}


SFrameBlendX(5,"lighten")

more information https://forum.doom9.org/showthread.php?p=1829149#post1829149

it use same method of https://forum.doom9.org/showthread.php?p=1188377#post1188377

but the problem seems that the frames from previous didn't ever clean up so it will crash after use all ram, the problem can't be seen in Srestore or any MOmonster function since they not do this with clips so seems ram will not be full easily

so, is there any method/idea/update to clean/limit theses data from previous frames process?

like


GScriptClip(last, """
global prev = curr
global curr = next
global next = AverageLuma(last.trim(1,0))
...outputclip...
""", limit=5)


or


GScriptClip(last, """
global prev = curr
global curr = next
global next = AverageLuma(last.trim(1,0)).runtimecleanup(5)
...outputclip...
""")


or anything suitable

StainlessS
19th January 2018, 04:23
Presumably, you are relying on 'b' being at top script level (never did get the hang of that), and so is sort of Global [or maybe 'common'].
I tried removing Globals and using Grunt Args instead, but it stopped working, seemed to interfere with setting non global b, and
was not seen as set on subsequent frames.

Nope, cant do it, cannot improve on yours. Tried a few things, no go. I'm quite mystified about how it works :confused:
Only suggestion is that blendfactor cannot be optional. [EDIT: Remove quotes]

I dont think you can 'clean up', even if you use blankclip and trim [ie create an additional new clip] while keeping a single previous frame, that frame is reliant on ones prior to itself, and they are reliant upon ones prior to them, etc.
With SRestore, etc, I would guess that output is reliant upon [and constructed from] only input frames [or frames synthesized from input frames] , here you are reliant upon all previous output frames.

EDIT: Working on Multi-Instance version function, that might work, but will reset if user jumps about.

Gavino
19th January 2018, 20:40
I tried removing Globals and using Grunt Args instead, but it stopped working, seemed to interfere with setting non global b, and
was not seen as set on subsequent frames.
I think that could be fixed by setting local=false (default is 'true' when args is used), but I don't think it would fix real.finder's original problem.

real.finder
20th January 2018, 02:41
I think that could be fixed by setting local=false (default is 'true' when args is used), but I don't think it would fix real.finder's original problem.


function SFrameBlendX(clip C, int "blendfactor", string "blend_mode", float "blend_opacity")
{
C
limit = (blendfactor<=0) ? 1 : Max(1, FrameCount/blendfactor)
FrameCount!=1 ? ScriptClip("""
try{bb=isclip(b)} catch(error_msg) {bb=false}
b = bb ? limit>1 ? Overlay(b.Loop(2,0,0),last,opacity=float(limit)/FrameCount) : b.Loop(2,0,0) : nop()
b = bb ? b.Overlay(last, mode=blend_mode, opacity=blend_opacity) : last
return b
""",args="blend_mode, blend_opacity, limit", local=false) : last
}


yes it work like before and it not fix the ram problem

StainlessS
20th January 2018, 11:55
This works for a little bit longer (orig 825 frame, below 845 frames) before crash.


Function SBX_MI(clip c,int Factor, string "Mode", float "Opacity") {
c
myName = "SBX_MI: "
Mode = Default(Mode,"lighten")
Opacity = Default(Opacity,1.0)
Limit = (Factor<=0) ? 1 : Max(1, FrameCount/Factor)
FuncS="""
Function Fn@@@(clip c,Int factor,String mode,Float opacity,Int limit) {
n=current_frame
if(Prev@@@ != n) {
c
cf=c.Trim(n,-1) # current frame
if(Prev@@@ != n-1) {
Global B@@@ = cf # First Frame OR User Jumping, Init/Reset to current frame
} else {
# limit>1 ? Overlay(b.Loop(2,0,0),last,opacity=float(limit)/FrameCount) : b.Loop(2,0,0)
if(limit > 1) {
Global B@@@ = Overlay(B@@@,cf, opacity=float(limit)/FrameCount)
} # Else, B@@@ = Previous B@@@
# b.Overlay(last, mode=mode, opacity=opacity)
Global B@@@=Overlay(B@@@,cf,mode=Mode,opacity=Opacity) # HOW do we cache only this frame & release all others ?
}
Global Prev@@@=n # REM for next time
} # Else, Cache failure, repeat request for current frame, just return same as last time
Return B@@@
}
#######################################
# Unique Global Variables Initialization
#######################################
Global Prev@@@=-666
#######################################
# Unique Runtime Call, GScriptClip must be a one-liner:
#######################################
ARGS = "Factor,Mode,Opacity,Limit"
c.GScriptClip("Fn@@@(last, "+ARGS+")", local=true, args=ARGS)
"""
#######################################
# Unique Identifier Definition
#######################################
GIFunc="SBX_MI" # Function Name, Supply unique name for your multi-instance function.
GIName=GIFunc+"_InstanceNumber" # Name of the Instance number Global
RT_IncrGlobal(GIName) # Increment Instance Global (init to 1 if not already exists)
GID = GIFunc + "_" + String(Eval(GIName))
InstS = RT_StrReplace(FuncS,"@@@","_"+GID)
# RT_WriteFile("DEBUG_"+GID+".TXT","%s",InstS)
FrameCount!=1 ? GScript(InstS) : Last
Return Last
}

Client

aviSource("Parade.avi").trim(500,0)
SBX_MI(5)



EDIT: Made same mistake as in original, "Factor" was optional, removed quotes above.
EDIT: Perhaps Loop() added somewhere might delay crash ??? [EDIT: Nope, dont seem to work]
EDIT: Oops, added Int to Fn@@@(Int Limit).
EDIT: Small mods.

EDIT:
I think that could be fixed by setting local=false (default is 'true' when args is used), but I don't think it would fix real.finder's original problem.
I thought that I'de tried that, maybe I just tried without Local arg, perhaps Local is default true.
EDIT: Yep, default for Local=False, unless Args used, in which case default is True. (I should have read above quote more closely).
EDIT: I did above test with AVS Standard, without AVS+ Trim() issue ongoing in Devs forum, Avisynth+ Thread. https://forum.doom9.org/showthread.php?p=1830902#post1830902
EDIT: Above, we are using Global B@@@ as a single frame accumulator for all previous frames overlayed, we need some method to cache only that frame, and release all prevous ones.

pinterf
20th January 2018, 14:43
Maybe it's not the frames but the string resources that will never get freed up. Shorten everything in the runtime section and see if it crashes later.