Remove Gaps Across Multiple Tracks

Comments

Jayboy75 wrote on 12/13/2022, 7:45 AM

Sent a donation your way. Thank you again, so much!

jetdv wrote on 12/13/2022, 2:55 PM

@Jeditpro, @Jayboy75, Here's one more update. I found one small issue that needed to be fixed so try out this one instead.
 

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;

            placeRegionsAroundAudio();
            checkOverlappedRegions();
            reverseRegions();
            closeGaps();
        }

        private void placeRegionsAroundAudio()
        {
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    AddRegion(evnt);
                }
            }
        }

        private void AddRegion(TrackEvent evnt)
        {
            bool eventUnderRegion = false;

            foreach (ScriptPortal.Vegas.Region myRegion in myVegas.Project.Regions)
            {
                Timecode regionEnd = myRegion.Position + myRegion.Length;

                if (evnt.Start >= myRegion.Position && evnt.Start <= regionEnd)
                {
                    eventUnderRegion = true;
                }
                if (evnt.End >= myRegion.Position && evnt.End <= regionEnd)
                {
                    eventUnderRegion = true;
                }

                if (eventUnderRegion)
                {
                    if (evnt.Start < myRegion.Position)
                    {
                        myRegion.Position = evnt.Start;
                        if (evnt.End > regionEnd)
                        {
                            myRegion.Length = evnt.End - myRegion.Position;
                        }
                        else
                        {
                            myRegion.Length = regionEnd - evnt.Start;
                        }
                        regionEnd = myRegion.Position + myRegion.Length;
                    }
                    if (evnt.End > regionEnd)
                    {
                        myRegion.Length = evnt.End - myRegion.Position;

                        if (myRegion.Index + 1 != myVegas.Project.Regions.Count)
                        {
                            if (myVegas.Project.Regions[myRegion.Index + 1].Position < evnt.End)
                            {
                                Timecode newEnd = myVegas.Project.Regions[myRegion.Index + 1].Position + myVegas.Project.Regions[myRegion.Index + 1].Length;
                                if (newEnd > myRegion.Position + myRegion.Length)
                                {
                                    myRegion.Length = newEnd - myRegion.Position;
                                }
                                myVegas.Project.Regions.Remove(myVegas.Project.Regions[myRegion.Index + 1]);
                            }
                        }
                    }
                    break;
                }
            }

            if (!eventUnderRegion)
            {
                addRegionToTimeline(evnt.Start, evnt.Length, "Audio Present");
            }
        }

        private void addRegionToTimeline(Timecode regStart, Timecode regLen, string title)
        {
            ScriptPortal.Vegas.Region newRegion = new ScriptPortal.Vegas.Region(regStart, regLen, title);
            myVegas.Project.Regions.Add(newRegion);
        }

        private void checkOverlappedRegions()
        {
            bool foundoverlap = true;

            while (foundoverlap)
            {
                for (int i = 0; i < myVegas.Project.Regions.Count; i++)
                {
                    foundoverlap = false;
                    ScriptPortal.Vegas.Region myRegion = myVegas.Project.Regions[i];

                    Timecode regStart = myRegion.Position;
                    Timecode regEnd = myRegion.Position + myRegion.Length;

                    foreach(ScriptPortal.Vegas.Region thisRegion in myVegas.Project.Regions)
                    {
                        if (myRegion.Index != thisRegion.Index)
                        {
                            Timecode thisStart = thisRegion.Position;
                            Timecode thisEnd = thisRegion.Position + thisRegion.Length;

                            if (regStart >= thisStart && regStart <= thisEnd)
                            {
                                foundoverlap = true;
                                if (thisEnd < regEnd)
                                {
                                    thisRegion.Length = regEnd - thisStart;
                                    myVegas.Project.Regions.Remove(myRegion);
                                    break;
                                }
                                else
                                {
                                    myVegas.Project.Regions.Remove(myRegion);
                                    break;
                                }
                            }
                        }
                    }

                    if (foundoverlap)
                    {
                        break;
                    }
                }
            }
        }

        private void reverseRegions()
        {
            ArrayList RegionStart = new ArrayList();
            ArrayList RegionLength = new ArrayList();

            foreach (ScriptPortal.Vegas.Region myRegion in myVegas.Project.Regions)
            {
                RegionStart.Add(myRegion.Position);
                RegionLength.Add(myRegion.Length);
            }

            for (int i = myVegas.Project.Regions.Count - 1; i >= 0; i--)
            {
                myVegas.Project.Regions.Remove(myVegas.Project.Regions[i]);
            }

            Timecode nextStart = new Timecode(0);
            for (int i = 0; i < RegionStart.Count; i++)
            {
                if (i == 0)
                {
                    if ((Timecode)RegionStart[i] > Timecode.FromFrames(0))
                    {
                        addRegionToTimeline(Timecode.FromFrames(0), (Timecode)RegionStart[i], "Audio Absent");
                    }
                }
                else
                {
                    addRegionToTimeline(nextStart, (Timecode)RegionStart[i] - nextStart, "Audio Absent");
                }
                nextStart = (Timecode)RegionStart[i] + (Timecode)RegionLength[i];
            }
        }

        private void closeGaps()
        {
            for (int i = myVegas.Project.Regions.Count - 1; i >= 0; i--)
            {
                Timecode regStart = myVegas.Project.Regions[i].Position;
                Timecode regLen = myVegas.Project.Regions[i].Length;

                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Start > regStart)
                        {
                            evnt.Start = evnt.Start - regLen;
                        }
                    }
                }
                myVegas.Project.Regions.Remove(myVegas.Project.Regions[i]);
            }
        }


    }
}

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

 

