Is it possible to create a RenderTemplate using the Vegas Pro 22 API?

Risae wrote on 5/31/2025, 2:35 AM

Hi Vegas Community,

i am trying to automate workflows using the Vegas Pro 22 API and c# scripts.

I am currenty trying to create a new RenderTemplate, but the documentation doesn't really tell me how i can accomplish that: https://help.magix-hub.com/video/vegas/22/en/content/topics/external/vegasscriptapi.html

RenderTemplate CreateRenderTemplate(Byte[] templateData)
Create a RenderTemplate from the template data.
Parameters:
templateData: 

RenderTemplate CreateRenderTemplate(UInt32 templateID)
Create a RenderTemplate of the given templateID.
Parameters:
templateID: 

Does anybody know how i can create a new rendertemplate? Or is this currently only possible through the Vegas Pro 22 GUI?

Comments

zzzzzz9125 wrote on 5/31/2025, 6:09 AM

@Risae I can offer you some ideas, but I can't really complete it. It is a very complicated matter to parse it manually.

First of all, we need to save a Render Template as a preset. After that, we can find it in the following path:

C:\Users\<UserName>\AppData\Roaming\VEGAS\Render Templates\

In this way, we can get a Render Template file, which is an example.

 

In the script, we can use the following code to read it and start rendering with it:

string roamingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string filePath = Path.Combine(roamingPath, "VEGAS", "Render Templates", "avc", "Internet HD 1080p 59.94 fps (NVENC)_Test.sft2");
byte[] bytes = File.ReadAllBytes(filePath);
Renderer renderer = myVegas.Renderers.FindByClassID(new Guid("4c184f1e-4d99-4353-9de0-e49da388cb63"));
if (renderer != null)
{
    RenderTemplate template = renderer.CreateRenderTemplate(bytes);
    if (template != null)
    {
        string str = string.Format("FilePath: {0}\nName: {1}\nDescription: {2}\nWidth: {3}\nHeight: {4}\nFrameRate: {5}", filePath, template.Name, template.Description, template.VideoWidth, template.VideoHeight, template.VideoFrameRate);
        DialogResult result = MessageBox.Show(string.Format("Template Info:\n{0}\n\nStart rendering?", str), "Render Template", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string outputPath = Path.Combine(desktopPath, "test.mp4");
            myVegas.Project.Render(outputPath, template);
        }
    }
}

It shows:

And starts rendering when we press "Yes".

 

However, how can we modify its parameters through the script? This is a very, very difficult question, which requires us to be proficient in manually parsing binary data.

Open this file using a hex editor. I 'm using an online one: https://hexed.it/.

Suppose we want to modify the width value 1920 to 1440. We search for the integer type 1920 from these binary data and replace them one by one with 1440.

After that, we save and re-read it using a script:

It works.

Of course, we can do this in a script, but it requires a bit more code.

The difficulty in editing binary data lies in that it is very hard for us to know exactly how to edit it correctly. Moreover, many binary data will indicate the data length at the beginning. Therefore, when the data length (i.e., the file size) changes, we also need to modify the length value at the beginning of the data.

 

All in all, it requires us to be very patient in trying to understand and modify each parameter. I haven't seen a better way to customize the parameters of Render Templates.

Last changed by zzzzzz9125 on 5/31/2025, 6:23 AM, changed a total of 6 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

jetdv wrote on 5/31/2025, 9:02 AM

You'd really be better off creating whatever presets you want in VEGAS and then using those presets from a script.

Risae wrote on 5/31/2025, 9:18 AM

Hi zzzzzz9125,

first off, thank you very much for taking the time to take a look at this!
Background why i want to create a RenderTemplate through the API:

I am working on an english fan translation of a Playstation 2 JRPG. I am currently using Vegas Pro 22 to add subtitles to a demuxed PSS file that is just a .m2v:

General
Complete name                            : 00000006_video_0.m2v
Format                                   : MPEG Video
Format version                           : Version 2
File size                                : 40.1 MiB
Duration                                 : 53 s 20 ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 6 342 kb/s
Frame rate                               : 29.970 FPS
Writing library                          : TMPGEnc 2.524.63.181Video
Format                                   : MPEG Video
Format version                           : Version 2
Format profile                           : Main@Main
Format settings                          : CustomMatrix / BVOP
Format settings, BVOP                    : Yes
Format settings, Matrix                  : Custom
Format settings, GOP                     : M=3, N=18
Duration                                 : 53 s 20 ms
Bit rate mode                            : Variable
Bit rate                                 : 6 342 kb/s
Maximum bit rate                         : 15.0 Mb/s
Width                                    : 640 pixels
Height                                   : 448 pixels
Display aspect ratio                     : 1.429
Frame rate                               : 29.970 (30000/1001) FPS
Standard                                 : NTSC
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Compression mode                         : Lossy
Bits/(Pixel*Frame)                       : 0.738
Time code of first frame                 : 00:00:00:00
GOP, Open/Closed                         : Open
GOP, Open/Closed of first frame          : Closed
Stream size                              : 40.1 MiB (100%)
Writing library                          : TMPGEnc 2.524.63.181
Color primaries                          : BT.601 NTSC
Transfer characteristics                 : BT.601
Matrix coefficients                      : BT.601

