ConditionalReader
From Avisynth wiki
(Difference between revisions)
Raffriff42 (Talk | contribs) (formatting, links, phrasing) |
(add link to avs+ documentation) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | <div {{BlueBox2|40|0|3px solid purple}} > | ||
+ | {{AvsPlusFullname}}<br> | ||
+ | Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/conditionalreader.html https://avisynthplus.readthedocs.io] | ||
+ | </div> | ||
+ | |||
+ | |||
Import information from a text file and assign a per-frame value to a script variable. | Import information from a text file and assign a per-frame value to a script variable. | ||
Line 17: | Line 23: | ||
:{{Par2|show|bool|false}} | :{{Par2|show|bool|false}} | ||
− | ::If ''true'', show | + | ::If ''true'', show a text overlay with {{FuncArg|variablename}}'s assigned value at the current frame. |
</div> | </div> | ||
Line 23: | Line 29: | ||
== File format == | == File format == | ||
<div style="max-width:62em" > | <div style="max-width:62em" > | ||
− | *The file is plain text. It is not case sensitive. Each line stands alone. There are several different line types, as explained below. | + | *The file is plain text. |
+ | **It is not case sensitive. | ||
+ | **Each line stands alone. | ||
+ | **There are several different ''line types'', as explained below. | ||
+ | **Blank lines are ignored. | ||
− | *The file may begin with one or more optional {{BoldColor| | + | *The file may begin with one or more optional {{BoldColor|#4F81BD|100|''comment''}} lines: |
<div {{ListItemContinue}} > | <div {{ListItemContinue}} > | ||
Any line is ignored if the first non-whitespace character is '<tt>#</tt>' (hash), '<tt>;</tt>' (semicolon) or '<tt>%</tt>' (percent). | Any line is ignored if the first non-whitespace character is '<tt>#</tt>' (hash), '<tt>;</tt>' (semicolon) or '<tt>%</tt>' (percent). | ||
Line 35: | Line 45: | ||
</div> | </div> | ||
− | *The first non-{{BoldColor| | + | *The first non-{{BoldColor|#4F81BD|100|''comment''}} line should be a {{BoldColor|#4F81BD|100|''type''}} line, which must be one of the following: |
:: <code>TYPE int</code> | :: <code>TYPE int</code> | ||
− | ::: | + | ::: Tells '''ConditionalReader''' to expect decimal integer ''values''. |
+ | ::: (Digits <code>0-9</code>, optionally preceded with a '<code>+</code>' or '<code>-</code>' sign) | ||
:: <code>TYPE float</code> | :: <code>TYPE float</code> | ||
− | ::: | + | ::: Tells '''ConditionalReader''' to expect floating-point ''values''. |
+ | ::: (Decimal number with '<code>.</code>' decimal point, and optionally followed by an '<code>E</code>' character and decimal exponent) | ||
:: <code>TYPE bool</code> | :: <code>TYPE bool</code> | ||
− | ::: ('true' or 'false') | + | ::: Tells '''ConditionalReader''' to expect boolean ''values''. |
+ | ::: ('<code>true</code>' or '<code>false</code>', '<code>T</code>' or '<code>F</code>', '<code>yes</code>' or '<code>no</code>', '<code>1</code>' or '<code>0</code>') | ||
:: <code>TYPE string</code> (from v2.60) | :: <code>TYPE string</code> (from v2.60) | ||
− | ::: ( | + | ::: (Any sequence of characters, including quote, backslash etc. A line break ends the string.) |
− | :The {{BoldColor| | + | :The {{BoldColor|#4F81BD|100|''type''}} line defines the {{BoldColor|#4F81BD|100|''data''}} type to be parsed from this file and assigned to the variable {{FuncArg|variablename}}. You must have one, and only one, {{BoldColor|#4F81BD|100|''type''}} line per file, and it must come before any other lines, except for optional comments. |
− | * | + | *A {{BoldColor|#4F81BD|100|''default''}} line should come next. It is optional but strongly suggested: |
− | + | ||
− | + | ||
:::<code>DEFAULT <value></code> | :::<code>DEFAULT <value></code> | ||
− | :::This specifies the | + | :::This specifies the value to be used if there is no applicable {{BoldColor|#4F81BD|100|''data''}} line for a given frame. |
− | + | :::If you do not define a default, be sure to specify a value for all frames; otherwise your script variable could be [[Internal functions/Defined|undefined]] or take a random value. | |
− | + | ||
− | + | ||
− | + | ||
− | + | *An optional {{BoldColor|#4F81BD|100|''offset''}} line may appear: | |
+ | :::<code>OFFSET <integer-value></code> (from v2.60) | ||
+ | :::When specified, this will add an offset adjustment to all frame numbers in the {{BoldColor|#4F81BD|100|''data''}} lines below. | ||
− | *Next come the {{BoldColor| | + | *Next come the {{BoldColor|#4F81BD|100|''data''}} line(s), which set the per-frame value(s) to be assigned to the script variable. There are three styles: |
− | ::''Single-frame'' | + | ::''Single-frame'' style: |
:::<code><framenumber> <value></code> | :::<code><framenumber> <value></code> | ||
:::Set ''value'' for frame ''framenumber'' only. | :::Set ''value'' for frame ''framenumber'' only. | ||
− | ::''Range'' | + | ::''Range'' style: |
:::<code>R <startframe> <endframe> <value></code> | :::<code>R <startframe> <endframe> <value></code> | ||
:::Apply ''value'' to a range of frames. | :::Apply ''value'' to a range of frames. | ||
− | :::Note that both ''startframe'' and ''endframe'' are included. | + | :::Note that both ''startframe'' and ''endframe'' are included in the range. |
− | ::''Interpolated'' | + | ::''Interpolated'' style: |
− | :::<code>I <startframe> <endframe> < | + | :::<code>I <startframe> <endframe> <start-value> <stop-value></code> |
− | :::Interpolate between | + | :::Interpolate between ''start-value'' and ''stop-value'' over a range of frames. This only works on ''int'' and ''float'' types. |
− | :::Note that both ''startframe'' and ''endframe'' are included. | + | :::Note that both ''startframe'' and ''endframe'' are included in the range. |
− | : | + | :Later data lines in the file overrule earlier ones. Styles may be mixed-and-matched; see the examples below. |
− | + | :All {{BoldColor|#4F81BD|100|''data''}} and {{BoldColor|#4F81BD|100|''default''}} ''values'' must be valid for the defined {{BoldColor|#4F81BD|100|''type''}}, as defined above, or an error will be raised. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</div> | </div> | ||
− | |||
− | |||
== Examples == | == Examples == | ||
<div style="max-width:62em" > | <div style="max-width:62em" > | ||
===== ''Basic usage'' ===== | ===== ''Basic usage'' ===== | ||
− | : | + | :File ''Basic.txt:'' |
<div {{BoxWidthIndent|24|3}} > | <div {{BoxWidthIndent|24|3}} > | ||
Type float | Type float | ||
Line 124: | Line 103: | ||
:The file above will return values of type ''float''. It will return <tt>3.45567</tt> by default, but at frames 45-300 it will return <tt>76.5654</tt>, and at frames 2 and 72 it will return <tt>-671.454</tt>. | :The file above will return values of type ''float''. It will return <tt>3.45567</tt> by default, but at frames 45-300 it will return <tt>76.5654</tt>, and at frames 2 and 72 it will return <tt>-671.454</tt>. | ||
− | :Later lines in the file overrule earlier ones. This is illustrated by the '72' line: even though frame 72 is inside the range of 45-300, frame 72 will use the value <tt>-671.454</tt>, not <tt>76.5654</tt>. If the 'R' line had been placed ''after'' the '72' line, the range value would have had priority. | + | :Later data lines in the file overrule earlier ones. This is illustrated by the '72' line: even though frame 72 is inside the range of 45-300, frame 72 will use the value <tt>-671.454</tt>, not <tt>76.5654</tt>. If the 'R' line had been placed ''after'' the '72' line, the range value would have had priority. |
:A script to invoke this file could be: | :A script to invoke this file could be: | ||
Line 130: | Line 109: | ||
[[ColorBars]](512,512) | [[ColorBars]](512,512) | ||
[[Trim]](0,500) | [[Trim]](0,500) | ||
− | [[ScriptClip]](" | + | [[ScriptClip]]("Subtitle(String(myvar))") |
− | ConditionalReader(" | + | ConditionalReader("Basic.txt", "myvar", false) |
</div> | </div> | ||
− | :This will put the values into the variable called <tt>myvar</tt>, which is displayed with [[Subtitle]]. Subtitle must be invoked by [[ScriptClip]] to display the | + | :This will put the values into the variable called <tt>myvar</tt>, which is displayed with [[Subtitle]]. Subtitle must be invoked by [[ScriptClip]] to display the [[Runtime_environment|runtime]] value. |
+ | |||
+ | :'''Note''' the '''ConditionalReader''' line comes ''after'' any use of <tt>myvar</tt> in the script. | ||
+ | |||
+ | ===== ''String values'' ===== | ||
+ | :File ''Strings.txt:'' | ||
+ | <div {{BoxWidthIndent|24|3}} > | ||
+ | Type string | ||
+ | Default | ||
+ | # (default=empty string) | ||
+ | |||
+ | R 45 99 this is a string | ||
+ | R 100 199 "quoted string" | ||
+ | R 200 299 w:\xyz.txt | ||
+ | </div> | ||
+ | :Try the above Subtitle script with ''Strings.txt''. You will see the strings displayed verbatim. | ||
===== ''Adjusting Overlay'' ===== | ===== ''Adjusting Overlay'' ===== | ||
Line 167: | Line 161: | ||
</div> | </div> | ||
− | :Basically this example defines ''keyframes'' for an [[Overlay]] x-offset and opacity. For frames 25-50 the opacity is scaled from 0.0 to 1.0, while the text is moving from left to right. The text is then kept steady from frame 50-250, and thereafter it moves further to the right while fading out | + | :It is easier to watch the clip above than completely describe what it does. Basically, this example defines ''keyframes'' for an [[Overlay]] x-offset and opacity. For frames 25-50 the opacity is scaled from 0.0 to 1.0, while the text is moving from left to right. The text is then kept steady from frame 50-250, and thereafter it moves further to the right, while fading out. |
− | ===== '' | + | ===== ''ApplyRange replacement'' ===== |
− | : | + | :Using a large number of [[ApplyRange]] calls in a script can lead to resource issues. '''ConditionalReader''' together with [[ConditionalFilter]] can be used instead, leading to an efficient solution: |
:File ''File.txt:'' | :File ''File.txt:'' | ||
Line 185: | Line 179: | ||
</div> | </div> | ||
− | : | + | :By default, the script value will be False. However for frames 2, 45-60, 72, 200-220 and 315, except for 210, it will be True. Later data lines in the file overrule earlier ones. This is illustrated by frame '210': even though it is inside the range of 200-220, the later value, False, will be used. |
:A script to make use of this file could be: | :A script to make use of this file could be: | ||
Line 194: | Line 188: | ||
[[FlipHorizontal]] # Add a complex filter chain | [[FlipHorizontal]] # Add a complex filter chain | ||
B = Last | B = Last | ||
− | ConditionalFilter(A, B, " | + | [[ConditionalFilter]](A, B, "myvar", "==", "False", false) |
− | ConditionalReader("File.txt", " | + | ConditionalReader("File.txt", "myvar", false) |
</div> | </div> | ||
− | :This will | + | :This will read the file values into the variable <tt>myvar</tt>, which is used by [[ConditionalFilter]] to select between the unprocessed and flipped version of the source. |
− | + | ||
− | + | ||
− | ===== '' | + | ===== ''Strings values in v2.58'' ===== |
:'''ConditionalReader''' cannot return strings prior to v2.60, but one solution is to create a list of variables with corresponding string assignments, and [[Internal functions/Eval|Eval]] the indexed solution. For example: | :'''ConditionalReader''' cannot return strings prior to v2.60, but one solution is to create a list of variables with corresponding string assignments, and [[Internal functions/Eval|Eval]] the indexed solution. For example: | ||
<div {{BoxWidthIndent|42|3}} > | <div {{BoxWidthIndent|42|3}} > |
Latest revision as of 04:18, 18 September 2022
AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io
Import information from a text file and assign a per-frame value to a script variable.
Contents |
[edit] Syntax and Parameters
ConditionalReader(clip clip, string filename, string variablename [, bool show ] )
- clip clip =
- Source clip. Not touched, unless you specify show=true.
- string filename =
- Path to the file with the per-frame values you want to set. See File format below.
- string variablename =
- Name of the variable you want the filename values assigned to.
- bool show = false
- If true, show a text overlay with variablename's assigned value at the current frame.
[edit] File format
- The file is plain text.
- It is not case sensitive.
- Each line stands alone.
- There are several different line types, as explained below.
- Blank lines are ignored.
- The file may begin with one or more optional comment lines:
Any line is ignored if the first non-whitespace character is '#' (hash), ';' (semicolon) or '%' (percent).
#this line is a comment ;so is this line %and this one too.
- The first non-comment line should be a type line, which must be one of the following:
-
TYPE int
- Tells ConditionalReader to expect decimal integer values.
- (Digits
0-9
, optionally preceded with a '+
' or '-
' sign)
-
TYPE float
- Tells ConditionalReader to expect floating-point values.
- (Decimal number with '
.
' decimal point, and optionally followed by an 'E
' character and decimal exponent)
-
TYPE bool
- Tells ConditionalReader to expect boolean values.
- ('
true
' or 'false
', 'T
' or 'F
', 'yes
' or 'no
', '1
' or '0
')
-
TYPE string
(from v2.60)- (Any sequence of characters, including quote, backslash etc. A line break ends the string.)
-
- The type line defines the data type to be parsed from this file and assigned to the variable variablename. You must have one, and only one, type line per file, and it must come before any other lines, except for optional comments.
- A default line should come next. It is optional but strongly suggested:
DEFAULT <value>
- This specifies the value to be used if there is no applicable data line for a given frame.
- If you do not define a default, be sure to specify a value for all frames; otherwise your script variable could be undefined or take a random value.
- An optional offset line may appear:
OFFSET <integer-value>
(from v2.60)- When specified, this will add an offset adjustment to all frame numbers in the data lines below.
- Next come the data line(s), which set the per-frame value(s) to be assigned to the script variable. There are three styles:
- Single-frame style:
<framenumber> <value>
- Set value for frame framenumber only.
- Single-frame style:
- Range style:
R <startframe> <endframe> <value>
- Apply value to a range of frames.
- Note that both startframe and endframe are included in the range.
- Range style:
- Interpolated style:
I <startframe> <endframe> <start-value> <stop-value>
- Interpolate between start-value and stop-value over a range of frames. This only works on int and float types.
- Note that both startframe and endframe are included in the range.
- Interpolated style:
- Later data lines in the file overrule earlier ones. Styles may be mixed-and-matched; see the examples below.
- All data and default values must be valid for the defined type, as defined above, or an error will be raised.
[edit] Examples
[edit] Basic usage
- File Basic.txt:
Type float Default 3.45567 R 45 300 76.5654 2 -671.454 72 -671.454
- The file above will return values of type float. It will return 3.45567 by default, but at frames 45-300 it will return 76.5654, and at frames 2 and 72 it will return -671.454.
- Later data lines in the file overrule earlier ones. This is illustrated by the '72' line: even though frame 72 is inside the range of 45-300, frame 72 will use the value -671.454, not 76.5654. If the 'R' line had been placed after the '72' line, the range value would have had priority.
- A script to invoke this file could be:
ColorBars(512,512) Trim(0,500) ScriptClip("Subtitle(String(myvar))") ConditionalReader("Basic.txt", "myvar", false)
- This will put the values into the variable called myvar, which is displayed with Subtitle. Subtitle must be invoked by ScriptClip to display the runtime value.
- Note the ConditionalReader line comes after any use of myvar in the script.
[edit] String values
- File Strings.txt:
Type string Default # (default=empty string) R 45 99 this is a string R 100 199 "quoted string" R 200 299 w:\xyz.txt
- Try the above Subtitle script with Strings.txt. You will see the strings displayed verbatim.
[edit] Adjusting Overlay
- Script:
ColorBars(512,256) a1 = Trim(0,600) a2 = MessageClip("Text clip") Overlay(a1,a2, y = 100, x = 110, mode="subtract", opacity=0, pc_range=true) ConditionalReader("opacity.txt", "ol_opacity_offset", false) ConditionalReader("xoffset.txt", "ol_x_offset", false)
- File xoffset.txt:
Type int Default -50 I 25 50 -50 100 R 50 250 100 I 250 275 100 250
- File opacity.txt:
Type float Default 0.0 I 25 50 0.0 1.0 R 50 250 1.0 I 250 275 1.0 0.0
- It is easier to watch the clip above than completely describe what it does. Basically, this example defines keyframes for an Overlay x-offset and opacity. For frames 25-50 the opacity is scaled from 0.0 to 1.0, while the text is moving from left to right. The text is then kept steady from frame 50-250, and thereafter it moves further to the right, while fading out.
[edit] ApplyRange replacement
- Using a large number of ApplyRange calls in a script can lead to resource issues. ConditionalReader together with ConditionalFilter can be used instead, leading to an efficient solution:
- File File.txt:
Type Bool Default False 2 True R 45 60 True 72 True R 200 220 True 210 False 315 True
- By default, the script value will be False. However for frames 2, 45-60, 72, 200-220 and 315, except for 210, it will be True. Later data lines in the file overrule earlier ones. This is illustrated by frame '210': even though it is inside the range of 200-220, the later value, False, will be used.
- A script to make use of this file could be:
ColorBars(512,512) Trim(0,500) A = Last FlipHorizontal # Add a complex filter chain B = Last ConditionalFilter(A, B, "myvar", "==", "False", false) ConditionalReader("File.txt", "myvar", false)
- This will read the file values into the variable myvar, which is used by ConditionalFilter to select between the unprocessed and flipped version of the source.
[edit] Strings values in v2.58
- ConditionalReader cannot return strings prior to v2.60, but one solution is to create a list of variables with corresponding string assignments, and Eval the indexed solution. For example:
Import("strings.txt") ScriptClip("""subtitle(Eval("n"+string(mystringindex)))""") ConditionalReader("range_string.txt", "mystringindex")
- File strings.txt
n0="" n1="Intro" n2="Main" n3="Credits"
- File range_string.txt
Type int Default 0 R 10 1000 1 R 1005 3000 2 R 3200 3800 3
- Obviously strings.txt does not need to be a separate file, but this solution is sometimes appropriate in e.g., multilingual applications:
language="spanish" Import(language + "_strings.txt")
[edit] Changelog
v2.60 | Added OFFSET, Added Type=string. |