I had some videos I needed to work in batch, but I needed to resize them and keep their aspect ratio. AviSynth is a great video post-production utility, but its resize functions won’t add black bars. I’ve written the following AviSynth script to fix that. The following functions will also try to use an even width/height if the format requires it.
It is released to the public domain.
function BilinearResizeBars(clip vid, int target_width, int target_height) {
orig_width = vid.width
orig_height = vid.height
return (target_width / target_height < orig_width / orig_height) ? \
vid.BilinearResizeBarsWidth(target_width, target_height) : \
vid.BilinearResizeBarsHeight(target_width, target_height)
}
function BilinearResizeBarsWidth(clip vid, int target_width, int target_height) {
orig_width = vid.width
orig_height = vid.height
try {
new_height = int(target_width * float(orig_height) / orig_width)
vid = vid.BilinearResize(target_width, new_height) \
.AddBorders(0, ceil((target_height - new_height) / 2), 0, \
floor((target_height - new_height) / 2))
} catch (error) { # Assuming that height must be even
new_height = int(target_width * float(orig_height) / orig_width)
vid = vid.BilinearResize(target_width, new_height - 1) \
.AddBorders(0, ceil((target_height - new_height) / 2), 0, \
floor((target_height - new_height) / 2) + 1)
}
return vid
}
function BilinearResizeBarsHeight(clip vid, int target_width, int target_height) {
orig_width = vid.width
orig_height = vid.height
try {
new_width = int(target_height * float(orig_width) / orig_height)
vid = vid.BilinearResize(new_width, target_height) \
.AddBorders(ceil((target_width - new_width) / 2), 0, \
floor((target_width - new_width) / 2), 0)
} catch (error) { # Assuming that width must be even
new_width = new_width
vid = vid.BilinearResize(new_width - 1, target_height) \
.AddBorders(ceil((target_width - new_width) / 2) + 1, 0, \
floor((target_width - new_width) / 2), 0)
}
return vid
}
orig_width = vid.width
orig_height = vid.height
return (target_width / target_height < orig_width / orig_height) ? \
vid.BilinearResizeBarsWidth(target_width, target_height) : \
vid.BilinearResizeBarsHeight(target_width, target_height)
}
function BilinearResizeBarsWidth(clip vid, int target_width, int target_height) {
orig_width = vid.width
orig_height = vid.height
try {
new_height = int(target_width * float(orig_height) / orig_width)
vid = vid.BilinearResize(target_width, new_height) \
.AddBorders(0, ceil((target_height - new_height) / 2), 0, \
floor((target_height - new_height) / 2))
} catch (error) { # Assuming that height must be even
new_height = int(target_width * float(orig_height) / orig_width)
vid = vid.BilinearResize(target_width, new_height - 1) \
.AddBorders(0, ceil((target_height - new_height) / 2), 0, \
floor((target_height - new_height) / 2) + 1)
}
return vid
}
function BilinearResizeBarsHeight(clip vid, int target_width, int target_height) {
orig_width = vid.width
orig_height = vid.height
try {
new_width = int(target_height * float(orig_width) / orig_height)
vid = vid.BilinearResize(new_width, target_height) \
.AddBorders(ceil((target_width - new_width) / 2), 0, \
floor((target_width - new_width) / 2), 0)
} catch (error) { # Assuming that width must be even
new_width = new_width
vid = vid.BilinearResize(new_width - 1, target_height) \
.AddBorders(ceil((target_width - new_width) / 2) + 1, 0, \
floor((target_width - new_width) / 2), 0)
}
return vid
}
I'm on Twitter!