At first i tried using FFmpeg, but for some ungodly reason the game doesn't properly "close" the movie at the end and just a black screen is displaying (unless the user presses a cancel button).
I tried Vegas Pro 22 with the following preset:

-- Video --
Output type: MPEG-2
Width: 640
Height: 448
I-frames: 18
B-frames: 2
Frame rate: 29.970
Aspect ratio: Square pixels
Insert I-frames at markers: FALSE
Profile: Main
Level: Main
Field order: None (progressive scan)
Video quality: 31 (High)
Insert sequence header before every GOP: TRUE
Contant bit rate (bps): TRUE 6.000.000

Format: Video: 29,970 fps; 640x448 Progressive; YUV; 6 Mbps
Pixel Aspect Ratio: 1,000

-- Advanced Video --
Write sequence end code: TRUE
Write sequence display extension: FALSE
Allow field-based motion compensation: FALSE (cant be made true)
Use closed GOPs: FALSE
Length of GOPs: None short (can't be changed)
Non-Linear Quantization:
- I Frames: TRUE
- P Frames: TRUE
- B Frames: TRUE
Optimize to Table 1:
- I Frames: TRUE
- P Frames: TRUE
- B Frames: TRUE
Alternate Scan Pattern:
- I Frames: FALSE
- P Frames: FALSE
- B Frames: FALSE
DC coefficient: 8 Bit
Video format: NTSC
Color primaries: SMPTE 170M
Transfer: SMPTE 170M
Matrix coefficients: ITU-R Rec. 624-4 System B, G
VBV buffer size (KB): 224
Sampling format: 4:2:0 (can't be changed)

-- System --
Save as separate elementary streams: TRUE

And i was able to add subtitles, kinda keep the quality, remux is back to the PSS file (using ps2str) and add it back to the game -> the movie closed properly after it finished.

So, i don't like doing stuff in the GUI, since it is difficult to automate and document it, so the last few days i checked out how to use scripting for Vegas Pro 22.

This is the current script i made:

test.bat

@echo off
start /B "" "D:\VegasPro\vegas220.exe" -SCRIPT:"D:\test.cs"
exit

test.cs

using System;
using System.IO;
using System.Windows.Forms;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Set up logging, create logfile in the same folder as the script
        string logPath = Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[1].Substring(8)), "render.log");
        using (StreamWriter log = new StreamWriter(logPath, false))
        {
            log.WriteLine("INFO Script started at " + DateTime.Now);

            // Add video track and media
            VideoTrack videoTrack = vegas.Project.AddVideoTrack();
            Media media = vegas.Project.MediaPool.AddMedia(@"D:\00000006_video_0.m2v");
            VideoEvent videoEvent = videoTrack.AddVideoEvent();
            videoEvent.AddTake(media.GetVideoStreamByIndex(0));
            videoEvent.Length = media.Length;

            // Add subtitle track
            VideoTrack textTrack = vegas.Project.AddVideoTrack();
            textTrack.Name = "Subtitles";

            // Load Titles & Text generator and SRT file
            PlugInNode textPlugin = vegas.Generators.FindChildByName("Titles & Text");
            string[] srtLines = File.ReadAllLines(@"D:\00000006.MOVIE.srt");
            int i = 0;
            while (i < srtLines.Length)
            {
                if (string.IsNullOrWhiteSpace(srtLines[i]))
                {
                    i++;
                    continue;
                }                i++; // Skip index line
                string[] times = srtLines[i++].Split(new string[] { " --> " }, StringSplitOptions.None);
                Timecode duration = ParseSrtTime(times[1]) - ParseSrtTime(times[0]);

                // Collect subtitle text
                int textStart = i;
                while (i < srtLines.Length && !string.IsNullOrWhiteSpace(srtLines[i]))
                {
                    i++;
                }
                string text = string.Join("\n", srtLines, textStart, i - textStart).Trim();

                // Add text event with formatting
                VideoEvent textEvent = textTrack.AddVideoEvent(ParseSrtTime(times[0]), duration);
                textEvent.AddTake(Media.CreateInstance(vegas.Project, textPlugin).GetVideoStreamByIndex(0));
                Effect effect = textEvent.ActiveTake.Media.Generator;
                effect.Preset = "Subtitles";
                OFXStringParameter stringParam = effect.OFXEffect.FindParameterByName("Text") as OFXStringParameter;
                RichTextBox rbx = new RichTextBox();
                rbx.Rtf = stringParam.Value;
                rbx.Text = text;
                rbx.SelectionAlignment = HorizontalAlignment.Center;
                stringParam.Value = rbx.Rtf;
                effect.OFXEffect.AllParametersChanged();
            }

            // Select the "Growlanser6" template by name
            Renderer renderer = vegas.Renderers.FindByName("MainConcept MPEG-2");
            RenderTemplate template = null;
            foreach (RenderTemplate rt in renderer.Templates)
            {
                if (rt.Name == "Growlanser6")
                {
                    template = rt;
                    break;
                }
            }            
            // Set up render arguments
            RenderArgs renderArgs = new RenderArgs(vegas.Project);
            renderArgs.OutputFile = @"D:\output_video.m2v";
            renderArgs.RenderTemplate = template;
            renderArgs.ShowOpenButtonsOnComplete = false;

            // Perform render
            RenderStatus status = vegas.Render(renderArgs);

            // Clean up and exit
            log.WriteLine("INFO Clearing project and closing VEGAS Pro...");
            vegas.NewProject(false, false);
            vegas.Exit();
        }
    }    private Timecode ParseSrtTime(string srtTime)
    {
        string[] parts = srtTime.Split(new char[] { ':', ',' });
        return Timecode.FromMilliseconds(int.Parse(parts[0]) * 3600000 + int.Parse(parts[1]) * 60000 + int.Parse(parts[2]) * 1000 + int.Parse(parts[3]));
    }
}

