Regions From Timecodes Script Help

iEmby wrote on 9/1/2024, 6:14 PM

Hi guys.. @zzzzzz9125 @Thiago_Sase @jetdv

i create this script for adding region from user input timecode in following format.

00:56:19 - 00:57:34

00:57:37 - 00:58:19

01:01:33 - 01:02:28

01:03:41 - 01:04:53

01:12:16 - 01:13:25

but is taking only starting timecodes (Bold ones) of every line as region's start and end.

i want it to work like

00:56:19 - 00:57:34 ... first one should be region's start and second one regions end

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;

public class EntryPoint
{
    [STAThread]
    public void FromVegas(Vegas vegas)
    {
        // Prompt user for timecodes
        string input = PromptForTimecodes();
        if (string.IsNullOrEmpty(input))
        {
            MessageBox.Show("No timecodes entered. Exiting.");
            return;
        }

        // Parse and sort the timecodes
        List<Timecode> timecodes = ParseTimecodes(input);
        if (timecodes.Count == 0)
        {
            MessageBox.Show("No valid timecodes were entered. Exiting.");
            return;
        }
        timecodes.Sort();

        // Create regions in VEGAS Pro
        CreateRegionsInVegas(vegas, timecodes);
    }

    private string PromptForTimecodes()
    {
        using (Form form = new Form())
        {
            form.Text = "Enter Timecodes in HH:MM:SS format (one per line)";
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Size = new System.Drawing.Size(400, 300);

            TextBox textBox = new TextBox
            {
                Multiline = true,
                Width = 360,
                Height = 200,
                ScrollBars = ScrollBars.Vertical,
                Location = new System.Drawing.Point(10, 10)
            };

            Button okButton = new Button
            {
                Text = "OK",
                DialogResult = DialogResult.OK,
                Location = new System.Drawing.Point(100, 220)
            };
            Button cancelButton = new Button
            {
                Text = "Cancel",
                DialogResult = DialogResult.Cancel,
                Location = new System.Drawing.Point(200, 220)
            };

            form.Controls.Add(textBox);
            form.Controls.Add(okButton);
            form.Controls.Add(cancelButton);
            form.AcceptButton = okButton;
            form.CancelButton = cancelButton;

            DialogResult result = form.ShowDialog();
            return result == DialogResult.OK ? textBox.Text : null;
        }
    }

