Hello, I’m working on a script for Vegas Pro 14 that would restore all of the audio to video clips when the audio has been removed from the timeline. I tried the 15 day trial of Excalibur but it only restores one audio channel and I’m trying to get back all 6 audio channels for 5.1 audio.
I’d like to be able to recreate the same audio track layout that Vegas does on import:
Audio Track 1: channels 1 + 2 (stereo track)
Audio Track 2: channel 3 (mono track)
Audio Track 3: channel 4 (mono track)
Audio Track 4: channels 5 + 6 (stereo track)
One problem I’m having is I don’t see a way to get the individual audio channels directly from an AudioStream. Using audioStream.Channels I can see that the AudioStream contains 6 channels of audio but there doesn’t appear to be any getter methods in the API that give access to these audio channels.
My next thought is to create four separate AudioEvents and disable the audio channels in order to recreate the layout I mentioned above. AudioEvent #1 would have channels 1 & 2 and everything else disabled, AudioEvent #2 would have channel 3 with everything else disabled, and so on. Then add the four AudioEvents onto four audio tracks in the project and group them together with the video track.
The problem I’m having is that when trying to add a Take to an AudioEvent it causes an error:
audioEvent.AddTake(audioStream, take.IsActive);
And here is the error message:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ApplicationException: Invalid TrackEvent object.
at ScriptPortal.Vegas.TrackEvent.Validate()
at ScriptPortal.Vegas.TrackEvent.get_Takes()
at EntryPoint.FromVegas(Vegas vegas)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at ScriptPortal.Vegas.ScriptHost.ScriptManager.Run(Assembly asm, String className, String methodName)
at ScriptPortal.Vegas.ScriptHost.RunScript(Boolean fCompileOnly)
I also tried this, but it also caused an error:
audioEvent.Takes.Add(take);
This is my first Vegas script and I’m not sure if my method will work or if this is the best way to go about recovering all of the audio from a video clip. Any help will be much appreciated. I’ll also post my completed script once it is finished in case that helps anyone else.
Here is my code.
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using ScriptPortal.Vegas;
public class EntryPoint{
public void FromVegas(Vegas vegas){
// get selected video tracks from project
Track[] tracks = GetSelectedVideoTracks(vegas.Project);
// if no video tracks selected then exit
if (tracks.Length == 0){
MessageBox.Show("NO VIDEO TRACKS SELECTED");
return;
}
// get all events on each selected video track
foreach (Track track in tracks){
// add empty audio track(s) below selected video track
AudioTrack[] audioTracks = AddAudioTracksAfter(vegas.Project, track);
// get video events on track
TrackEvent[] events = GetTrackEvents(track);
// add audio events for each video event
foreach (TrackEvent trackEvent in events){
Take take = trackEvent.ActiveTake;
Media media = take.Media;
AudioStream audioStream = media.GetAudioStreamByIndex(0); // one AudioStream with multiple Channels
AudioEvent audioEvent = new AudioEvent(vegas.Project, trackEvent.Start, trackEvent.Length, null);
// **** BOTH THESE LINES CAUSE AN ERROR ****
// audioEvent.AddTake(audioStream, take.IsActive); // #1
// audioEvent.Takes.Add(take); // #2
}
}
}
// GET SELECTED VIDEO TRACKS
Track[] GetSelectedVideoTracks(Project project) {
List<Track> selectedTracks = new List<Track>();
foreach (Track track in project.Tracks) {
if (track.MediaType == MediaType.Video && track.Selected) {
selectedTracks.Add(track);
}
}
return selectedTracks.ToArray();
}
// ADD AUDIO TRACKS AFTER VIDEO TRACK
AudioTrack[] AddAudioTracksAfter(Project project, Track videoTrack){
int tracksToAdd = 6; // 5.1 AUDIO: mono tracks (for now)
List<AudioTrack> audioTracks = new List<AudioTrack>();
int videoIndex = videoTrack.Index;
for (int i=1, n=tracksToAdd+1; i<n; i++){
int insertIndex = videoIndex + i;
AudioTrack audioTrack = new AudioTrack(Project.ActiveProject, insertIndex, null);
project.Tracks.Add(audioTrack);
audioTracks.Add(audioTrack);
}
return audioTracks.ToArray();
}
// GET ALL EVENTS IN VIDEO TRACK
TrackEvent[] GetTrackEvents(Track track) {
List<TrackEvent> events = new List<TrackEvent>();
foreach (TrackEvent trackEvent in track.Events) {
events.Add(trackEvent);
}
return events.ToArray();
}
}