raffriff42
1st October 2014, 05:32
This is a small part of a larger script I am working on, but I think it's worth posting on its own. I don't know how many of you ever use ConditionalReader (http://avisynth.nl/index.php/ConditionalReader) - judging from the Google hits, nobody at all - but with the ability to load multiple variables from a single file (instead of one variable per file), more people might find a use for it.
Anyway, here is the script. It uses a simple trick: arrays.
See "Arrays and Loops", http://forum.doom9.org/showthread.php?t=106880
Test data (MultiVariableReader-test.log)Type STRING
#this is a comment
# format 'R' first-frame last-frame (comma delimited list of array elements)
# example: if
# first-frame=0
# last-frame=2
# array(0) = 19:58:25.000
# array(1) = 2
# array(2) = 62.156100
# array(3) = 51.960021
# array(4) = 50.000763
# array(5) = "R"
# array(6) = "U"
# array(7) = "V"
# array(8) = -1
# the line in the text file would be:
R 0 2 19:58:25.000,2,62.156100,51.960021,50.000763,R,U,V,-1
# etc...
R 3 5 19:58:25.109,2,65.882353,49.999237,50.000763,R,U,V,-1
R 6 8 19:58:25.218,2,61.176471,48.534371,50.000763,R,U,V,-1
R 9 12 19:58:25.328,2,59.018845,47.753109,50.000763,R,U,V,-1
# next line has no commas, so array(0) = "a line with no commas"
# and array(1) and above = ""
R 13 15000 a line with no commas
Quick demo:Import("MultiVariableReader.avs")
ColorBars.KillAudio.Trim(0, 300)
logpath="MultiVariableReader-test.log"
MultiVariableReader(logpath)
return ScriptClip(Last, """
## do something with array elements 0, 2 & 3
a = String(mvr_arr.DeRef(2))
b = String(mvr_arr.DeRef(3))
Subtitle(mvr_arr.DeRef(0) + "\n" +
\ "f(2)=" + a + "\nf(3)=" + b,
\ x=Width*Value(a)/100.0,
\ y=Height*Value(b)/100.0,
\ lsp=0)
""")
(MultiVariableReader.avs)# requires rb-array.avsi
# http://forum.doom9.org/showthread.php?t=106880
global mvr_line = ""
#######################################
### Load multiple variables with ConditionalReader,
### using a delimited list
##
## @ C - dummy; returned unchanged
## @ sPath - [[ConditionalReader]] file (TYPE String)
## @ delim - field delimiter; default ',' (comma)
##
function MultiVariableReader(clip C, string sPath, string "delim")
{
global mvr_delim = Default(delim, ",")
C ## Last==C
Dim("mvr_arr", 25, -1111)
## read delimited data into a string:
ConditionalReader(sPath, "mvr_line", show=false)
return ScriptClip(Last, """
mvr_arr.FillStrings(mvr_line)
return Last
""")
}
#######################################
# FillStrings - modded rb-array Fill() for string values
#
# USAGE: array.FillStrings("3, -50, 124")
#
# @ index - Start writing values at this index. Default is 0.
#
function FillStrings(string arr, string values, int "index")
{
index = Default(index, 0)
offset = FindStr(values, ",")
# FindStr returns 0 if not found
val = (offset==0)
\ ? values
\ : LeftStr(values, offset-1)
Eval("global " + arr + String(index) + "=" + Chr(34) + val + Chr(34))
return (offset==0)
\ ? Nop
\ : arr.FillStrings(MidStr(values, offset + 1), index + 1)
}
__END__
Anyway, here is the script. It uses a simple trick: arrays.
See "Arrays and Loops", http://forum.doom9.org/showthread.php?t=106880
Test data (MultiVariableReader-test.log)Type STRING
#this is a comment
# format 'R' first-frame last-frame (comma delimited list of array elements)
# example: if
# first-frame=0
# last-frame=2
# array(0) = 19:58:25.000
# array(1) = 2
# array(2) = 62.156100
# array(3) = 51.960021
# array(4) = 50.000763
# array(5) = "R"
# array(6) = "U"
# array(7) = "V"
# array(8) = -1
# the line in the text file would be:
R 0 2 19:58:25.000,2,62.156100,51.960021,50.000763,R,U,V,-1
# etc...
R 3 5 19:58:25.109,2,65.882353,49.999237,50.000763,R,U,V,-1
R 6 8 19:58:25.218,2,61.176471,48.534371,50.000763,R,U,V,-1
R 9 12 19:58:25.328,2,59.018845,47.753109,50.000763,R,U,V,-1
# next line has no commas, so array(0) = "a line with no commas"
# and array(1) and above = ""
R 13 15000 a line with no commas
Quick demo:Import("MultiVariableReader.avs")
ColorBars.KillAudio.Trim(0, 300)
logpath="MultiVariableReader-test.log"
MultiVariableReader(logpath)
return ScriptClip(Last, """
## do something with array elements 0, 2 & 3
a = String(mvr_arr.DeRef(2))
b = String(mvr_arr.DeRef(3))
Subtitle(mvr_arr.DeRef(0) + "\n" +
\ "f(2)=" + a + "\nf(3)=" + b,
\ x=Width*Value(a)/100.0,
\ y=Height*Value(b)/100.0,
\ lsp=0)
""")
(MultiVariableReader.avs)# requires rb-array.avsi
# http://forum.doom9.org/showthread.php?t=106880
global mvr_line = ""
#######################################
### Load multiple variables with ConditionalReader,
### using a delimited list
##
## @ C - dummy; returned unchanged
## @ sPath - [[ConditionalReader]] file (TYPE String)
## @ delim - field delimiter; default ',' (comma)
##
function MultiVariableReader(clip C, string sPath, string "delim")
{
global mvr_delim = Default(delim, ",")
C ## Last==C
Dim("mvr_arr", 25, -1111)
## read delimited data into a string:
ConditionalReader(sPath, "mvr_line", show=false)
return ScriptClip(Last, """
mvr_arr.FillStrings(mvr_line)
return Last
""")
}
#######################################
# FillStrings - modded rb-array Fill() for string values
#
# USAGE: array.FillStrings("3, -50, 124")
#
# @ index - Start writing values at this index. Default is 0.
#
function FillStrings(string arr, string values, int "index")
{
index = Default(index, 0)
offset = FindStr(values, ",")
# FindStr returns 0 if not found
val = (offset==0)
\ ? values
\ : LeftStr(values, offset-1)
Eval("global " + arr + String(index) + "=" + Chr(34) + val + Chr(34))
return (offset==0)
\ ? Nop
\ : arr.FillStrings(MidStr(values, offset + 1), index + 1)
}
__END__