Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 12th March 2008, 10:28   #1  |  Link
mikeytown2
Resize Abuser
 
mikeytown2's Avatar
 
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
KenBurnsEffect() - Zoom, Pan & Rotate Clip. New Version Released May 27th, 2009!

http://en.wikipedia.org/wiki/Ken_Burns_Effect
Code:
# KenBurnsEffect() - May 27th, 2009
#  Given a clip, zooms and pans clip with the Ken Burns Effect.
# 
# Requirements:
#  zoom.dll
#   http://forum.doom9.org/showthread.php?t=49429				-Original zoom thread
#   http://avisynth.org/warpenterprises/files/zoom_25_dll_20050122.zip		-Direct Download
#  CalcBox(), ZoomBoxIt()
#   http://forum.doom9.org/showthread.php?p=1111789#post1111789
# 
# Inputs:
#  clip c: input clip, needs to be RGB32() if useZoomBox = 0 or 1
# Optional Parameters:
## <Starting Position> ##
#  Mode 2 - Starting Alignment:
#   int startAlign: -9 to +9 (0=Do not use Align). Positive values - fit with no black borders, may crop clip. Negative values - fit with black borders, may letterbox clip.
#                1:top left.     2:top center.     3:top right. 
#                4:middle left.  5:middle center.  6:middle bottom. 
#                7:bottom left.  8:bottom center.  9:bottom right.
#
#  Mode 1 - Starting Box:
#   float startX1: upper left x cord
#   float startY1: upper left y cord
#   float startX2: lower right x cord
#   float startY2: lower right y cord
#               3 or 4 out of 4 points must be defined when using Mode 1
#               when omitting a single point, it will calculate the missing value, with the Aspect Ratio in mind.
#  
#  float startZoomFactor: 100 = 100%, ect... 
#                      Negative values is the zoom of the pixels, positive values is the zoom of the frame. 
#                      When converting AR, this isn't perfect.
#  float startPanX: shift the clip x pixels. Value in source pixels. - left, + right
#  float startPanY: shift the clip x pixels. Value in source pixels. - up, + down
#  float startAngle: Rotate clip so many degrees. Positive is Clockwise.
## </Starting Position> ##
#
## <Ending Position> ##
#  Mode 2 - Ending Alignment:
#   int endAlign: -9 to +9 (0=Do not use Align). Positive values - fit with no black borders, may crop clip. Negative values - fit with black borders, may letterbox clip.
#                1:top left.     2:top center.     3:top right. 
#                4:middle left.  5:middle center.  6:middle bottom. 
#                7:bottom left.  8:bottom center.  9:bottom right.
#
#  Mode 1 - Ending Box:
#   float endX1: upper left x cord
#   float endY1: upper left y cord
#   float endX2: lower right x cord
#   float endY2: lower right y cord
#               3 or 4 out of 4 points must be defined when using Mode 1
#               when omitting a single point, it will calculate the missing value, with the Aspect Ratio in mind.
#  
#  float endZoomFactor: 100 = 100%, ect... 
#                      Negative values is the zoom of the pixels, positive values is the zoom of the frame. 
#                      When converting AR, this isn't perfect.
#  float endPanX: shift the clip x pixels. Value in source pixels. - left, + right
#  float endPanY: shift the clip x pixels. Value in source pixels. - up, + down
#  float endAngle: Rotate clip so many degrees. Positive is Clockwise.
## </Ending Position> ##
#
#  int width: Output Width
#  int height: Output Height
#  int startFrame: starting frame of pan and zoom
#  int endFrame: ending frame of pan and zoom
#  bool KeepState: If using start/end Frame, it keeps the clip at the start & end zoom level. Default = true
#  string ResizeMethod: name of resize function. Default = BilinearResize
#  float SourceDAR: Display Aspect Ratio of the Source. If 0 then it uses the source aspect ratio.
#  float TargetDAR: Display Aspect Ratio of the Target. Useful for DVD's. If 0 then it uses the source aspect ratio.
#
#  int useZoomBox: 0 - Never, 1 - for static parts, 2 - for everything. default 2
#  float Speed: 1 is "normal". For high quality set to something like 0.25. For speed set to 5+. Relevant if useZoomBox=0 or 1.
#  int cubic: Type of zoom, pan & rotate.
#  int cubicX: Type of pan in the x direction.
#  int cubicY: Type of pan in the y direction.
#  int cubicA: Type of rotation.
#  int cubicZ: Type of zoom.
#  int color: Color of letterbox border. Default: $000000 (Black). Irrelevant if useZoomBox=0 or 1.
# 
# 
# Notes:
#  Clip should be deinterlaced for best visual quality.
#  Aspect ratio for output and box(x1,y1,x2,y2) should be the same, unless you want to distort the clip.
#  The aspect ratio for the 2 boxes as well as the output should to be the same.
#  Default behavior is to center clip by adding borders (Align=-5).
#  If you specify Align (Mode 2) and x1,y1,x2,y2 (Mode 1), Mode 1 will be used and the align will control the position of the zoomFactor.
#  Mode 1 (x1,y1,x2,y2) can take values from a crop operation. x2 and y2 must be negative though; so if x2 or y2 equals 0, replace 0 with -0.00001.

