代码: 全选
import vapoursynth as vs
import mvsfunc as mvf
import math
# BalanceBorders by PL — [2009.09.25] v0.2
# Port by Muonium 2017/8/14
# Source: https://www.dropbox.com/s/v8fm6om7hm1dz0b/BalanceBorders.avs
def scale(val, bits):
return val * ((1 << bits) - 1) // 255
def TurnRight(clip):
core = vs.get_core()
return core.std.FlipVertical(clip).std.Transpose()
def BalanceTopBorder(c, cTop, thresh, blur):
core = vs.get_core()
cWidth = c.width
cHeight = c.height
cTop = min(cTop, cHeight - 1)
blurWidth = max(4, math.floor(cWidth / blur))
c2 = mvf.PointPower(c, 1, 1)
#last = core.std.CropAbs(c2, cWidth * 2, 2, 0, cTop * 2)
last = core.std.CropRel(c2, 0, 0, cTop*2, (cHeight - cTop - 1) * 2)
last = core.resize.Point(last, cWidth * 2, cTop * 2)
last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
last = core.std.Convolution(last, [1, 1, 1], mode='h')
last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
referenceBlur = last
original = core.std.CropRel(c2, 0, 0, 0, (cHeight - cTop) * 2)
last = original
last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2)
last = core.std.Convolution(last, [1, 1, 1], mode='h')
last = core.resize.Bilinear(last, cWidth * 2, cTop * 2)
originalBlur = last
"""
balanced = core.std.Expr([original, originalBlur, referenceBlur], ['z y - x +'])
difference = core.std.MakeDiff(balanced, original)
tp = scale(128 + thresh, c.format.bits_per_sample)
tm = scale(128 - thresh, c.format.bits_per_sample)
difference = core.std.Expr([difference], ['x {tp} min {tm} max'.format(tp=tp, tm=tm)])
last = core.std.MergeDiff(original, difference)
"""
tp = scale(thresh, c.format.bits_per_sample)
tm = -tp
last = core.std.Expr([original, originalBlur, referenceBlur], ['z y - {tp} min {tm} max x +'.format(tp=tp, tm=tm)])
return core.std.StackVertical([last, core.std.CropRel(c2, 0, 0, cTop*2, 0)]).resize.Point(cWidth, cHeight)
def BalanceBorders(c, cTop=0, cBottom=0, cLeft=0, cRight=0, thresh=128, blur=999):
core = vs.get_core()
last = c
if cTop > 0:
last = BalanceTopBorder(last, cTop, thresh, blur)
last = TurnRight(last)
if cLeft > 0:
last = BalanceTopBorder(last, cLeft, thresh, blur)
last = TurnRight(last)
if cBottom > 0:
last = BalanceTopBorder(last, cBottom, thresh, blur)
last = TurnRight(last)
if cRight > 0:
last = BalanceTopBorder(last, cRight, thresh, blur)
last = TurnRight(last)
return last
感谢移植,试了一下,预览效果不错,不知压制怎么样了,期待中Muonium 写了:BalanceBorders.py:删掉了 avsi 中对色彩空间的限制。自己注意 blur 和 thresh 两个参数大于0就好,懒得写范围检查了。代码: 全选
import vapoursynth as vs import mvsfunc as mvf import math # BalanceBorders by PL — [2009.09.25] v0.2 # Port by Muonium 2017/8/14 # Source: https://www.dropbox.com/s/v8fm6om7hm1dz0b/BalanceBorders.avs def scale(val, bits): return val * ((1 << bits) - 1) // 255 def TurnRight(clip): core = vs.get_core() return core.std.FlipVertical(clip).std.Transpose() def BalanceTopBorder(c, cTop, thresh, blur): core = vs.get_core() cWidth = c.width cHeight = c.height cTop = min(cTop, cHeight - 1) blurWidth = max(4, math.floor(cWidth / blur)) c2 = mvf.PointPower(c, 1, 1) last = core.std.CropAbs(c2, cWidth * 2, 2, 0, cTop * 2) last = core.resize.Point(last, cWidth * 2, cTop * 2) last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2) last = core.std.Convolution(last, [1, 1, 1], mode='h') last = core.resize.Bilinear(last, cWidth * 2, cTop * 2) referenceBlur = last original = core.std.CropAbs(c2, cWidth * 2, cTop * 2, 0, 0) last = original last = core.resize.Bilinear(last, blurWidth * 2, cTop * 2) last = core.std.Convolution(last, [1, 1, 1], mode='h') last = core.resize.Bilinear(last, cWidth * 2, cTop * 2) originalBlur = last balanced = core.std.Expr([original, originalBlur, referenceBlur], ['z y - x +']) difference = core.std.MakeDiff(balanced, original) tp = scale(128 + thresh, c.format.bits_per_sample) tm = scale(128 - thresh, c.format.bits_per_sample) difference = core.std.Expr([difference], ['x {tp} min {tm} max'.format(tp=tp, tm=tm)]) last = core.std.MergeDiff(original, difference) return core.std.StackVertical([last, core.std.CropAbs(c2, cWidth * 2, (cHeight - cTop) * 2, 0, cTop * 2)]).resize.Point(cWidth, cHeight) def BalanceBorders(c, cTop=0, cBottom=0, cLeft=0, cRight=0, thresh=128, blur=999): core = vs.get_core() last = c if cTop > 0: last = BalanceTopBorder(last, cTop, thresh, blur) last = TurnRight(last) if cLeft > 0: last = BalanceTopBorder(last, cLeft, thresh, blur) last = TurnRight(last) if cBottom > 0: last = BalanceTopBorder(last, cBottom, thresh, blur) last = TurnRight(last) if cRight > 0: last = BalanceTopBorder(last, cRight, thresh, blur) last = TurnRight(last) return last
thresh 会自动根据视频的位深进行调整,范围和 avsi 中的一样。
俄文文档就不翻译了
这论坛的Codebox是怎么用的?