    private List<Timecode> ParseTimecodes(string input)
    {
        List<Timecode> timecodes = new List<Timecode>();
        string[] lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
        List<string> invalidLines = new List<string>();

        foreach (string line in lines)
        {
            Timecode timecode;
            string trimmedLine = line.Trim();
            if (TryParseTimecode(trimmedLine, out timecode))
            {
                timecodes.Add(timecode);
            }
            else
            {
                invalidLines.Add(trimmedLine);
            }
        }

        if (invalidLines.Count > 0)
        {
            string invalidLinesMessage = "The following lines were not valid timecodes:\n";
            foreach (string invalidLine in invalidLines)
            {
                invalidLinesMessage += invalidLine + "\n";
            }
            MessageBox.Show(invalidLinesMessage.TrimEnd(), "Invalid Timecodes", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        return timecodes;
    }

    private bool TryParseTimecode(string timecodeString, out Timecode timecode)
    {
        timecode = null;
        try
        {
            timecode = new Timecode(timecodeString);
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Error parsing timecode '{0}': {1}", timecodeString, ex.Message), "Invalid Timecode", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
    }

    private void CreateRegionsInVegas(Vegas vegas, List<Timecode> timecodes)
    {
        Project project = vegas.Project;
        if (project == null)
        {
            MessageBox.Show("No active project found. Exiting.");
            return;
        }

        if (timecodes.Count < 2)
        {
            MessageBox.Show("At least two valid timecodes are required to create regions.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        try
        {
            for (int i = 0; i < timecodes.Count - 1; i++)
            {
                Timecode start = timecodes[i];
                Timecode end = timecodes[i + 1];

                if (start < end) // Ensure that the start is before the end
                {
                    project.Regions.Add(new Region(start, end - start));
                }
            }

            MessageBox.Show("Regions created successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to create regions: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

can someone please fix it?

Last changed by iEmby

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

Comments

jetdv wrote on 9/1/2024, 6:50 PM

Why do you have to make it so complicated???
 

using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Runtime;
using System.Xml;
using ScriptPortal.Vegas;

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            var lines = File.ReadAllLines("D:\\Times.txt");

            foreach (string line in lines)
            {
                string[] pieces = line.Split('-');

                Timecode regionStart = Timecode.FromString(pieces[0].Trim());
                Timecode regionEnd = Timecode.FromString(pieces[1].Trim());

                ScriptPortal.Vegas.Region myRegion = new ScriptPortal.Vegas.Region(regionStart, regionEnd - regionStart);
                myVegas.Project.Regions.Add(myRegion);
            }
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Test_Script.Class1 test = new Test_Script.Class1();
        test.Main(vegas);
    }
}

 

jetdv wrote on 9/1/2024, 6:50 PM

Content of Times.txt:

00:56:19 - 00:57:34
00:57:37 - 00:58:19
01:01:33 - 01:02:28
01:03:41 - 01:04:53
01:12:16 - 01:13:25

 

jetdv wrote on 9/1/2024, 8:39 PM

If those are really Hour:minute:Second, you'll also need to add ":00" to the string before converting it to a timecode to get it into "HH:MM:SS:FF" format...

iEmby wrote on 9/1/2024, 9:14 PM

If those are really Hour:minute:Second, you'll also need to add ":00" to the string before converting it to a timecode to get it into "HH:MM:SS:FF" format...

sir actually it is for user input. like a textbox appear i will paste the timecode sent by client in that then it will add regions on them.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

iEmby wrote on 9/1/2024, 9:47 PM

ok check it here sir.. with your guidance and with help of ChatGPT.. i make it more compatible.

you can add 00.00.00.00 or 000000 it will add automatically : and missing frame 00.

 

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

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            // Prompt for timecodes
            string userInput = PromptForTimecodes();
            if (userInput == null)
            {
                MessageBox.Show("Operation cancelled or no input provided.");
                return;
            }

            string[] lines = userInput.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
            {
                try
                {
                    string[] pieces = line.Split('-');
                    if (pieces.Length != 2)
                    {
                        MessageBox.Show("Line format is incorrect: " + line);
                        continue;
                    }

                    // Process each timecode to ensure it has exactly four pairs (HH:MM:SS:FF)
                    string startCode = EnsureFourPairs(pieces[0].Trim());
                    string endCode = EnsureFourPairs(pieces[1].Trim());

                    Timecode regionStart = Timecode.FromString(startCode);
                    Timecode regionEnd = Timecode.FromString(endCode);

                    // Ensure end time is greater than start time
                    if (regionEnd < regionStart)
                    {
                        MessageBox.Show("End time must be greater than start time: " + line);
                        continue;
                    }

                    ScriptPortal.Vegas.Region myRegion = new ScriptPortal.Vegas.Region(regionStart, regionEnd - regionStart);
                    myVegas.Project.Regions.Add(myRegion);
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("Error processing line: " + line + " - " + ex.Message);
                }
            }
        }

        private string PromptForTimecodes()
        {
            using (Form form = new Form())
            {
                form.Text = "Enter Timecodes in HH:MM:SS or HH:MM:SS:FF format";
                form.StartPosition = FormStartPosition.CenterScreen;
                form.Size = new System.Drawing.Size(400, 300);

                TextBox textBox = new TextBox
                {
                    Multiline = true,
                    Width = 360,
                    Height = 200,
                    ScrollBars = ScrollBars.Vertical,
                    Location = new System.Drawing.Point(10, 10)
                };

                Button okButton = new Button
                {
                    Text = "OK",
                    DialogResult = DialogResult.OK,
                    Location = new System.Drawing.Point(100, 220)
                };
                Button cancelButton = new Button
                {
                    Text = "Cancel",
                    DialogResult = DialogResult.Cancel,
                    Location = new System.Drawing.Point(200, 220)
                };

                form.Controls.Add(textBox);
                form.Controls.Add(okButton);
                form.Controls.Add(cancelButton);
                form.AcceptButton = okButton;
                form.CancelButton = cancelButton;

                DialogResult result = form.ShowDialog();
                return result == DialogResult.OK ? textBox.Text : null;
            }
        }

        private string EnsureFourPairs(string timecode)
        {
            // Replace periods with colons
            timecode = timecode.Replace('.', ':');

            // Check if the timecode is a continuous number string
            if (timecode.Length == 8 || timecode.Length == 10)
            {
                // Add colons based on the length
                if (timecode.Length == 8)
                {
                    // Format: HHMMSSFF
                    timecode = timecode.Insert(2, ":").Insert(5, ":").Insert(8, ":");
                }
                else if (timecode.Length == 10)
                {
                    // Format: HHMMSSFF with frames already included
                    timecode = timecode.Insert(2, ":").Insert(5, ":").Insert(8, ":");
                }
            }
            else
            {
                // Replace colons if present
                timecode = timecode.Replace('.', ':');

                // Split by ':' and ensure that there are exactly four pairs
                string[] parts = timecode.Split(':');
                if (parts.Length == 3)
                {
                    // If there are only HH:MM:SS, append ":00" for frames
                    timecode = timecode + ":00";
                }
                else if (parts.Length > 4)
                {
                    // Handle cases with more than four pairs, keep only the first four pairs
                    string[] firstFourParts = new string[4];
                    for (int i = 0; i < 4 && i < parts.Length; i++)
                    {
                        firstFourParts[i] = parts[i];
                    }
                    timecode = string.Join(":", firstFourParts);
                }
            }
            return timecode;
        }
    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Test_Script.Class1 test = new Test_Script.Class1();
        test.Main(vegas);
    }
}

 

Last changed by iEmby on 9/1/2024, 9:47 PM, changed a total of 1 times.

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

iEmby wrote on 9/1/2024, 10:28 PM

@jetdv thanks for response sir

PROCESSOR
     

Operating System: Windows 11 Pro 64-bit (Always Updated)
System Manufacturer: ASUS
12th Gen Intel(R) Core(TM) i7-12700 (20 CPUs), ~2.1GHz - 4.90GHz
Memory: 32GB RAM
Page File: 11134MB used, 7934MB Available
DirectX Version: DirectX 12

-----------------------------------------------

MOTHERBOARD

 

ASUS PRIME H610-CS D4
Intel® H610 (LGA 1700)
Ready for 12th Gen Intel® Processors
Micro-ATX Motherboard with DDR4
Realtek 1 Gb Ethernet
PCH Heatsink
PCIe 4.0 | M.2 slot (32Gbps) 
HDMI® | D-Sub | USB 3.2 Gen 1 ports
SATA 6 Gbps | COM header
LPT header | TPM header
Luminous Anti-Moisture Coating
5X Protection III
(Multiple Hardware Safeguards
For all-round protection)

-----------------------------------------------
EXTERNAL GRAPHIC CARD

-----------------------------------------------

INTERNAL GRAPHIC CARD (iGPU)

------------------------------------------------

LED - MONITOR

Monitor Name: Generic PnP Monitor
Monitor Model: HP 22es
Monitor Id: HWP331B
Native Mode: 1920 x 1080(p) (60.000Hz)
Output Type: HDMI

-----------------------------------------------

STORAGE DRIVE

Drive: C:
Free Space: 182.3 GB
Total Space: 253.9 GB
File System: NTFS
Model: WD Blue SN570 1TB (NVMe)

---------------O----------------

My System Info (PDF File).

https://drive.google.com/open?id=1-eoLmuXzshTRH_8RunAYAuNocKpiLoiV&usp=drive_fs

 

Also Check

Some useful creations by me including VEGAS Scripts

https://getopensofts.blogspot.com/

 

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)