function KenBurnsEffect(clip c, 
\ int "startAlign", float "startX1", float "startY1", float "startX2", float "startY2", float "startZoomFactor", float "startPanX", float "startPanY", float "startAngle",
\ int   "endAlign", float   "endX1", float   "endY1", float   "endX2", float   "endY2", float   "endZoomFactor", float   "endPanX", float   "endPanY", float   "endAngle",
\ int "width", int "height", int "StartFrame", int "EndFrame", bool "KeepState", string "ResizeMethod", float "SourcePAR", float "SourceDAR", float "TargetPAR", float "TargetDAR",
\ int "useZoomBox", float "speed", int "cubic", int "cubicX", int "cubicY", int "cubicA", int "cubicZ", int "color")

{
	#Set Defaults
	startAlign = Default(startAlign, -5)
	endAlign = Default(endAlign, -5)
	startZoomFactor = Default(startZoomFactor, 100.0)
	endZoomFactor = Default(endZoomFactor, 100.0)
	startPanX = Default(startPanX, 0.0)
	startPanY = Default(startPanY, 0.0)
	endPanX = Default(endPanX, 0.0)
	endPanY = Default(endPanY, 0.0)
	startAngle = Default(startAngle, 0.0)
	endAngle = Default(endAngle, 0.0)
	width = Default(width, c.width())
	height = Default(height, c.height())
	length = c.Framecount()-1
	StartFrame = Default(StartFrame, 0)
	EndFrame = Default(EndFrame, length)
	KeepState = Default(KeepState, True)
	ResizeMethod = Default(ResizeMethod, "BilinearResize")
	SourceAR = float(c.width())/float(c.height())
	TargetAR = float(width)/float(height)
	useZoomBox = Default(useZoomBox, 2)
	speed = Default(speed, 1)
	cubic = Default(cubic, 0)
	cubicX = Default(cubicX, cubic)
	cubicY = Default(cubicY, cubic)
	cubicZ = Default(cubicZ, cubic)
	cubicA = Default(cubicA, cubic)
	color = Default(color, $000000)
	
	#Calculate Defaults
	modzoom = Max(Float(c.width())/Float(width),Float(c.height())/Float(height))
	startZoomFactor = startZoomFactor<0 ? (abs(startZoomFactor)/100.0)*modzoom : (abs(startZoomFactor)/100.0)
	endZoomFactor = endZoomFactor<0 ? (abs(endZoomFactor)/100.0)*modzoom : (abs(endZoomFactor)/100.0)
	
	#Check For Any Unreasonable Inputs
	Assert(startZoomFactor<>0, "KenBurnsEffect: startZoomFactor can not be zero.")
	Assert(endZoomFactor<>0, "KenBurnsEffect: endZoomFactor can not be zero.")
	Assert((Defined(startX1) && Defined(startY1) && (Defined(startX2) || Defined(startY2))) || !(Defined(startX1) && Defined(startY1) && Defined(startX2) && Defined(startY2)), "KenBurnsEffect: when using Mode 1, you must define startX1,startY1, startX2 and/or startY2")
	Assert((Defined(endX1) && Defined(endY1) && (Defined(endX2) || Defined(endY2))) || !(Defined(endX1) && Defined(endY1) && Defined(endX2) && Defined(endY2)), "KenBurnsEffect: when using Mode 1, you must define endX1,endY1, endX2 and/or endY2")
	Assert(startAlign<9 || startAlign>-9, "KenBurnsEffect: startAlign [" + String(startAlign) + "] should be between -9 and 9.")
	Assert(endAlign<9 || endAlign>-9, "KenBurnsEffect: endAlign [" + String(endAlign) + "] should be between -9 and 9.")
	Assert(IsRGB32(c) || useZoomBox==2, "KenBurnsEffect: Clip Needs To Be RGB32. Use ConvertToRGB32() or set useZoomBox=2")
	Assert(!((cubic>0 || cubicX>0 || cubicY>0 || cubicZ>0 || cubicA>0) && useZoomBox==2), "KenBurnsEffect: Can not use the Cubic option when useZoomBox=2. Set useZoomBox to 0 or 1 or don't use cubic.")
	Assert(endFrame-startFrame<=length, "KenBurnsEffect: Transition length[" + String(endFrame-startFrame) + "] greater then clip lenght[" + String(length) + "]")
	Assert(startFrame<endFrame, "KenBurnsEffect: Starting Frame[" + String(startFrame) + "] greater then Ending Frame[" + String(endFrame) + "]")
	#Assert(useZoomBox == 0 && !(startAngle == 0), "KenBurnsEffect: You must set useZoomBox=0, if using startAngle [" + String(startAngle) + "]")
	#Assert(useZoomBox == 0 && !(endAngle == 0), "KenBurnsEffect: You must set useZoomBox=0, if using endAngle [" + String(endAngle) + "]")
	
	
	#Do Some Magic
	Eval (CalcBox(c, 2, width, height, TargetAR, SourceAR, startZoomFactor, startPanX, startPanY, startAlign, startX1, startY1, startX2, startY2, SourcePAR, SourceDAR, TargetPAR, TargetDAR))
	Eval (CalcBox(c, 3, width, height, TargetAR, SourceAR, endZoomFactor, endPanX, endPanY, endAlign, endX1, endY1, endX2, endY2, SourcePAR, SourceDAR, TargetPAR, TargetDAR))
	
	
	#Check For Any Unreasonable Inputs
	Assert(startX1<startX2, "KenBurnsEffect: Start X1[" + String(startX1) + "] point larger then Start X2 Point[" + String(startX2) + "]")
	Assert(startY1<startY2, "KenBurnsEffect: Start Y1[" + String(startY1) + "] point larger then Start Y2 Point[" + String(startY2) + "]")
	Assert(endX1<endX2, "KenBurnsEffect: End X1[" + String(endX1) + "] point larger then End X2 Point[" + String(endX2) + "]")
	Assert(endY1<endY2, "KenBurnsEffect: End Y1[" + String(endY1) + "] point larger then End Y2 Point[" + String(endY2) + "]")	

	
	#non pan frames 
	start = useZoomBox>0 && startFrame>0 ? KeepState ? c.Trim(0, -startFrame).ZoomBoxIt(width, height, ResizeMethod, startX1, startY1, startX2, startY2, color) : c.Trim(0, -startFrame) : nop()
	end = useZoomBox>0 && endFrame<length ? KeepState ? c.Trim(endFrame+1, 0).ZoomBoxIt(width, height, ResizeMethod, endX1, endY1, endX2, endY2, color) : c.Trim(endFrame+1, 0) : nop()
	#zoom and pan frames
	cAlt = c.Trim(startFrame, endFrame)
	cAlt = useZoomBox==2 ? Animate(0, cAlt.framecount()-1, "ZoomBoxIt", cAlt, width, height, ResizeMethod, Float(startX1),Float(startY1), Float(startX2),Float(startY2), color, cAlt, width, height, ResizeMethod, Float(endX1),Float(endY1),  Float(endX2),Float(endY2), color) :nop()

	
	#add in static frames
	cAlt = useZoomBox==2 && startFrame>0 ? start + cAlt : cAlt
	cAlt = useZoomBox==2 && endFrame<length ? cAlt + end : cAlt
	
	#get center of start rectangle from first box
	startCenterX = startX1+((startX2-startX1)/2.0)-0.5
	startCenterY = startY1+((startY2-startY1)/2.0)-0.5
	
	#get center of end rectangle from second box
	endCenterX = endX1+((endX2-endX1)/2.0)-0.5
	endCenterY = endY1+((endY2-endY1)/2.0)-0.5
	
	#Speed Logic pt 1 - Resize clip 
	width = Float(width)/speed
	height = Float(height)/speed	
	
	#get starting zoom factor from first box
	startZoomFactorX = width/float(startX2-startX1)
	startZoomFactorY = height/float(startY2-startY1)
	
	#get ending zoom factor from second box
	endZoomFactorX = width/float(endX2-endX1)
	endZoomFactorY = height/float(endY2-endY1)
	
	
	#Pan starting and ending x point
	srcxSE = SplineCalc(c, cubicX, startFrame, startCenterX, endFrame, endCenterX)
	
	#Pan starting and ending y point
	srcySE = SplineCalc(c, cubicY, startFrame, startCenterY, endFrame, endCenterY)
	
	#Zoom X
	factor_x_SE = SplineCalc(c, cubicZ, startFrame, startZoomFactorX, endFrame, endZoomFactorX)
	
	#Zoom Y
	factor_y_SE = SplineCalc(c, cubicZ, startFrame, startZoomFactorY, endFrame, endZoomFactorY)
	
	#Rotation
	angleSE = SplineCalc(c, cubicA, startFrame, startAngle, endFrame, endAngle)
	
	
	#non pan frames 
	start = useZoomBox==0 ? c.Zoom(srcx=String(startCenterX), srcy=String(startCenterY), factorx=String(startZoomFactorX), factory=String(startZoomFactorY), width=Round(width), height=Round(height), angle=String(startAngle)).Trim(0, -startFrame) : start
	end = useZoomBox==0   ? c.Zoom(srcx=String(endCenterX), srcy=String(endCenterY), factorx=String(endZoomFactorX), factory=String(endZoomFactorY), width=Round(width), height=Round(height), angle=String(endAngle)).Trim(endFrame+1, 0) : end
	
	#Do the zoom/pan
	c = useZoomBox<2 ? c.Zoom(srcx=srcxSE, srcy=srcySE, factorx=factor_x_SE, factory=factor_y_SE, width=Round(width), height=Round(height), angle=angleSE).Trim(startFrame, endFrame) : nop()
	
	#speed Logic pt 2 - Scale back up or down depending on speed set
	c = useZoomBox==1 ? Eval(ResizeMethod + "(c, " + String(Round(width*speed)) + ", " + String(Round(height*speed)) + ")" ) : c
		
	#Add in static frames
	c = useZoomBox<2 && startFrame>0 ? start + c : c
	c = useZoomBox<2 && endFrame<length ? c + end : c
	
	#speed Logic pt 2 - Scale back up or down depending on speed set
	c = useZoomBox==0 ? Eval(ResizeMethod + "(c, " + String(Round(width*speed)) + ", " + String(Round(height*speed)) + ")" ) : c
	useZoomBox==2 ? cAlt :c
}
KenBurnsEffect needs zoom.dll and ZoomBoxIt(), CalcBox() (next post).
Original zoom.dll Thread http://forum.doom9.org/showthread.php?t=49429
Details in Next Post (scroll down)

Last edited by mikeytown2; 28th May 2009 at 09:18.
mikeytown2 is offline   Reply With Quote
 

Tags
anamorphic, crop, dar, pan and scan, resize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:41.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.