I am kinda able to replicate everything i do in the GUI:

- Add the video to the track
- Add subtitles with kinda the same formatting
- Render the video using the preset

But i can't seem to replicate the preset creation, which i originally did in the GUI.

I also noticed that the preset is some kind of binary file (Vegas, WHY?!) and that i probably have to reverse engineer that... but i don't know if i have enough time and motivation to do that.

Risae wrote on 5/31/2025, 9:19 AM

You'd really be better off creating whatever presets you want in VEGAS and then using those presets from a script.

I think that might be my only simple solution right now

zzzzzz9125 wrote on 5/31/2025, 9:47 AM

You'd really be better off creating whatever presets you want in VEGAS and then using those presets from a script.

I think that might be my only simple solution right now

@Risae After creating some render templates, you can use renderer.Templates.FindProjectMatches(Project project) to filter out the templates that best match the project properties, which is very useful for some basic properties (video width & height, framerate, etc.). Just change your project properties via script in advance.

Last changed by zzzzzz9125 on 5/31/2025, 9:48 AM, changed a total of 1 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

jetdv wrote on 5/31/2025, 7:04 PM

You shouldn't need to "replicate the preset creation". Open the render as dialog, create the preset the way you want it, and save it as a new preset name. Now, instead of "replicate the preset creation", just use YOUR named preset. That's very easy to do - pick the renderer as normal and then pick YOUR preset from the preset list.

Risae wrote on 6/1/2025, 12:45 AM

You shouldn't need to "replicate the preset creation". Open the render as dialog, create the preset the way you want it, and save it as a new preset name. Now, instead of "replicate the preset creation", just use YOUR named preset. That's very easy to do - pick the renderer as normal and then pick YOUR preset from the preset list.

As a said, i don't want to deal with the Vegas Pro GUI in my automation workflow. I want to be able to use a "new" Vegas Pro installation and execute my script to do everything automatically (including creating and deleting a RenderTemplate).

With that, anybody would be able to use my script to replicate exactly what i am doing. The "steps" to replicate would be documented in the code of the script.
I also saw a feature request thread, i will write one over there.

jetdv wrote on 6/1/2025, 7:33 AM

@Risae, I'm not saying to "deal with the Vegas Pro GUI in my automation workflow"

Are you needing a different preset every time you process a file? I would think that one or two presets should be sufficient. Just make them ahead of time and then call the proper pre-made preset. No need to deal with the VEGAS GUI after you've created the proper preset(s).

You've found: RenderTemplate CreateRenderTemplate(Byte[] templateData)

However, nowhere have I seen Byte[] templateData defined.

And, technically, it's against the VEGAS user agreement to "reverse engineer" as zzzzzz9125 did above to find the height/width. And if you need more changes than that, it would take a lot more work.