Vegas Pro 13 script to add media to timeline ?

prango wrote on 7/6/2023, 1:25 PM

Update : Anyone still stuck with VP13. I have posted the updated scripts in the subsequent posts. Please feel free to use them / modify them. The scripts should work in newer versions as well, but I cannot test them.


A big thanks to @jetdv for the guidance.

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

Background : I am helping on a pro bono project to create some lesson videos.
They have a big bunch of videos, I just need to join multiple videos and insert some image files in between and render them.

For example, the list may be like this :

image1.jpg
video1.mp4
video2.mp4
video3.mp4
image2.jpg
video4.mp4
image3.jpg
chart1.png
video4.mp4
...

Since there are almost 1000+ videos of various lengths + 1000+ image files, doing this manually is a real pain.

All the files are on their old (read ancient) desktop, which has Vegas Pro 13 installed on it.

---------------------Bare Minimum Ask----------------------------

Is there a batch script for VP13 which can read a text file list and just add the images and videos as per the text file into the timeline without any gaps ?

---------------------Wishful Thinking :) ----------------------------

It would be even more helpful if there was a way to add some region markers also through the text file. For example :

BEGIN REGION 1
image1.jpg
video1.mp4
video2.mp4
video3.mp4
END REGION 1
BEGIN REGION 2
image2.jpg
video4.mp4
image3.jpg
END REGION 2
BEGIN REGION 3
chart1.png
video4.mp4
END REGION 3
...

Moving the files out of their desktop into a different machine and using Resolve / Final Cut etc. is not an option. The work needs to be done on their desktop and they don't have anything other than VP13.

Any help / guidance on where I can find such a script will be great.

Comments

jetdv wrote on 7/6/2023, 3:38 PM

Change "Using ScriptPortal.Vegas" to "Using Sony.Vegas" for version 13.

prango wrote on 7/7/2023, 2:09 AM

Thanks @jetdv for the guidance. I managed to get something banged out.

The script works perfectly in VP13.

As you mentioned - "Change "Using ScriptPortal.Vegas" to "Using Sony.Vegas" for version 13." - Changing it to Using ScriptPortal.Vegas should make this work in the more recent Magix versions of VP.

Here is the working script , with instructions :
 

  1. Open a text editor - Notepad or Notepad++ will be good enough.
  2. Create a new file and copy the script code into it.
  3. Save the file with a .cs extension, such as vegas_add_media_from_list.cs.
  4. Open Vegas Pro and ensure that the project you want to work with is open or create a new project.
  5. Run the script by going to the "Tools" menu in Vegas Pro and selecting "Scripting" and then "Run Script...". Browse and select the script file from the file dialog.
  6. A file selection dialog will open. Browse and select the folder that contains the 'list.txt' file.
  7. The script will read the 'list.txt' file, locate the media files listed in it, and insert them into the Vegas Pro timeline in the same order specified in the file.
  8. Once the script execution is complete, you will see the media files inserted into the timeline without any gaps between them.

The list.txt file should look like this:

C:\Users\Documents\source videos\e1a.MP4
C:\Users\Documents\source videos\e1b.MP4
C:\Users\Documents\source videos\english (2).JPG
C:\Users\Documents\source videos\e2a.MP4
C:\Users\Documents\source videos\e2b.MP4
C:\Users\Documents\source videos\english (3).JPG
C:\Users\Documents\source videos\english (4).JPG
C:\Users\Documents\source videos\e3.MP4
C:\Users\Documents\source videos\e4.MP4
C:\Users\Documents\source videos\e5.MP4


This is the script :
Change using ScriptPortal.Vegas; to Using Sony.Vegas" for version 13. No change required for Magix versions of VP.

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

namespace SampleScript1
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            // Open a dialog box to select a folder
            string selectedFolder = SelectFolder();
            if (selectedFolder == null)
            {
                MessageBox.Show("No folder selected.");
                return;
            }

            // Find the 'list.txt' file in the selected folder
            string listFilePath = Path.Combine(selectedFolder, "list.txt");
            if (!File.Exists(listFilePath))
            {
                MessageBox.Show("Could not find 'list.txt' in the selected folder.");
                return;
            }

            // Read the file and extract the media file paths
            string[] lines = File.ReadAllLines(listFilePath);
            string[] mediaFiles = Array.FindAll(lines, line => IsMediaFile(line));

            // Insert the media files into the timeline
            int trackIndex = 0; // Target track index
            Timecode cursorPosition = vegas.Transport.CursorPosition;
            foreach (string mediaFile in mediaFiles)
            {
                InsertFileAt(vegas, mediaFile, trackIndex, cursorPosition);
                TrackEvent lastEvent = GetLastEvent(vegas.Project.Tracks[trackIndex]);
                cursorPosition = lastEvent.End;
            }

            MessageBox.Show("Media files inserted successfully.");
        }

        private string SelectFolder()
        {
            using (var dialog = new FolderBrowserDialog())
            {
                dialog.Description = "Select a folder";
                dialog.ShowNewFolderButton = false;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    return dialog.SelectedPath;
                }
            }
            return null;
        }

        private bool IsMediaFile(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLower();
            return extension == ".mp4" || extension == ".mov" || extension == ".avi" || extension == ".jpeg" || extension == ".jpg" || extension == ".png";
        }

        private void InsertFileAt(Vegas vegas, string fileName, int trackIndex, Timecode cursorPosition)
        {
            foreach (Track track in vegas.Project.Tracks)
            {
                track.Selected = false;
            }
            vegas.Project.Tracks[trackIndex].Selected = true;
            vegas.Transport.CursorPosition = cursorPosition;
            vegas.OpenFile(fileName);
        }

        private TrackEvent GetLastEvent(Track track)
        {
            TrackEvent lastEvent = null;
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (lastEvent == null || trackEvent.End > lastEvent.End)
                {
                    lastEvent = trackEvent;
                }
            }
            return lastEvent;
        }
    }
}


