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 4th September 2021, 14:25   #1  |  Link
konstantin1
Registered User
 
Join Date: Mar 2014
Posts: 38
How can I read a string value from a text file and assign it to a variable?

Unfortunately Avisynth script can't accept parameters, as far as I know. Currently I try to use environment variables, and a plugin from stickyboy, to make my script more flexible by parameterizing some variables. It would be better and more flexible to read the value(s) from a text file. This is a quite simple task in every programming language, but seems impossible in avisynth. Is it possible? How can I do that?

I found command "conditionalreader", but as far as I can tell, the variable can be used only in "scriptclip" calls.
konstantin1 is offline   Reply With Quote
Old 4th September 2021, 15:10   #2  |  Link
konstantin1
Registered User
 
Join Date: Mar 2014
Posts: 38
I could solve this by "import", and importing an avs script which merely returns a string. That avs file is always rewritten with the needed content.
konstantin1 is offline   Reply With Quote
Old 4th September 2021, 19:41   #3  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,260
from RT_Stats,
Code:
RT_ReadTxtFromFile(String ,Int "Lines"=0,Int "Start"=0)
 Non-clip function.
 String Filename, Name of text file to load into a string.
 Lines=0=unlimited. Set to number of leading lines in text file to load, eg 1 = load only the first line of text file.
 The return string is n/l ie Chr(10) separated, and carriage returns are removed from the returned string.
 If source file was missing newline on very last line, it will append a newline so that all lines are similarly formatted.
 v1.03, Added Start arg default=0=very first line (relative 0). Would have been nice to have start and lines in reverse
 order but implemented as above to not break scripts.
 Throws an error if your requested Start is >= to the number of lines in the file, or zero len file.
 To fetch the last line of a text file, use eg Start = RT_FileQueryLines(Filename) - 1 (Start is zero relative).
 You could eg get the last line of a d2v file which might look like this:- "FINISHED  100.00% VIDEO"
Code:
RT_TxtWriteFile(String,String,bool "append"=false)
 Non-clip function.
 The first arg is a string of text that you want to write to a file.
 The second arg is the output filename.
 Returns the number of lines written or -1 if file error, cannot open or write error.
 An empty write string ("") would return 0 unless newline terminated [chr(10)].
 Append, if true appends to existing file. NOTE, the return value does NOT include any already existing
 lines if append is true. (use RT_FileQueryLines to inquire total number lines when append = true).
Code:
RT_WriteFile(String FileName, string format, dat1, ... , datn, bool "Append"=False)
 Writes  a formatted string to FileName file. The unnamed 'format' string and optional unnamed 'dat' args are used to construct the
 text string that is written, uses C/CPP printf() style formatting.
 Format: compulsory string controlling format and describing the datn type args that are expected.
 datn: Variable number of data args of any type (excluding clip).
 Append, Default False, optional and MUST be supplied with name (eg Append=True) as we do not know where optional data args end,
 Appends to file if already exists, else opens new FileName.

 printf Format spec here:- http://msdn.microsoft.com/en-us/library/56e442dc%28v=vs.71%29.aspx
  NOTE, the only support for printing Bool variables is %s as string, ie prints "True" or "False".
 Formatting supported %[flags] [width] [.precision] type
  flags, one of  "-,+,0, ,#"
  width, integer, "*" supported (width supplied via dat arg).
  Precision, integer, "*" supported (precision supplied via dat arg).
  type,
    "c,C,d,i,o,u,x,X",  Integer type, c,C=character, d,i=signed, o,u,x,X=unsigned (o=octal, x=Hex).
    "e,E,f,g,G",        Floating point type
    "s,S",              String type (also Bool).

  Formatting Insertion point is marked with '%' character in the format string (as in Avisynth String function), if you wish to use
  a percent character within the output string, it should be inserted twice in the format string ie '%%' will produce a single '%'.
  The data datn arg strings do not require a double '%' character.

  A Backslash character '\' introduces escape sequences, to insert a backslash character itself, you must supply a double
  backslash sequence ie '\\'.
  Converts embedded escape character sequences (Case Significant):-
    '\\' Converted to '\'       Single Backslash
    '\a' Converted to Chr(7)
    '\b' Converted to Chr(8)
    '\t' Converted to Chr(9)
    '\n' Converted to Chr(10)   NewLine
    '\r' [and Chr(13)] Converted to Chr(10)   NewLine
    '\v' Converted to Chr(11)
    '\f' Converted to Chr(12)
    '\x', where x is ANY OTHER CHARACTER not included above, will be copied verbatim, ie '\x'.

  Escape sequences are replaced only when occurring in the format string, if you need escapes in data string then use RT_String.

  Example:
   RT_WriteFile(FileName,"Hello there %s and %s.\nGoodbye %d.","Fred","Ted",2013,Append=False)
