Log in

View Full Version : Why there's no "for"?


nji
1st August 2025, 13:36
With respect to my other thread (https://forum.doom9.org/showthread.php?t=186445)
I would like to ask why AVS doesn't provide a "for" construct.

If "for" was available e.g. the task of swaping n pairs of frames,
then leaving one frame as is, then n pairs swapping again etc.
would be next to "Hello world!".

Without "for" it could be simulated with recursion
(complex, less comprehensible, using more ressources (time and mem (stack)),
or do it with temporary clips filled by complex selecting.

Selur
1st August 2025, 13:44
You could use Avisynth with GScript (https://forum.doom9.org/showthread.php?t=147846) (or Vapoursynth instead of Avisynth).

wonkey_monkey
1st August 2025, 13:54
There is a "for", but it's just not the sort of "for" that you can loop over the frames of a video with. AviSynth just doesn't really work like that. You can kind of fudge it with GScript, but it's not very efficient because it's just not really how AviSynth was designed to be used.

StvG
1st August 2025, 14:00
Yes, there is built in "for" (as already said) (not GScript).
Yes, it can be used to loop over video frames (run time filters abusing).

StainlessS
1st August 2025, 15:27
Dont think "for" has yet been properly doc'ed for Avs+ yet.
Can read docs for GScript() for.

nji
1st August 2025, 17:06
Sorry, but I've to state that up to this point this thread is not much more than a source of confusion.

The answers are all contraditory, up to deny that there is no "for".

But I got a notion: I'm talking about AVS, not AVS+.

Though obviously not properly doc'ed it seems to be available in AVS+ (?).
But I assume with the same drawbacks why it hasn't been provided in AVS.
However maybe everything is completely different...

StainlessS
1st August 2025, 17:22
There is no 'for' in Avs standard, but there is in Avs+, and is similar to GScript 'for'. [so see GScript docs]
Avs+ 'for' same drawbacks as Gscript 'for'.

nji
1st August 2025, 17:32
Thank you for the clear statement! :)

Still there remains the main question, why AVS doesn't provide "for" and AVS+ only with AVS' drawbacks.

If you take the example of my mentioned other thread (https://forum.doom9.org/showthread.php?p=2021313#post2021313)
there are two temporary clips built,
then interlaced in a third one
to return that.

It's hard having to use these tools when building an alg...

StainlessS
1st August 2025, 17:51
To process clip using for, would need to be able to hold ENTIRE result clip, uncompressed, in memory.
eg, swap even odd frames, only

Colorbars.killaudio.showframeNumber.trim(0,-100) # 100 frames
C = 0 # dummy clip
for(i=0,Last.FrameCount-1) {
IsOdd = i % 2 != 0
if(IsOdd) { # swap even/odd
n = i - 1 # get different frame
} else {
n = i + 1 # get different frame
}
Frame = Last.Trim(n, -1) # -1 == single frame from n
C = (C.IsClip) ? C ++ Frame : Frame
}
Last = C # clip C/Last is stored ENTIRELY in memory
Return Last




Using ScriptClip, where entire clip NOT held in memory. Uses normal-ish frameserving mode (not iteration).

Colorbars.killaudio.showframeNumber.trim(0,-100) # 100 frames
SSS="""
IsOdd = current_frame % 2 != 0
if(IsOdd) { # swap even/odd
current_frame = current_frame - 1 # Avisynth current_frame kludge to get different frame
} else {
current_frame = current_frame + 1 # Avisynth current_frame kludge to get different frame
}
Return Last.Trim(current_frame,-1) # Return the frame number as held in current_frame
"""
Scriptclip(SSS)

EDIT: Above, both scripts tested, fixed.

EDIT: 'Secret' current_frame variable, is really only meant to be used by the AVS system, and has never been
sanctioned for 'alternative' use by the developers. Is meant to be used by runtime scripts (via eg Scriptclip),
where current_frame variable is set by Avs system just prior to entering a runtime script (scriptclip), so that
runtime functions (eg AverageLuma) know which frame is supposed to be tested.
This is an example of a kludge to allow a script to test average luma of frame 0 by naughty kludging of current_frame.


Avisource("...")
current_frame = 0 # EDIT: if comment this line out, then below AverageLuma call will FAIL
AL0 = AverageLuma() # test averagluma at frame 0 ONLY.
Subtitle("Frame 0 averageluma = " + String(AL0) )
Return Last