* Credit goes to @jetdv for providing the guidance and the documentation for getting this done.


 

prango wrote on 7/7/2023, 5:16 AM

Update : This is version 2 of the script. It does everything in the previous version; an this one also adds regions to the timeline from the list.

To use this version, your list.txt must look like this : (You can replace the number with any region name... like 'RegionTake1' or just keep it to Region1 to name the region as 1.

C:\Users\school\Documents\source videos\e1a.MP4
C:\Users\school\Documents\source videos\e1b.MP4
C:\Users\school\Documents\source videos\english (2).JPG
Region1
C:\Users\school\Documents\source videos\e2a.MP4
C:\Users\school\Documents\source videos\e2b.MP4
C:\Users\school\Documents\source videos\english (3).JPG
Region2
C:\Users\school\Documents\source videos\english (4).JPG
C:\Users\school\Documents\source videos\e3.MP4
Region3
C:\Users\school\Documents\source videos\e4.MP4
C:\Users\school\Documents\source videos\e5.MP4

The updated script is here :

Again, change "Using ScriptPortal.Vegas" to "Using Sony.Vegas" for version 13 or vice versa for version 14 and above.

using System;
using System.IO;
using System.Windows.Forms;
using Sony.Vegas;namespace SampleScript1
{
    public class EntryPoint
    {
        public void FromVegas(Vegas vegas)
        {
            // Open a dialog box to select a folder
            string selectedFolder = SelectFolder();
            if (selectedFolder == null)
            {
                MessageBox.Show("No folder selected.");
                return;
            }            // Find the 'list.txt' file in the selected folder
            string listFilePath = Path.Combine(selectedFolder, "list.txt");
            if (!File.Exists(listFilePath))
            {
                MessageBox.Show("Could not find 'list.txt' in the selected folder.");
                return;
            }            // Read the file and extract the media files and regions
            string[] lines = File.ReadAllLines(listFilePath);
            string[] mediaFiles = Array.FindAll(lines, line => IsMediaFile(line));
            string[] regions = Array.FindAll(lines, line => IsRegion(line));            // Insert the media files and add regions into the timeline
            int trackIndex = 0; // Target track index
            Timecode cursorPosition = vegas.Transport.CursorPosition;
            foreach (string line in lines)
            {
                if (IsMediaFile(line))
                {
                    string mediaFile = line;
                    InsertFileAt(vegas, mediaFile, trackIndex, cursorPosition);
                    TrackEvent lastEvent = GetLastEvent(vegas.Project.Tracks[trackIndex]);
                    cursorPosition = lastEvent.End;
                }
                else if (IsRegion(line))
                {
                    string regionName = line.Trim().Replace("Region", "").Trim();
                    AddRegion(vegas, regionName, cursorPosition);
                }
            }            MessageBox.Show("Media files and regions inserted successfully.");
        }        private string SelectFolder()
        {
            using (var dialog = new FolderBrowserDialog())
            {
                dialog.Description = "Select a folder";
                dialog.ShowNewFolderButton = false;                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    return dialog.SelectedPath;
                }
            }
            return null;
        }        private bool IsMediaFile(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLower();
            return extension == ".mp4" || extension == ".mov" || extension == ".avi" ||
                extension == ".jpeg" || extension == ".jpg" || extension == ".png";
        }        private bool IsRegion(string line)
        {
            return line.Trim().StartsWith("Region");
        }        private void InsertFileAt(Vegas vegas, string fileName, int trackIndex, Timecode cursorPosition)
        {
            foreach (Track track in vegas.Project.Tracks)
            {
                track.Selected = false;
            }
            vegas.Project.Tracks[trackIndex].Selected = true;
            vegas.Transport.CursorPosition = cursorPosition;
            vegas.OpenFile(fileName);
        }        private TrackEvent GetLastEvent(Track track)
        {
            TrackEvent lastEvent = null;
            foreach (TrackEvent trackEvent in track.Events)
            {
                if (lastEvent == null || trackEvent.End > lastEvent.End)
                {
                    lastEvent = trackEvent;
                }
            }
            return lastEvent;
        }        private void AddRegion(Vegas vegas, string regionName, Timecode start)
        {
            Timecode end = vegas.Transport.CursorPosition;
            Region region = new Region(start, end, regionName);
            vegas.Project.Regions.Add(region);
        }
    }
}

 

jetdv wrote on 7/7/2023, 9:13 AM

Glad to hear the tutorials helped.