using ScriptPortal.Vegas; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Windows.Forms; namespace Quick_Render_by_iEmby { public partial class Form3 : Form { private Vegas vegas; private ArrayList SelectedTemplates = new ArrayList(); private List<RenderItem> renderItems = new List<RenderItem>(); enum RenderMode { Project = 0, Selection, Regions, } public Form3(Vegas vegas) { InitializeComponent(); this.vegas = vegas; AttachEventHandlers(); treeView1.CheckBoxes = true; FillTemplateTree(); } private void AttachEventHandlers() { button1.Click += new EventHandler(RenderSelection_Click); button2.Click += new EventHandler(RenderRegions_Click); button3.Click += new EventHandler(RenderProject_Click); button5.Click += new EventHandler(Cancel_Click); } private void RenderSelection_Click(object sender, EventArgs e) { foreach (TreeNode rendererNode in treeView1.Nodes) { foreach (TreeNode templateNode in rendererNode.Nodes) { if (templateNode.Checked) { RenderItem renderItem = (RenderItem)templateNode.Tag; SelectedTemplates.Add(renderItem); } } } if (SelectedTemplates.Count == 0) { MessageBox.Show("No render templates selected."); return; } // Proceed with batch rendering Render(RenderMode.Selection); } private void RenderRegions_Click(object sender, EventArgs e) { foreach (TreeNode rendererNode in treeView1.Nodes) { foreach (TreeNode templateNode in rendererNode.Nodes) { if (templateNode.Checked) { RenderItem renderItem = (RenderItem)templateNode.Tag; SelectedTemplates.Add(renderItem); } } } if (SelectedTemplates.Count == 0) { MessageBox.Show("No render templates selected."); return; } // Proceed with batch rendering InitializeBatchRendering(RenderMode.Regions); } private void RenderProject_Click(object sender, EventArgs e) { SelectedTemplates.Clear(); // Clear previous selections foreach (TreeNode rendererNode in treeView1.Nodes) { foreach (TreeNode templateNode in rendererNode.Nodes) { if (templateNode.Checked) { RenderItem renderItem = (RenderItem)templateNode.Tag; SelectedTemplates.Add(renderItem); } } } if (SelectedTemplates.Count == 0) { MessageBox.Show("No render templates selected."); return; } // Proceed with batch rendering InitializeBatchRendering(RenderMode.Project); } private void Cancel_Click(object sender, EventArgs e) { // Close the form or cancel the rendering process this.Close(); } private void Render(RenderMode renderMode) { // Prepare rendering based on the mode InitializeBatchRendering(renderMode); } private void InitializeBatchRendering(RenderMode renderMode) { // Check if the project is saved if (string.IsNullOrEmpty(vegas.Project.FilePath)) { // Prompt user to save the project if not saved DialogResult result = MessageBox.Show( "The project has not been saved. Do you want to save it now?", "Save Project", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if (result == DialogResult.Yes) { // Show SaveFileDialog SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "Vegas Project Files (*.veg)|*.veg", Title = "Save Project As" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { vegas.Project.SaveProject(saveFileDialog.FileName); } else { // User chose not to save the project, cancel rendering MessageBox.Show("Project must be saved before rendering.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { // User chose not to save the project, cancel rendering MessageBox.Show("Project must be saved before rendering.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Check if SelectedTemplates is valid if (SelectedTemplates == null || SelectedTemplates.Count == 0) { MessageBox.Show("No templates selected for rendering.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Initialize batch rendering with the generated output file path DoBatchRender(SelectedTemplates, renderMode); } private void DoBatchRender(ArrayList selectedTemplates, RenderMode renderMode) { List<RenderArgs> renders = new List<RenderArgs>(); string projectName = Path.GetFileNameWithoutExtension(vegas.Project.FilePath); foreach (RenderItem renderItem in selectedTemplates) { try { string projectFilePath = vegas.Project.FilePath; string projectDirectory = Path.GetDirectoryName(projectFilePath); string renderDirectory = Path.Combine(projectDirectory, "Renders"); Directory.CreateDirectory(renderDirectory); string outputFileName = Path.GetFileNameWithoutExtension(vegas.Project.FilePath); string outputFilePath = Path.Combine(renderDirectory, outputFileName); if (renderMode == RenderMode.Regions) { AddRegionRenders(renderItem, outputFilePath, renders); } else { AddSingleRender(renderItem, outputFilePath, renderMode, renders); } } catch (Exception e) { MessageBox.Show($"Error processing render item: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } foreach (RenderArgs args in renders) { try { if (DoRender(args) == RenderStatus.Canceled) break; } catch (Exception e) { MessageBox.Show($"Rendering failed: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void AddRegionRenders(RenderItem renderItem, string filename, List<RenderArgs> renders) { int regionIndex = 0; foreach (ScriptPortal.Vegas.Region region in vegas.Project.Regions) { string regionFilename = $"{filename}[{regionIndex}]{renderItem.Extension}"; string projectFilePath = vegas.Project.FilePath; string projectDirectory = Path.GetDirectoryName(projectFilePath); string renderDirectory = Path.Combine(projectDirectory, "Renders"); Directory.CreateDirectory(renderDirectory); string outputFileName = Path.GetFileNameWithoutExtension(vegas.Project.FilePath); string outputFilePath = Path.Combine(renderDirectory, outputFileName); RenderArgs args = new RenderArgs { OutputFile = outputFilePath + regionFilename, // Fixed the syntax issue here RenderTemplate = renderItem.Template, Start = region.Position, Length = region.Length, }; renders.Add(args); regionIndex++; } } private void AddSingleRender(RenderItem renderItem, string filename, RenderMode renderMode, List<RenderArgs> renders) { filename += renderItem.Extension; string projectFilePath = vegas.Project.FilePath; string projectDirectory = Path.GetDirectoryName(projectFilePath); string renderDirectory = Path.Combine(projectDirectory, "Renders"); Directory.CreateDirectory(renderDirectory); string outputFileName = Path.GetFileNameWithoutExtension(vegas.Project.FilePath); string outputFilePath = Path.Combine(renderDirectory, outputFileName); RenderArgs args = new RenderArgs { OutputFile = outputFilePath + filename, // Set 'overwrite' flag as needed RenderTemplate = renderItem.Template, UseSelection = (renderMode == RenderMode.Selection) }; renders.Add(args); } private RenderStatus DoRender(RenderArgs args) { RenderStatus status = vegas.Render(args); if (status == RenderStatus.Failed) { StringBuilder msg = new StringBuilder("Render failed:\n"); msg.Append("\n file name: ").Append(args.OutputFile); msg.Append("\n Template: ").Append(args.RenderTemplate.Name); throw new ApplicationException(msg.ToString()); } return status; } static Guid[] TheDefaultTemplateRenderClasses = { Renderer.CLSID_SfWaveRenderClass, Renderer.CLSID_SfW64ReaderClass, Renderer.CLSID_CSfAIFRenderFileClass, Renderer.CLSID_CSfFLACRenderFileClass, Renderer.CLSID_CSfPCARenderFileClass, }; bool AllowDefaultTemplates(Guid rendererID) { foreach (Guid guid in TheDefaultTemplateRenderClasses) { if (guid == rendererID) return true; } return false; } private void FillTemplateTree() { int projectAudioChannelCount = (vegas.Project.Audio.MasterBusMode == AudioBusMode.Stereo) ? 2 : 6; bool projectHasVideo = ProjectHasVideo(); bool projectHasAudio = ProjectHasAudio(); int projectVideoStreams = (!projectHasVideo || vegas.Project.Video.Stereo3DMode != Stereo3DOutputMode.Off) ? 2 : 1; foreach (Renderer renderer in vegas.Renderers) { try { string rendererName = renderer.FileTypeName; TreeNode rendererNode = new TreeNode(rendererName) { Tag = new RenderItem(renderer, null, null) }; foreach (RenderTemplate template in renderer.Templates) { try { if (!template.IsValid() || (!projectHasVideo && template.VideoStreamCount > 0) || (projectHasVideo && projectVideoStreams < template.VideoStreamCount) || (template.TemplateID == 0 && !AllowDefaultTemplates(renderer.ClassID)) || (!projectHasAudio && template.AudioStreamCount > 0 && template.VideoStreamCount == 0) || (projectAudioChannelCount < template.AudioChannelCount) || template.FileExtensions.Length != 1) { continue; } string templateName = template.Name; TreeNode templateNode = new TreeNode(templateName) { Tag = new RenderItem(renderer, template, template.FileExtensions[0]) }; rendererNode.Nodes.Add(templateNode); } catch (Exception e) { MessageBox.Show(e.ToString(), "Error"); } } treeView1.Nodes.Add(rendererNode); } catch (Exception e) { MessageBox.Show(e.ToString(), "Error"); } } } private bool ProjectHasVideo() { foreach (Track track in vegas.Project.Tracks) { if (track.IsVideo()) return true; } return false; } private bool ProjectHasAudio() { foreach (Track track in vegas.Project.Tracks) { if (track.IsAudio()) return true; } return false; } public class RenderItem { public Renderer Renderer { get; set; } public RenderTemplate Template { get; set; } public string Extension { get; set; } public RenderItem(Renderer renderer, RenderTemplate template, string extension) { Renderer = renderer; Template = template; Extension = extension; } } } }
I know we have already a batch render script but i need this for my another script