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 24th October 2013, 18:19   #1  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
Variables that survive AVIsynth sessions

I need a script to write several string variables once to a file (e.g. c:\test\temp.ini) and then another script that can be run later to read that file and import each variable back into my running scripts (e.g. size=........). I will be using this as a type of Global variable that survives sessions of AVIsynth 2.58
I think this can be done in script (WriteFile ??), but I can't figure out how. There may even be a plugin to ease the process. Any ideas?
Forensic is offline   Reply With Quote
Old 24th October 2013, 18:45   #2  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
If the variables are fixed at compile-time (ie not dependent on individual frames, but possibly on clip properties like width), then WriteFileStart() will do the job.
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 25th October 2013, 05:43   #3  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
Nothing is frame dependent. These are complex text strings.

Script #1 writes the contents of two text string variables (A and B) into c:\temp.ini (either by deleting the file or replacing whatever might already be there)
Script #2 reads the first two lines of c:\temp.ini into variables A and B

I am lost here. Can you provide any insight into the two scripts to do this?
Forensic is offline   Reply With Quote
Old 25th October 2013, 07:59   #4  |  Link
Sir BlunderBrain
Registered User
 
Join Date: Jun 2010
Posts: 11
If the variables are just static strings, couldn't you just have script #1 generate an .avsi file (using WriteFile or any of it's variants) that sets the relevant variables and then Import() that into script #2?

I.e Script #1 generates c:\test\temp.avsi with:
Code:
A="string 1"
B="string 2"
and in script #2
Code:
Import("c:\test\temp.avsi")
Another option might be to use ConditionalReader(). But that would require AVISynth 2.60 to work with string variables and also a separate file for each variable.
Sir BlunderBrain is offline   Reply With Quote
Old 25th October 2013, 09:24   #5  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
It's a bit tricky because of the need to put quotes in the output string.
Code:
file = "C:\temp.ini"
q = chr(34)
q3 = q+q+q

A = """Some "quoted" string"""
B = "Another string"

BlankClip() # or source filter
WriteFileStart(file,  """ "A =" """, "q3", "A", "q3")
WriteFileStart(file,  """ "B =" """, "q3", "B", "q3", append=true)
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 25th October 2013, 16:34   #6  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
That gets me closer

Gavino. That solves the part of the first script, and I am truly appreciative. Any suggestions for the rest of #1 (deleting the file or replacing whatever might already be there) and also retrieving the variables (script #2)?

Script #1 writes the contents of two text string variables (A and B) into c:\temp.ini (either by deleting the file or replacing whatever might already be there)
Script #2 reads the first two lines of c:\temp.ini into variables A and B


P.S. I am using this with AVSpmod, so the solution can store the variables through that program (if that is easier and possible).
Forensic is offline   Reply With Quote
Old 25th October 2013, 19:07   #7  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 405
Forensic,
I suggest that you take a look at RT_Stats, especially the RT_TxtWriteFile() function for #1, RT_FileDelete(), RT_GetTextLine() and RT_ReadTxtFromFile() functions.
You could e.g. write avisynth variable assignment commands with RT_TxtWriteFile() - remember to use triple quotes or 'chr(34)' to write valid string terminators - and use
Code:
Exist(myFile)? Eval(RT_ReadTxtFromFile(myFile))
But you have many other options, e.g. read values directly from lines of the file etc.
martin53 is offline   Reply With Quote
Old 25th October 2013, 19:29   #8  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,380
Quote:
Originally Posted by Forensic View Post
Script #1 writes the contents of two text string variables (A and B) into c:\temp.ini (either by deleting the file or replacing whatever might already be there)
The first WriteFileStart (without append=true) does that automatically as its default action.

Quote:
Script #2 reads the first two lines of c:\temp.ini into variables A and B
As stated by Sir BlunderBrain, all you need is
Import("c:\temp.ini")
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 25th October 2013, 19:53   #9  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
Martin53 and Gavino (and BlunderBrain). I will try that. Thank you all,

Last edited by Forensic; 25th October 2013 at 19:54. Reason: Added everyone who helped
Forensic is offline   Reply With Quote
Old 26th October 2013, 01:32   #10  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 5,905
Forensic, Got your PM, sorry for not answering sooner, have not been on-line for a coupla days.
Think better to answer here.

Ini.TEMPLATE
Code:
Global VAR_1 = _VAR1_
VAR_2        = _VAR2_
VAR_3        = _VAR3_
Global VAR_4 = "_VAR4_"
Create.AVS
Code:
TMPT_NAME = "Ini.TEMPLATE"  # Template for ini file
INI_NAME  = "Test.ini"		# Output ini filename

