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. |
24th October 2013, 18:19 | #1 | Link |
Registered User
Join Date: Apr 2008
Location: California, USA
Posts: 127
|
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? |
24th October 2013, 18:45 | #2 | Link |
Avisynth language lover
Join Date: Dec 2007
Location: Spain
Posts: 3,431
|
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.
|
25th October 2013, 05:43 | #3 | Link |
Registered User
Join Date: Apr 2008
Location: California, USA
Posts: 127
|
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? |
25th October 2013, 07:59 | #4 | Link |
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" Code:
Import("c:\test\temp.avsi") |
25th October 2013, 09:24 | #5 | Link |
Avisynth language lover
Join Date: Dec 2007
Location: Spain
Posts: 3,431
|
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) |
25th October 2013, 16:34 | #6 | Link |
Registered User
Join Date: Apr 2008
Location: California, USA
Posts: 127
|
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). |
25th October 2013, 19:07 | #7 | Link |
Registered User
Join Date: Mar 2007
Posts: 407
|
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)) |
25th October 2013, 19:29 | #8 | Link | ||
Avisynth language lover
Join Date: Dec 2007
Location: Spain
Posts: 3,431
|
Quote:
Quote:
Import("c:\temp.ini") |
||
26th October 2013, 01:32 | #10 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
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_" 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 Code:
Global VAR_1 = True VAR_2 = 42 VAR_3 = 99.000000 Global VAR_4 = "A Test String" 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 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 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. |
27th October 2013, 13:00 | #13 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
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) 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. |
Tags |
avisynth, global variable, plugins, writefile |
|
|