View Full Version : help with if statement
hazel-ra
10th November 2008, 03:11
I'm trying to make a function to standardize my loading of video clips. I'm mixing widescreen and television ratio sources; the desired output is widescreen, 720x300.
If the source is in a TV ratio, 1.3 or less, then I want to add black borders on each side. If the ratio is closer to widescreen, I just want to adjust it to 720x300.
I gave it my best shot, but as you'll see below, I'm having a lot of problems with the syntax. I get a "syntax error" on line 13.
function StandardizedLoad( string file ) {
clip = AVISource(file)
target_horiz = 720
target_vert = 300
# figure out the dimensions of the source file.
ratio = clip.Width / clip.Height
ratio > 1.3 ? {
# the ratio of the image is close enough to widescreen.
return AVISource(file).BilinearResize( target_horiz, target_vert ).AssumeFPS("ntsc_film", true).ResampleAudio( 48000 )
} : {
# add borders to get the source to the ratio of 2.4
x = ( 2.4 * Width ) / Height
border = x / 2
clip.AddBorders( border, 0, border, 0 )
return clip.BilinearResize( target_horiz, target_vert ).AssumeFPS("ntsc_film", true).ResampleAudio( 48000 )
}
}
Sagekilla
10th November 2008, 06:31
First off, read the Avisynth documentation if you haven't. Or, re-read the grammar and syntax again if you did because avisynth doesn't use brackets to section off code during a ternary operation. If you need to use multiple lines then you do it like this:
Note there's a space after the plus sign, if you don't put that you'll get an error.
x = 3 +
\ 2
Here's your code, fixed and slightly improved:
function StandardizedLoad(string file) {
clip = Avisource(file)
target_horiz = 720
target_vert = 300
ratio = clip.Width() / clip.Height()
pad = (clip.Height() * 2.4) - clip.Width()
clip = (ratio < 1.4) ? clip.AddBorders(pad/2,0,pad/2,0) : NOP()
return clip.BilinearResize(target_horiz, target_vert).AssumeFPS("nstc_film",true).ResampleAudio(48000)
}
kemuri-_9
10th November 2008, 06:40
You should also change
ratio = clip.Width() / clip.Height()
to
ratio = Float(clip.Width()) / clip.Height()
to have it use floating point math instead of integer math, so you actually get decimal values.
pad = (clip.Height() * 2.4) - clip.Width()
to
pad = Int((clip.Height() * 2.4) - clip.Width())
to have it truncate to a decimal so it doesn't pass floats to addborders as well
clip = (ratio < 1.4) ? clip.AddBorders(pad/2,0,pad/2,0) : NOP()
to
clip = (ratio <= 1.3) ? clip.AddBorders(pad/2,0,pad/2,0) : clip
so it doesn't null out the clip if it doesn't pad
mikeytown2
10th November 2008, 07:44
you might also want to give ZoomBox (http://forum.doom9.org/showthread.php?p=1111789#post1111789) a try...
stickboy
10th November 2008, 09:23
Using AviSynth's ternary operator with multiple statements (http://avisynth.org/stickboy/ternary_eval.html)
Gavino
10th November 2008, 14:05
Note there's a space after the plus sign, if you don't put that you'll get an error.
x = 3 +
\ 2
This is wrong - no space is required.
hazel-ra
10th November 2008, 14:39
First off, read the Avisynth documentation if you haven't. Or, re-read the grammar and syntax again if you did because avisynth doesn't use brackets to section off code during a ternary operation.
I plead lousy documentation! :confused:
I read the documentation. Here's a code snippet from it: (http://avisynth.org/mediawiki/Block_statements)
Example 2
[result = ] cond ? {
filter1_1
filter1_2
...
filter1_n
} : {
filter2_1
filter2_2
...
filter2_n
}
Oops! Here's the code snippet in context:
It would be nice to be able to construct two blocks of filter operations and branch in a single step, as in the (ideal) Example 2 below:
Example 2
[result = ] cond ? {
filter1_1
filter1_2
...
filter1_n
} : {
filter2_1
filter2_2
...
filter2_n
}
Such a construction (and others) is possible; perhaps some constraints may apply, but you will nevertheless be capable of providing more powerful flow control to your scripts. The rest of this section will show you how to implement them.
See? Right after the brackets example, it says, "Such a construction is possible." :P
A code example showing what can't be done :) That will teach me to read more carefully!
Sagekilla
10th November 2008, 15:57
It would be nice to be able to construct two blocks of filter operations and branch in a single step, as in the (ideal) Example 2 below:
It's suggesting it would be nice if it were possible. In this case, it's not. The documentation there is very confusing, but what it's trying to say is that you can't use brackets for block statements, but you can get around it by using other methods.
Also, I recommend you use my check for AR as:
clip = (ratio < 1.4) instead of clip = (ratio < 1.3).
TV ratio video ("fullscreen") isn't an even 1.3: That would suggest video would be 640x492. 640x480 (TV) != 640x492. The check for < 1.4 will handle a size down to 640x457, which won't automatically stretch just about every known video source (which 99% have an AR of at least 4/3).
Fixed code:
function StandardizedLoad(string file) {
clip = Avisource(file)
target_horiz = 720
target_vert = 300
ratio = float(clip.Width()) / clip.Height()
pad = int((clip.Height() * 2.4) - clip.Width())
clip = (ratio < 1.4) ? clip.AddBorders(pad/2,0,pad/2,0) : clip
return clip.BilinearResize(target_horiz, target_vert).AssumeFPS("nstc_film",true).ResampleAudio(48000)
}
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.