Log in

View Full Version : Handy resizing script


Tuesday
1st May 2006, 05:14
Hey,

After some brain twisting i've come up with my first ever script, the docs explain it all and i hope some one finds it useful.

I know it's pretty simple but i find it comes in handy.

# Resizer v1.0
# Created by Bear
#
# Just a simple function to make resizing videos that little bit easier.
#
# Arguments:
# ==========
#
# x & y = desired output size (Defualt: input size)
# AR = desired aspect ratio in the decomal form ie. 1.78 or 2.36,
# NOT 16:9 (Default: input AR)
# mod = number the final dimensions will be a multiple of ie, mod8,
# mod16 etc. (Default: 16)
# type = resizer used (Default: Bicubic)
# 1 = point
# 2 = bilinear
# 3 = bicubic
# 4 = lanczos
# b & c = parameters for bicubic resizer (if used)
# (Defualt: b=0.333, c=0.666)
#
# Usage:
# ======
#
# Using no arguments just makes the output mod16.
#
# If only X or Y dimension is input the output will scale using the input
# videos aspect ratio to meet this.
#
# If only an AR is entered the output will scale the X dimension to meet this.
#
# If only a mod is entered the output will be as close to the input as possible
# while meeting this.
#
# If an X AND Y dimension are specified the video will be scaled to as near as
# possible as this taking "mod" into account.
#
# I hope someone else finds this useful too, it's my first script

function Resizer( clip clp, int "x", int "y", float "AR", int "mod", int "type", float "b", float "c")
{

# Set defaults
xdest = default(x, clp.width)
ydest = default(y, clp.height)
AR = default(AR, (clp.width / clp.height))
mod = default(mod, 16)
type = default(type, 3)
b = default(b, 0.333)
c = default(c, 0.666)

# Sort out AR
xdest = (AR != (clp.width / clp.height) && xdest == clp.width) ? round(ydest * AR) : xdest
ydest = (AR != (clp.width / clp.height) && ydest == clp.height) ? round(xdest / AR) : ydest

# Modulize dimensions
xdiff = xdest % mod
x = (xdiff < (mod / 2)) ? xdest - xdiff : xdest + (mod - xdiff)

ydiff = ydest % mod
y = (ydiff < (mod / 2)) ? ydest - ydiff : ydest + (mod - ydiff)

# Select resizer
(type == 1) ? PointResize(clp, x, y) : NOP
(type == 2) ? BilinearResize(clp, x, y) : NOP
(type == 3) ? BicubicResize(clp, x, y, b, c) : NOP
(type == 4) ? LanczosResize(clp, x, y) : NOP

# Output
return last
}

Any comments are welcome, props to Wilbert & IanB for tips.

Mug Funky
1st May 2006, 06:40
nice.

would there need to be internal cropping used to preserve aspect ratio when resizing to mod16 (or any larger number)?

for example aspect ratio = 16:9 and destination (mod16) = 384x224, the output aspect ratio would actually be ~ 15.4:9

the difference usually isn't visible, but while you're writing a resizer script, you might as well do it automagically.

actionman133
1st May 2006, 22:21
Not bad, I actually I have a resize function amongst my script which I use for high quality DVD playback on my computer....

It uses implicit Last to make the video easier. Ratio is specified by a positive float as width : height (16/9.0, 4/3.0, etc). If Ratio = -1, it will maintain the full width and resize the height. If Ratio = -2, it will maintain the height and crop the width according to Size (which is the desired width). HeightMultiplier determines how much it must resize the height by in order to match the current width.

TopBar and BottomBar are user inputted and are integers, indicating the size of the widescreen bars, if any.

What I love about this code is its adaptability. It maintains mod2 height to ensure compatibility with YV12, and scales the cropping height to ensure that it doesn't use the wrong PAR. For example, I recently had an NTSC fullscreen video with a frame height of 274 (after black bars were removed. Each bar was 103 pixels). Resized, it would have been 720x300.5, obviously an impossible resize. It would round down the height to 300, and reduce the cropping height by the same degree (to about 273.6).

The only time it will convert to YUY2 is if the ratio = -2, so the user just has to make sure the top and bottom bars are cropped by either both even or both odd integers. I do this because I prefer to convert to keep the full frame when using this mode for playback. It's easy enough to ensure the height is mod2, if I want to convert...

The only thing the user has to do is make sure that Size is mod4...

# Resizing and cropping
NTSC = (Height == 480) || (Height == 240) ? True : False
Format = (Width == 480) ? "SVCD" : (Width == 352) && ((Height == 240) || Height == (288)) ? "VCD" : "DVD"
HeightMultiplier = (NTSC == True) ? 4739/4320.0 : 117/128.0
HeightMultiplier = (Anamorphic == True) ? HeightMultiplier * 0.75 : HeightMultiplier
HeightMultiplier = (Format == "SVCD") ? HeightMultiplier / 1.5 : HeightMultiplier

FrameHeight = Height () - (TopBar + BottomBar)
TestRatio = (Ratio == -1) ? Width / Float (FrameHeight * HeightMultiplier) : \
(Ratio == -2) ? Size / Float (FrameHeight) : Ratio
FinalHeight = Floor (Size / TestRatio)
FinalHeight = (ratio != -2 ) ? FinalHeight - (FinalHeight % 2) : FrameHeight
NewRatio = Size / Float (FinalHeight)

CropHeight = (Ratio != -2) ? FrameHeight * (TestRatio / NewRatio) : FrameHeight
CropWidth = (Ratio != -1) ? CropHeight * NewRatio * HeightMultiplier : Width ()
NewTopBar = TopBar + ((FrameHeight - CropHeight) * 0.5)
NewBottomBar = BottomBar + ((FrameHeight - CropHeight) * 0.5)
CropLeft = (Ratio != -1) ? (Width - CropWidth) * 0.5 : 0

(TopBar %2 != 0 || BottomBar %2 != 0) && ((FinalHeight == CropHeight) || Method == 1) ? ConvertToYUY2 () : Last

SharpWidth = (1.25 * Size / Float (CropWidth)) - 1
SharpWidth = (SharpWidth > 1) ? 1 : SharpWidth
SharpHeight = (1.25 * FinalHeight / Float (CropHeight)) - 1
SharpHeight = (SharpHeight > 1) ? 1 : SharpHeight

(Method == 1) ? Crop (0, TopBar, Width, FrameHeight) : \
(Method == 2) ? PointResize (Size, FinalHeight, CropLeft, NewTopBar, CropWidth, CropHeight) : \
(Method == 3) ? BilinearResize (Size, FinalHeight, CropLeft, NewTopBar, CropWidth, CropHeight) : \
(Method == 4) ? Lanczos4Resize (Size, FinalHeight, CropLeft, NewTopBar, CropWidth, CropHeight) : \
(Method == 5) ? Lanczos4Resize (Size, FinalHeight, CropLeft, NewTopBar, CropWidth, CropHeight). \
Sharpen (SharpWidth, SharpHeight) : Last
# End resizing and cropping