jetdv wrote on 12/16/2022, 10:54 AM

I redid this in a totally different way. Here's the new version. It does not use any regions at all so you can use regions in your project and it will not affect this script nor will they be removed by this version.
 

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;
        ArrayList RegionStart = new ArrayList();
        ArrayList RegionLength = new ArrayList();
        int[] lastEvent = new int[1000];
        Timecode nextEvent = null;

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

            Timecode regStart = null;
            for (int i=0; i<myVegas.Project.Tracks.Count; i++)
            {
                lastEvent[i] = 0;
            }

            for (long i=0; i<myVegas.Project.Length.FrameCount; i++)
            {
                nextEvent = myVegas.Project.Length;
                Timecode EndCode = CheckFoundUnderThisFrame(i);
                if (EndCode != null)
                {
                    if (regStart != null)
                    {
                        if (regStart != Timecode.FromFrames(0))
                        {
                            regStart = regStart - Timecode.FromFrames(1);
                        }
                        addRegionToTimeline(regStart, Timecode.FromFrames(i) - regStart);
                        regStart = null;
                    }
                    i = EndCode.FrameCount;
                }
                else
                {
                    if (regStart == null)
                    {
                        regStart = Timecode.FromFrames(i);
                    }
                    i = nextEvent.FrameCount;
                }
            }

            closeGaps();
        }

        private Timecode CheckFoundUnderThisFrame(long i)
        {
            Timecode FoundEventEnd = null;

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                int totalEvents = myTrack.Events.Count;
                for(int j=lastEvent[myTrack.Index]; j < totalEvents; j++)
                {
                    TrackEvent myEvent = myTrack.Events[j];
                    if (myEvent.Start.FrameCount <= i && myEvent.End.FrameCount >=i)
                    {
                        if (FoundEventEnd == null)
                        {
                            FoundEventEnd = myEvent.End;
                        }
                        else
                        {
                            if (myEvent.End > FoundEventEnd)
                            {
                                FoundEventEnd = myEvent.End;
                            }
                        }
                    }
                    if (myEvent.Start.FrameCount >= i)
                    {
                        if (myEvent.Start < nextEvent)
                        {
                            nextEvent = myEvent.Start;
                        }
                        lastEvent[myTrack.Index] = j;
                        j = totalEvents;
                    }
                }
            }

            return FoundEventEnd;

        }

        private void addRegionToTimeline(Timecode regStart, Timecode regLen)
        {
            RegionStart.Add(regStart);
            RegionLength.Add(regLen);
        }

        private void closeGaps()
        {
            for (int i = RegionStart.Count - 1; i >= 0; i--)
            {
                Timecode regStart = (Timecode)RegionStart[i];
                Timecode regLen = (Timecode)RegionLength[i];

                foreach (Track myTrack in myVegas.Project.Tracks)
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Start > regStart)
                        {
                            evnt.Start = evnt.Start - regLen;
                        }
                    }
                }
            }
        }


    }
}

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

 

Jayboy75 wrote on 12/16/2022, 12:56 PM

New version performs much faster! Thanks again :)

jetdv wrote on 12/16/2022, 8:17 PM

@Jayboy75 I'm really happy with "New Version 3"!