private void TranscodeMedia(RenderTemplate renderTemplate, string fileExtension) { List<TrackEvent> selectedEvents = GetSelectedEvents(vegas); if (selectedEvents == null || selectedEvents.Count == 0) { MessageBox.Show("No Event Selected.", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Create a list to store unique media objects List<Media> selectedMediaList = new List<Media>(); // Iterate over the selected events and add their media to the list foreach (TrackEvent ev in selectedEvents) { Media media = ev.ActiveTake.Media; if (media != null && !selectedMediaList.Contains(media)) { selectedMediaList.Add(media); } } // Ask user if they want to replace the original media with the transcoded media DialogResult result = MessageBox.Show("Replace the original media with the transcoded media?", "Replace Media", MessageBoxButtons.YesNo, MessageBoxIcon.Question); bool replaceMedia = (result == DialogResult.Yes); foreach (Media media in selectedMediaList) { if (media == null) { MessageBox.Show("Selected event has no media linked!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); continue; } // Calculate the new event position Timecode newEventStart = vegas.Project.Length + Timecode.FromMilliseconds(5000); // Create new tracks and add media as events VideoTrack videoTrack = new VideoTrack(vegas.Project.Tracks.Count, "TranscodeVideoTrack"); vegas.Project.Tracks.Add(videoTrack); AudioTrack audioTrack = new AudioTrack(vegas.Project.Tracks.Count + 1, "TranscodeAudioTrack"); vegas.Project.Tracks.Add(audioTrack); // Add new video and audio events TrackEvent newVideoEvent = videoTrack.AddVideoEvent(newEventStart, media.Length); TrackEvent newAudioEvent = audioTrack.AddAudioEvent(newEventStart, media.Length); // Set media streams for the new events MediaStream videoStream = media.GetVideoStreamByIndex(0); MediaStream audioStream = media.GetAudioStreamByIndex(0); if (videoStream != null) { newVideoEvent.AddTake(videoStream); } if (audioStream != null) { newAudioEvent.AddTake(audioStream); } // Create a loop region around the new event vegas.Transport.LoopRegionStart = newEventStart; vegas.Transport.LoopRegionLength = media.Length; // Define the output file path string mediaDirectory = Path.GetDirectoryName(media.FilePath); string outputFileName = $"{Path.GetFileNameWithoutExtension(media.FilePath)}_transcoded{fileExtension}"; string outputFilePathForMedia = Path.Combine(mediaDirectory, outputFileName); // Setup render arguments RenderArgs renderArgs = new RenderArgs { OutputFile = outputFilePathForMedia, RenderTemplate = renderTemplate, Start = newEventStart, Length = media.Length }; // Render the project RenderProject(renderArgs); // Replace or remove the event based on user's choice if (replaceMedia) { Media newMedia = new Media(outputFilePathForMedia); // Create new Media object for the transcoded file // Replace the original media in all events that use this media foreach (TrackEvent selectedEvent in selectedEvents) { if (selectedEvent.ActiveTake.Media == media) { selectedEvent.ActiveTake.Media.ReplaceWith(newMedia); } } // Remove the temporary events and tracks videoTrack.Events.Remove(newVideoEvent); audioTrack.Events.Remove(newAudioEvent); vegas.Project.Tracks.Remove(videoTrack); vegas.Project.Tracks.Remove(audioTrack); } else { // Just remove the temporary events and tracks videoTrack.Events.Remove(newVideoEvent); audioTrack.Events.Remove(newAudioEvent); vegas.Project.Tracks.Remove(videoTrack); vegas.Project.Tracks.Remove(audioTrack); } vegas.UpdateUI(); } }