View Full Version : Variables that survive AVIsynth sessions
Forensic
24th October 2013, 18:19
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?
Gavino
24th October 2013, 18:45
If the variables are fixed at compile-time (ie not dependent on individual frames, but possibly on clip properties like width), then WriteFileStart (http://avisynth.nl/index.php/WriteFileStart)() will do the job.
Forensic
25th October 2013, 05:43
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?
Sir BlunderBrain
25th October 2013, 07:59
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() (http://avisynth.nl/index.php/Import) that into script #2?
I.e Script #1 generates c:\test\temp.avsi with:
A="string 1"
B="string 2"
and in script #2
Import("c:\test\temp.avsi")
Another option might be to use ConditionalReader() (http://avisynth.nl/index.php/ConditionalReader). But that would require AVISynth 2.60 to work with string variables and also a separate file for each variable.
Gavino
25th October 2013, 09:24
It's a bit tricky because of the need to put quotes in the output string.
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)
Forensic
25th October 2013, 16:34
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).
martin53
25th October 2013, 19:07
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
Exist(myFile)? Eval(RT_ReadTxtFromFile(myFile))
But you have many other options, e.g. read values directly from lines of the file etc.
Gavino
25th October 2013, 19:29
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.
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")
Forensic
25th October 2013, 19:53
Martin53 and Gavino (and BlunderBrain). I will try that. Thank you all,
StainlessS
26th October 2013, 01:32
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
Global VAR_1 = _VAR1_
VAR_2 = _VAR2_
VAR_3 = _VAR3_
Global VAR_4 = "_VAR4_"
Create.AVS
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).
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
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
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.
Forensic
27th October 2013, 03:52
StainlessS...The 1st version was EXACTLY what I was looking for!!! THANK YOU.
Forensic
27th October 2013, 04:38
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.
StainlessS
27th October 2013, 13:00
Assuming that you want to read eg line 1 (0 relative, ie 2nd line).
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.
Forensic
27th October 2013, 20:05
Thank you !!!
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.