grass valley developers

Home > APIs > AppServer API > Examples > Asset Management > Import and Delete Tracks from a Clip

Import and Delete Tracks from a Clip

The sample code below shows how to import tracks from one clip to another. It also shows how to label tracks, delete tracks, and move tracks. Other track functionality can be found in the K2 API under the ITrackEditor interface.

IMediaMgr mediaMgr = iappServer.CreateMediaMgr(appName);

// define clip to modify and imported WAV clip
string clip = "edl/cmf//local/V:/default/Clip";
string wavClip = "edl/cmf//local/V:/default/wavClip";

// Note: this test app is using an imported clip named "wavClip". If you wanted to import a temporary clip that 
// you did not want to appear in AppCenter's list of clips, you could import a clip and give it a name that starts 
// with a tilde (~). All clips starting with tilde's are treated as temporary clips. They will not appear in AppCenter's
// list of clips and they will automatically be deleted after a little while...

// get editors for both clips
ISimpleEditor wavEditor = (ISimpleEditor) mediaMgr.CreateEditor(wavClip);
ISimpleEditor clipEditor = (ISimpleEditor) mediaMgr.CreateEditor(clip);
ITrackEditor clipTrackEditor = (ITrackEditor) clipEditor;

// get the XML representation of the WAV clip
string wavXml;
wavEditor.GetXml("", out wavXml);
// Console.WriteLine(wavXml);
// we're finished with wavEditor, so get rid of the editor and clean up

// load the XML string into an XML document so we can parse for Tracks
XmlDocument xmlDoc = new XmlDocument();

// load the namespace into a namespace manager. used for parsing the xml doc
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("ns", @"PdrDB__1.0");

// loop thru all tracks and look for audio tracks only (i.e TrackType="PdrAudioTrack". Uncomment the above 
// Console.WriteLine(wavXml) to see all XML data including other TrackTypes.)
XmlNodeList trackList = xmlDoc.SelectNodes(("//ns:XML/ns:Data/ns:Movie/ns:Track"), namespaceManager);

// if our clip has tracks, loop thru all tracks
if (trackList.Count > 0)
	// setup enumerator for loop
	IEnumerator trackNodeEnum = trackList.GetEnumerator();
	bool bMoreTracks = trackNodeEnum.MoveNext();

		// Get the TrackType, TrackNumber, and TrackId attribues from the Track node
		XmlNode trackNode = (XmlNode)trackNodeEnum.Current;
		string trackType = trackNode.Attributes.GetNamedItem("TrackType").Value;
		int trackNumber = Int32.Parse ( trackNode.Attributes.GetNamedItem("TrackNumber").Value );
		string trackId = trackNode.Attributes.GetNamedItem("TrackId").Value;

		// if we found a "PdrAudioTrack, add it to the Clip
		if ( 0 == String.Compare( trackType, "PdrAudioTrack", true) )
			Console.WriteLine("Track type: {0}  number: {1}  ID: {2}", trackType, trackNumber, trackId);

			// add WAV audio track to Clip
			int addedTrackIndex;
			string addedTrackId;
			clipTrackEditor.AddTrack(wavClip, trackId, Int32.MinValue, Int32.MaxValue, 
				0, out addedTrackIndex, out addedTrackId);

			Console.WriteLine("Added track index: {0}  ID: {1}", addedTrackIndex, addedTrackId);
			// Let's also setup a label for this track. You can see these in AppCenter in clip properties page under the Tracks button.
			string label = "xyz";
			Console.WriteLine("Set track {0} label to '{1}'", addedTrackId, label);
			clipTrackEditor.SetTrackLabel(addedTrackId, label);

			/*   uncomment to test
			// delete tracks from Clip
			Console.WriteLine("Delete track {0}", addedTrackId);

			/*   uncomment to test
			// Add blank/empty tracks to Clip, available in 7.2 Summit Code
			// 1. Add a video track
			clipTrackEditor.AddTrack("", "PdrVideoTrack", 0, 0, 0, out addedTrackIndex, out addedTrackId);
			Console.WriteLine("Added blank video track {0}", addedTrackId);
			// 2. Add an audio track
			clipTrackEditor.AddTrack("", "PdrAudioTrack", 0, 0, 0, out addedTrackIndex, out addedTrackId);
			Console.WriteLine("Added blank audio track {0}", addedTrackId);
			// 3. Add a timecode track
			clipTrackEditor.AddTrack("", "PdrTimeCodeTrack", 0, 0, 0, out addedTrackIndex, out addedTrackId);
			Console.WriteLine("Added blank timecode track {0}", addedTrackId);
			/*   uncomment to test
			// move track position in Clip
			int position = 0;
			Console.WriteLine("Move track {0} to position {1}", addedTrackId, position);
			clipTrackEditor.MoveTrack(addedTrackId, position);										

		bMoreTracks = trackNodeEnum.MoveNext();
	} while (bMoreTracks);
} // if trackList.Count > 0							

// cleanup - make sure you detach or movie ref will remain open!
clipEditor = null;