Code:
RT_FileFindStr(String FileName,string FndS,bool "Sig"=True,int "Pos"=1,int "Start"=0,int "Lines"=0)
 Finds unnamed string 'FndS' in 'FileName' text file, starting at line 'Start' and searching 'Lines' number of text lines.
 FileName: Name of a file containing text to search.
 FndS: is a string and will be considered terminated early at the first carriage return [Chr(13)] or newline [Chr(10)], carriage returns
 and newlines will not be findable using this function.
 Sig: Default=True, does case significant comparison, insignificant if false.
 Pos: Default=1, is the character start position in an individual line of text to start searching for the FndS string.
   Allows you to skip search on first Pos-1 characters within all lines of text.
   If Pos less than 1, then will return -ve number (usually -1), Not Found.
 Start: Default 0, is the 0 relative starting line where the searching begins in the text file.
 Lines: Default 0(all lines). Number of lines text to seach, starting at 'Start' line.
 A -ve return is 'Not Found'. On success returns the line number of the first instance of a found string in the Start and Lines
  range of lines. An empty "" FndS will always return -ve result, ie Not Found.
 see RT_FileQueryLines, to inquire number lines in a text file.
 see RT_ReadTxtFromFile to extract a range of lines from a text file.
 NOTE, Character position 'Pos' is 1 relative whereas multi-line line 'Start' index is 0 relative.
  Care must be taken if mixing RT_FindStr and RT_TxtFindStr or RT_FileFindStr, RT_FindStr returns 0 on error, the other
 two return -ve on error(0 denoting valid line zero).
Code:
RT_TxtQueryLines(String)
 Non-clip function.
 String, the multiline string that you require a line count from.
 Returns the number of Newline [Chr(10)] separated lines in a multiline string.
  The last line does not have to be Chr(10) terminated, it still counts.


RT_TxtGetLine(String, Int "Line"=0)
 Non-clip function.
 Extract a single line from a multiline Newline[Chr(10)] separated string. Default=0 == first line.
 The Line index is Zero Relative like frame number versus FrameCount.
 The returned string has trailing Newlines and carriage returns stripped from it.
 Throws an error if your requested line is >= to the number of lines in the multiline string.
There are more.

EDIT:
To Print contents of multiline text file on frame:
Code:
    AVISource("D:\AVS\TEST.AVI")
    S=RT_ReadTxtFromFile("test.txt")
    Scriptclip("""RT_Subtitle("%s",S,align=5,vcent=True)""")
    return last
To Print contents of multiline text file on frame, Scrolling upwards as for end credits:
Code:
    AVISource("D:\AVS\TEST.AVI").ConvertToRGB32()
    S=RT_ReadTxtFromFile("D:\avs\avi\test.txt")
    DELAY=100
    Scriptclip("""RT_Subtitle("%s",S,align=5,y=height+DELAY-current_frame,expy=true)""")
EDIT:
Quote:
and importing an avs script which merely returns a string.
Import is sort of same as Read a file into a string, and then do an Eval() on that string.

EDIT:
Test.txt
Code:
line1
line2
the quick brown fox
my helicopter is full of eels.
line5
Test.avs
Code:
BlankClip(Width=320,height=240)
FN=".\Test.txt"
Assert(Exist(FN),"Must have existing file")

AllFileString = RT_ReadTxtFromFile(FN, Lines=0, Start=0)  # all lines, and from beginning

Lines = RT_TxtQueryLines(AllFileString)  # Number of lines in multi-line string

For(i=0,Lines-1) {
    S = RT_TxtGetLine(AllFileString,Line=i)
    Subtitle(String(i,"%.0f] ")+S, y=i*20)
}
__________________
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; 4th September 2021 at 20:24.
StainlessS is offline   Reply With Quote
Reply

Tags
environment, file, parameter, string, variable

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:20.


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