View Single Post
Old 2nd March 2008, 00:29   #1  |  Link
mikeytown2
Resize Abuser
 
mikeytown2's Avatar
 
Join Date: Apr 2005
Location: Seattle, WA
Posts: 623
CutFrames: Opposite of Trim

Code:
# CutFrames() - March 9th, 2008
#  Cut a range of frames from a single a/v clip.
#
# Parameters: 
#  int start - start of cut
#  int end - end of cut
#  int duration - duration of fade between cuts
#  int fade_color - blank clip color (usually in Hex RGB) used at start or end of clip only
#  string transition - pass a function name, if user wants to use something other then Dissolve
#  val opt1 - pass a variable to the fade/transition function
#  val opt2 - pass a variable to the fade/transition function
#  val opt3 - pass a variable to the fade/transition function
# 
# Preconditions:
#  start >= 0 ; start can not be a negative value
#  start-duration > 0
#  start = 0 then a fade in is used
#  end = 0 cut till the end of the clip and a fade out is used
#  start != end ; mainly a condition to check for start == 0 and end == 0


function CutFrames(clip c, int start, int end, int "duration", int "fade_color", string "transition", val "opt1", val "opt2", val "opt3")
{
	# Set Defaults
	duration = default(duration, 0)
	transition = default(transition, "Dissolve")
	fade_color = default(fade_color,$000000)
	opt1=(Defined(opt1) ? ", "+string(opt1): "")
	opt2=(Defined(opt2) ? ", "+string(opt2): "")
	opt3=(Defined(opt3) ? ", "+string(opt3): "")
	opts = opt1 + opt2 + opt3
	end = c.Framecount()-1 == end ? 0: end
	d = c.BlankClip(duration, color=fade_color)
	
	# Check For Any Unreasonable Inputs
	Assert(start >= 0, "CutFrames: Starting Frame[" + String(start) + "] must be a positive value")
	Assert((start == 0) || (start-duration >= 0), "CutFrames: Starting Frame[" + String(start) + "] must be greater then or equal to" + Chr(10) + " inputed duration[" + String(duration) + "]")
	Assert(start <> end, "CutFrames: Start and End cannot both be the same")
	Assert(end <= c.Framecount()-1, "CutFrames: End[" + String(end) + "] is greater then " + Chr(10) + "clip length[" + String(c.Framecount()-1) + "]")
	Assert((end == 0) || (end+duration <= c.Framecount()-1), "CutFrames: Ending Frame[" + String(end) + "] must be less then or equal to" + Chr(10) + " last frame - duration [" + String(c.Framecount()-1-duration) + "]")

	# Select uncut frames (or blanks) as transition components:
	clip1 = (start == 0) ? d : c.trim(0, -start)
	clip2 = (end == 0) ? d : c.trim(end+1, 0)

	# Perform transition:
	Eval(transition + "(clip1, clip2," + string(duration) + opts + ")")
}
General Examples: - Assuming clip is 1000 frames in length
CutFrames(200, 400) - Cut out frames 200-400 from the video. Resulting length: 799
CutFrames(200, 400, 20) - Cut out frames 200-400 using Dissolve() for a 20 frame fade. Transition starts on frame 180; Resulting length: 779
CutFrames(210, 390, 20) - Cut out frames 210-390 using Dissolve() for a 20 frame fade. Transition starts on frame 190; Resulting length: 799
CutFrames(400, 200, 20) - Replay frames 200-400 using Dissolve() with a 20 frame fade. Transition starts on frame 380; Resulting length: 1179
CutFrames(200, 400, 20, $000000, "TransSwing", false, 1, 1) - Cut out frames 200-400 using TransSwing() for a 20 frame fade, passing 3 additional arguments to TransSwing(). Transition starts on frame 180; Resulting length: 779
CutFrames(0, 400) - Cut out frames 0-400 from the video. Resulting length: 599
CutFrames(0, 400, 20) - Cut out frames 0-400 from the video using Dissolve() for a 20 frame fade from black (FadeIn). Resulting length: 599
CutFrames(200, 0) - Cut out frames 200-1000 from the video. Resulting length: 199
CutFrames(200, 0, 20) - Cut out frames 200-1000 from the video using Dissolve() for a 20 frame fade into black (FadeOut). Resulting length: 199
CutFrames(0, 400, 20, $FF0000) - Cut out frames 0-400 from the video using Dissolve() for a 20 frame fade from red (FadeIn). Resulting length: 599

Tips:
  • When using this multiple times, work backwards. Cut frames from the end of the clip first. By doing this, your cuts will be independent from each other.
    Example:
    CutFrames(900, 950)
    CutFrames(800, 850)
    CutFrames(650, 700)
  • Inserting ShowFrameNumber(x=10, y=30) before any cutframes() will show what frame you're currently on.
  • If your looking for audio cuts add something like this before CutFrames()
    Code:
    Histogram("stereo")
    AddBorders(300, 0, 0, 0)
    Histogram("Audiolevels")
    ShowFrameNumber(x=width()-100, y=30)
    ShowSMPTE()
    Or a better way using AudioGraph(). Stickboy made a nice wrapper for stereo output. http://forum.doom9.org/showthread.php?s=&threadid=59412
    Code:
    Grayscale()
    ConvertToRGB()
    AudioGraph(Width()/96)
    ShowFrameNumber(x=10, y=30)
    ShowSMPTE()
    You can place these after all your CutFrames() to see the resulting sound/waveform.
  • Place your CutFrames() inside a function. This allows you to comment out all your cuts at once.
    Code:
    cut()
    Function Cut(clip c)
    {
    	c
    	CutFrames(254700, 0, 15)
    	CutFrames(194974, 254027, 15)
    	CutFrames(0, 189730, 15)
    }
    Taking this one step further, you can create multiple segments and order them in any way; selectively applying filters as well.
    Code:
    original = last
    segmentA(original)
    last + Grayscale(segmentB(original))
    function segmentA(clip c)
    {
    	c
    	CutFrames(254700, 0, 15)
    	CutFrames(194974, 254027, 15)
    	CutFrames(0, 189730, 15)
    }
    function segmentB(clip c)
    {
    	c
    	CutFrames(3390, 0, 15)
    	CutFrames(580, 2700, 15)
    	CutFrames(0, 460, 15)
    }

Footnotes:
Loop() can delete frames as well. Second to last example on page.

Future Ideas:
This is a RFC (Request For Comments), your input is greatly appreciated.

Edits:
March 4th, 2008: Got rid of ugly logic - Thanks Gavino
March 7th, 2008: Added error checking and FadeIn/FadeOut - Thanks stickboy for pointing out how this can fail and for header suggestions
March 8th, 2008: Improved End error checking
March 8th, 2008: Fixed cut points, now they are excluded - Thanks Gavino. Examples changed as a result.
March 9th, 2008: More code cleanup, added color option to fadeI/O, fixed end boundary condition - Thanks Gavino.

Last edited by mikeytown2; 15th March 2008 at 14:04.
mikeytown2 is offline   Reply With Quote