Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 31st January 2014, 06:38   #1  |  Link
shakana
Registered User
 
Join Date: Jan 2014
Posts: 4
Using Global Variables & ScriptClip

Sample Code
function a(bool isGoodFrame)
{
goodFrame = isGoodFrame ? current_frame: goodFrame
c = Subtitle(String(goodFrame))
}

global goodFrame = 0
source = avisource("testclip.avi")
ScriptClip(source, "a(isGoodFrame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)

In short, this sample code reads a file indicating if a frame looks good or not. It sets goodFrame to the frame number of the last good frame.

How do I get goodFrame to hold its value across frames. It always gets set back to 0.
shakana is offline   Reply With Quote
Old 31st January 2014, 11:26   #2  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
The basic problem is that the goodFrame you are setting in the function is a local variable, so the global variable is never updated and remains zero. The assignment should be:
global goodFrame = isGoodFrame ? current_frame: goodFrame

However, there are other errors in the posted script which would stop it getting that far, so I assume it is not what you used originally.

In the posted script, ScriptClip() is ignored, since ConditionalFilter is called on the original source.
Also, the function a has no clip input and does not return a result.
In addition - unless you are using GRunT - current_frame must be passed explicitly to the function, since it is a local variable in the standard Avisynth run-time.

Here is a fully working version of your script:
Code:
function a(clip c, bool isGoodFrame, int current_frame)
{
  global goodFrame = isGoodFrame ? current_frame: goodFrame
  return Subtitle(c, String(goodFrame))
}

global goodFrame = 0
source = avisource("testclip.avi")
source = ScriptClip(source, "a(isGoodFrame, current_frame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)

# alternatively, replace last two lines by:
# ScriptClip(source, "a(isGoodFrame, current_frame)")
# ConditionalReader("dataFile.txt", "isGoodFrame", false)
__________________
GScript and GRunT - complex Avisynth scripting made easier

Last edited by Gavino; 31st January 2014 at 12:48. Reason: add alternative for last two lines
Gavino is offline   Reply With Quote
Old 31st January 2014, 14:27   #3  |  Link
shakana
Registered User
 
Join Date: Jan 2014
Posts: 4
Using Global Variables & ScriptClip

Quote:
Originally Posted by Gavino View Post
The basic problem is that the goodFrame you are setting in the function is a local variable, so the global variable is never updated and remains zero. The assignment should be:
global goodFrame = isGoodFrame ? current_frame: goodFrame

However, there are other errors in the posted script which would stop it getting that far, so I assume it is not what you used originally.

In the posted script, ScriptClip() is ignored, since ConditionalFilter is called on the original source.
Also, the function a has no clip input and does not return a result.
In addition - unless you are using GRunT - current_frame must be passed explicitly to the function, since it is a local variable in the standard Avisynth run-time.

Here is a fully working version of your script:
Code:
function a(clip c, bool isGoodFrame, int current_frame)
{
  global goodFrame = isGoodFrame ? current_frame: goodFrame
  return Subtitle(c, String(goodFrame))
}

global goodFrame = 0
source = avisource("testclip.avi")
source = ScriptClip(source, "a(isGoodFrame, current_frame)")
ConditionalReader(source, "dataFile.txt", "isGoodFrame", false)

# alternatively, replace last two lines by:
# ScriptClip(source, "a(isGoodFrame, current_frame)")
# ConditionalReader("dataFile.txt", "isGoodFrame", false)
Perfect! I didn't realize that I must declare goodFrame as global in multiple place.

thanks.
shakana is offline   Reply With Quote
Old 31st January 2014, 19:12   #4  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
It's not so much 'declaring' it global in multiple places.
It's that every assignment to a global variable (even if all in the same scope) must include the keyword 'global'. Otherwise, the variable to be written will be taken as local, as global and local variables of the same name can co-exist.
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Reply

Tags
conditionalreader, global variables, scriptclip

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:06.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.