/** 
*
* The script uses one (or more) frames from each event on the selected video track, 
* and deletes everything else. All gaps between events are eliminated,
* resulting in a single smooth movie. 
* 
* The main use envisioned for this script is the creation of true time 
* lapse movies from the multiple one-second clips that many camcorders 
* record when put in "time-lapse" mode.
*
* A video track must be selected. If an audio track is selected, nothing happens.
* Only the first selected track is affected. If more than one video track is selected,
* subsequent tracks are ignored.
*
* This script uses the LAST frames in the event on the theory that first frame from 
* a time lapse capture may be slightly corrupted from tape startup glitches.
*
* Written By: John Meyer
* Written: October 22, 2003
* Updated for Vegas 7: May 11, 2007
*
**/ 

import System; 
import System.IO; 
import System.Windows.Forms; 
import Sony.Vegas; 


/**
**************************************************************************
* Start of main program                                                  *
**************************************************************************
**/

try { 

    //Change the next line to set the number of frames to keep.
    var SaveFrames : Timecode = new Timecode("00:00:00:01");  // Keep 1 frame (edit to keep more frames)
    var DeleteTime : double = SaveFrames.ToMilliseconds();    // Convert frames to milliseconds.

    //Global declarations
    var dStart : Double;
    var trackEnum : Enumerator; 
    var evnt : TrackEvent;

    //Use the FindSelectedTrack() function to find the first selected track.

    var track = FindSelectedTrack();
    var eventEnum = new Enumerator(track.Events);

    if (track.IsVideo()) {     // Proceed only if selected track is a video track.
      TrimEvents();             // Function that "trims" event down to "n" frames
    }
    Vegas.UpdateUI();  

} catch (e) { 
  MessageBox.Show(e); 
} 

// End of main program

// ***********************************************************************

// Beginning of functions

/**
**************************************************************************
* This function finds the first selected track                           *
**************************************************************************
**/

function FindSelectedTrack() : Track {
  trackEnum = new Enumerator(Vegas.Project.Tracks);
  while (!trackEnum.atEnd()) {
    var track : Track = Track(trackEnum.item());
    if (track.Selected) {
      return track;
    }
    trackEnum.moveNext();
  }
  return null;
}


/**
***************************************************************************
* The following function trims all events on the selected track           *
* by "n" (usually 1) frame(s). Only the LAST n frames in the event remain.*
***************************************************************************
**/

function TrimEvents() {

  dStart = 0;                                         // Initialize the starting point.

  //Go through each event on the track.

  while (!eventEnum.atEnd()) {

    evnt = TrackEvent(eventEnum.item());              // Get the next event on the timeline.
    evnt.Selected = true;
    evnt.AdjustStartLength(new Timecode(dStart), evnt.Length, false); // Move event to new location
  
    // (Delete the next line to leave frames at event start, instead of event end.)
    evnt.ActiveTake.Offset = (evnt.Length - SaveFrames) + evnt.ActiveTake.Offset; 
    evnt.AdjustStartLength(new Timecode(dStart), new Timecode(DeleteTime), false); // Adjust event length
 

    dStart = evnt.Start.ToMilliseconds() + DeleteTime; // Next event will start at end of this event.
    eventEnum.moveNext();                             // Go to next event on this timeline.
  } 

return null;
}
