Tracks and Events
You can find the currently selected track or event by iterating through each track and event and examining the Selected property. The following functions provide an example of this:
Track FindSelectedTrack(Project project) { foreach (Track track in project.Tracks) { if (track.Selected) { return track; } } return null; } TrackEvent[] FindSelectedEvents(Project project) { List<TrackEvent> selectedEvents = new List<TrackEvent>(); foreach (Track track in project.Tracks) { foreach (TrackEvent trackEvent in track.Events) { if (trackEvent.Selected) { selectedEvents.Add(trackEvent); } } } return selectedEvents.ToArray(); }
Note that multiple tracks and events selected at the same time. The first function returns only the first selected track while the second function returns all of the selected events. Events can be selected in tracks that are not selected.
Every TrackEvent object has FadeIn and FadeOut properties which give you objects that control the event's ASR (Attack, Sustain, Release), and in the case of VideoEvents control of transition effects. The Fade object has a Curve property which can be set to one of the CurveType enumeration values. The following example gives an event fade in curve the fast type.
evnt.FadeIn.Curve = CurveType.Fast;
One tricky aspect of fades comes into play when two events overlap on the same track. In this case, only the trailing event's FadeIn really matters and, to designate the type of fade curve for the leading event, you actually must set the trailing event's ReciprocalCurve property. The following code snippet makes a slow curved fade transition between an event (trailingEvent) and any event on the same track that overlaps its leading edge:
trailingEvent.FadeIn.Curve = CurveType.Slow; trailingEvent.FadeIn.ReciprocalCurve = CurveType.Slow;
You can add a dissolve (or any transition) to an event using its FadeIn and FadeOut properties. First you can set the length of the transition to the desired amount:
videoEvent.FadeIn.Length = Timecode.FromSeconds(1);
or
videoEvent.FadeOut.Length = Timecode.FromSeconds(1);
For video events, you can use a transition effect by creating a new instance of an Effect object using a transition PlugInNode then assign it to the Transition property of the appropriate Fade. To create a new transition effect, you must first find the appropriate PlugInNode. To find a transition node by name, use the GetChildByName method of the Transitions root node. The following function creates a new transition effect given its plug-in name:
Effect CreateTransitionEffect(Vegas vegas, String plugInName) { PlugInNode plugIn = vegas.Transitions.GetChildByName(plugInName); if (null == plugIn) throw new ApplicationException(String.Format("Failed to find plug-in: '{0}'", plugInName)); return new Effect(plugIn); }
Now you can set the event fade transitions and assign their preset:
Effect fadeInFx = CreateTransitionEffect(vegas,"Dissolve"); videoEvent.FadeIn.Transition = fadeInFx; fadeInFx.Preset = "Additive Dissolve"; Effect fadeOutFx = CreateTransitionEffect(vegas,"Slide"); videoEvent.FadeOut.Transition = fadeOutFx; fadeOutFx.Preset = "Slide Out, Left-Right";
You can use the OpenFile method of the VEGAS object to add a media file to the selected track at the current cursor position. The OpenFile method works much like the 'File.Open' menu command and will insert tracks if necessary. You can set the cursor position using the CursorPosition of the Vegas object's Transport property. The following method demonstrates how to insert a media file at a specific position on a specific track using the OpenFile method.
void InsertFileAt(Vegas vegas, String fileName, int trackIndex, Timecode cursorPosition) { // first clear all track selections foreach (Track track in vegas.Project.Tracks) { track.Selected = false; } // select the desired track vegas.Project.Tracks[trackIndex].Selected = true; // set the cursor position vegas.Transport.CursorPosition = cursorPosition; vegas.OpenFile(fileName); }
Sometimes you need more control of how you add media to the project. You can build events on the timeline by constructing media, tracks, events, and take objects individually:
VideoEvent AddVideoEvent(Vegas vegas, String mediaFile, Timecode start, Timecode length) { Media media = new Media(mediaFile); VideoTrack track = vegas.Project.AddVideoTrack(); VideoEvent videoEvent = track.AddVideoEvent(start, length); Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0)); return videoEvent; }
You can add a text event to the timeline by constructing a Media object from the text generator effect and adding the video event and take to the track.
VideoEvent AddTextEvent(Vegas vegas, VideoTrack track, Timecode start, Timecode length) { // find the text generator plug-in PlugInNode plugIn = vegas.Generators.GetChildByName("Titles & Text"); // create a media object with the generator plug-in Media media = new Media(plugIn); // set the generator preset media.Generator.Preset = "(Default)"; // add the video event VideoEvent videoEvent = track.AddVideoEvent(start, length); // add the take using the generated video stream Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0)); return videoEvent; }
This example method puts the specified text into the specified Titles & Text effect.
void SetTextForTitle(VideoEvent ev,string text) { // verify the event is of the expected type if ( (ev.ActiveTake.Media.Generator != null) && ev.ActiveTake.Media.Generator.PlugIn.Name == "Titles & Text") { Effect fxTX = ev.ActiveTake.Media.Generator; OFXEffect ofxTX = fxTX.OFXEffect; OFXStringParameter parText = ofxTX.FindParameterByName("Text") as OFXStringParameter; // ..... // Titles & Text uses RTF formatted text. This example uses // the .NET WindowsForms RichTextBox to assist with manipulation of the // text without having to know the details of the RTF text representation // ..... // retrieve the existing RTF format text from the effect instance // to a local RTF component RichTextBox rbx = new RichTextBox(); rbx.Rtf = parText.Value; // capture the font and alignment of the current text rbx.SelectAll(); System.Drawing.Font savedFont = rbx.SelectionFont; HorizontalAlignment savedAlignment = rbx.SelectionAlignment; // clear the content and set the new text rbx.Rtf = ""; rbx.AppendText(text); // restore the font and alignment rbx.SelectAll(); rbx.SelectionFont = savedFont; rbx.SelectionAlignment = savedAlignment; // push the revised RTF format text to the Titles & Text Effect parText.Value = rbx.Rtf; } }
Pan and crop settings can be manipulated using the VideoEvent's VideoMotion property. The VideoMotion object has a Keyframes collection that contains VideoMotionKeyframe objects. Each keyframe has a Position in time relative to the start of the event. Keyframes also have a Bounds polygon representing the four corners of the view-port onto the source media. The best way to manipulate this bounds rectangle is to use the keyframe's MoveBy, ScaleBy, and RotateBy methods. The keyframe's Center defines the point at the center of rotation.
The following function moves a video event into view from a position off screen over a two second period.
void PanFromLeft(Vegas vegas, VideoEvent videoEvent) { // create a new keyframe at 2 seconds. VideoMotionKeyframe key1 = new VideoMotionKeyframe(Timecode.FromSeconds(2)); // add the new keyframe videoEvent.VideoMotion.Keyframes.Add(key1); // get the first keyframe VideoMotionKeyframe key0 = videoEvent.VideoMotion.Keyframes[0]; // get the width of the project int videoWidth = vegas.Project.Video.Width; // move the first keyframe just off screen key0.MoveBy(new VideoMotionVertex(videoWidth, 0)); }
You can to set the CompositeLevel property of a VideoTrack object to effect the entire track's opacity level.
videoTrack.CompositeLevel = 0.5f;
You can also add a composite level envelope to the video track (described below) if you need to the opacity to change over time.
Individual event opacity can be set using the Gain property of the video event's FadeIn object:
videoEvent.FadeIn.Gain = 0.5;