VAR_1 = True
VAR_2 = 42
VAR_3 = 99.0
VAR_4 = "A Test String"

FIND_S=RT_String("%s\n%s\n%s\n%s\n","_VAR1_","_VAR2_","_VAR3_","_VAR4_")
REPL_S=RT_String("%s\n%d\n%f\n%s\n",VAR_1,VAR_2,VAR_3,VAR_4)

TMPT = RT_ReadTxtFromFile(TMPT_NAME)	# Read in entire avs template file

S = RT_StrReplaceMulti(TMPT,FIND_S,REPL_S)		# Replace insertion point markers with variables (as strings)
RT_TxtWriteFile(S,INI_NAME,Append=False)		# Create ini file for Import()

return colorbars
Test.Ini (created output file).
Code:
Global VAR_1 = True
VAR_2        = 42
VAR_3        = 99.000000
Global VAR_4 = "A Test String"
Above is one way, there are probably quite a few more ways to do it.

EDIT: Instead of template file could just use a string, eg
Code:
INI_NAME  = "Test.ini"		# Output ini filename

VAR_1 = True
VAR_2 = 42
VAR_3 = 99.0
VAR_4 = "A Test String"

TMPT="""Global VAR_1 = _VAR1_
VAR_2        = _VAR2_
VAR_3        = _VAR3_
Global VAR_4 = "_VAR4_"
"""

FIND_S=RT_String("%s\n%s\n%s\n%s\n","_VAR1_","_VAR2_","_VAR3_","_VAR4_")
REPL_S=RT_String("%s\n%d\n%f\n%s\n",VAR_1,VAR_2,VAR_3,VAR_4)

S = RT_StrReplaceMulti(TMPT,FIND_S,REPL_S)		# Replace insertion point markers with variables (as strings)
RT_TxtWriteFile(S,INI_NAME,Append=False)		# Create ini file for Import()

return colorbars
EDIT: Or skipping the string replacments
Code:
INI_NAME  = "Test.ini"		# Output ini filename

VAR_1 = True
VAR_2 = 42
VAR_3 = 99.0
VAR_4 = "A Test String"

Fmt="""Global VAR_1 = %s
VAR_2        = %d
VAR_3        = %f
Global VAR_4 = "%s"
"""
S=RT_String(Fmt,VAR_1,VAR_2,VAR_3,VAR_4)
RT_TxtWriteFile(S,INI_NAME,Append=False)		# Create ini file for Import()

return colorbars
EDIT: The 1st two examples have the advantage of being able to insert the same variable at multiple places in the template string,
as RT_StrReplaceMulti() replaces all instances of the find sub string with the corresponding replace sub string.
With the 3rd example, you would need create an "%s" style insertion point for each instance of the same variable.
The TEMPLATE version (1st) allows more complex scripts to be used (easily), perhaps inserting variables directly inside functions
or eg ScriptClip.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 26th October 2013 at 17:03.
StainlessS is online now   Reply With Quote
Old 27th October 2013, 03:52   #11  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
StainlessS...The 1st version was EXACTLY what I was looking for!!! THANK YOU.
Forensic is offline   Reply With Quote
Old 27th October 2013, 04:38   #12  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
The script works great. Just one question...What is the code for a different script to read one specific variable (say the second one) from the INI_NAME file.
Forensic is offline   Reply With Quote
Old 27th October 2013, 13:00   #13  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 5,905
Assuming that you want to read eg line 1 (0 relative, ie 2nd line).

Code:
 INI_NAME  = "Test.ini"
 REQ_LINE = 1 # 0 relative
 VS = RT_ReadTxtFromFile(INI_NAME ,Lines=1,Start=REQ_LINE)
 Eval(VS)
Or without Eval, you would need to parse the returned string, eg find '=' and return the number (EDIT: or string) following that.

Above CODE block is probably easiest, but you could use RT_FileFindStr to eg find 1st instance of variable name and parse that line,
but easier if you know which line the var initialization lives on.

EDIT: I guess you could even use above method as a sort of complex Select, where ini file contains eg 100 separate
statement lines to be Eval'ed based on an int in range 0 - 99.

EDIT: Or RT_TxtGetLine instead of RT_ReadTxtFromFile if contained in a multi-line string rather than a file.

EDIT: Oops, changed from line 3(4th) to line 1(2nd) as per your request.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 27th October 2013 at 14:45.
StainlessS is online now   Reply With Quote
Old 27th October 2013, 20:05   #14  |  Link
Forensic
Registered User
 
Join Date: Apr 2008
Location: California, USA
Posts: 128
Thank you !!!
Forensic is offline   Reply With Quote
Reply

Tags
avisynth, global variable, plugins, writefile

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 15:18.


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