I have been looking for a script to be able to import some media, which I usually use during my compositions.
I found a script by Edward Troxel that did this, but using already present tracks on the timeline.
I am not very familiar with everything related to scripting, but taking a cue from that and also based on other scripts I had, I modified it to be able to get both the tracks and the media.
The problem I have now, however, is that the imported media has both video and audio ungrouped.
If anyone can give me a tip on how I could fix this, I would appreciate it!
I apologize for my less than perfect English!
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScriptPortal.Vegas;
namespace MediaToTimeline
{
public class EntryPoint
{
public void FromVegas(Vegas myVegas)
{
try
{
Timecode cursorPosition = myVegas.Transport.CursorPosition;
//this is just one example of media to import
string videoPath = "D:\\Youtube\\TV no signal.mp4";
// Create a video track if it does not exist and add the video event
int videoTrackIndex = -1;
for (int i = 0; i < myVegas.Project.Tracks.Count; i++)
{
if (myVegas.Project.Tracks[i] is VideoTrack)
{
videoTrackIndex = i;
break;
}
}
if (videoTrackIndex == -1)
{
VideoTrack videoTrack = new VideoTrack();
myVegas.Project.Tracks.Add(videoTrack);
videoTrackIndex = myVegas.Project.Tracks.Count - 1;
}
VideoEvent videoEvent = (VideoEvent)AddMedia(myVegas.Project, videoPath, videoTrackIndex, cursorPosition, Timecode.FromSeconds(0.2));
//Create an audio track if it does not exist and add the audio event
int audioTrackIndex = -1;
for (int i = 0; i < myVegas.Project.Tracks.Count; i++)
{
if (myVegas.Project.Tracks[i] is AudioTrack)
{
audioTrackIndex = i;
break;
}
}
if (audioTrackIndex == -1)
{
AudioTrack audioTrack = new AudioTrack();
myVegas.Project.Tracks.Add(audioTrack);
audioTrackIndex = myVegas.Project.Tracks.Count - 1;
}
AudioEvent audioEvent = (AudioEvent)AddMedia(myVegas.Project, videoPath, audioTrackIndex, cursorPosition, Timecode.FromSeconds(0.2));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
TrackEvent AddMedia(Project project, string mediaPath, int trackIndex, Timecode start, Timecode length)
{
Media media = Media.CreateInstance(project, mediaPath);
Track track = project.Tracks[trackIndex];
if (track.MediaType == MediaType.Video)
{
VideoTrack videoTrack = (VideoTrack)track;
VideoEvent videoEvent = videoTrack.AddVideoEvent(start, length);
Take take = videoEvent.AddTake(media.GetVideoStreamByIndex(0));
return videoEvent;
}
else if (track.MediaType == MediaType.Audio)
{
AudioTrack audioTrack = (AudioTrack)track;
AudioEvent audioEvent = audioTrack.AddAudioEvent(start, length);
Take take = audioEvent.AddTake(media.GetAudioStreamByIndex(0));
return audioEvent;
}
//should be impossible.
return null;
}
}
}
