Simple matching event length script is not working properly.

iEmby wrote on 10/17/2024, 11:48 AM

hello guys..

 

It match length well when length is extended than original by decreasing it.
and if video event is trimmed (using split S) then it works good by in extending.

but when make my event small by moving edge back then it dosnt extend.

 

using System;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Loop through all tracks in the project.
        foreach (Track track in vegas.Project.Tracks)
        {
            // Loop through all events in the track.
            foreach (TrackEvent trackEvent in track.Events)
            {
                try
                {
                    // Only work on selected video events.
                    if (trackEvent.Selected && trackEvent.MediaType == MediaType.Video)
                    {
                        // Get the original length from the active take of the event.
                        Timecode originalLength = trackEvent.ActiveTake.Length;

                        // Get the current length of the video event.
                        Timecode currentLength = trackEvent.Length;

                        // Check if the current length is less than the original length.
                        if (currentLength < originalLength)
                        {
                            trackEvent.Length = originalLength;
                        }
                        else
                        {
                            // If the end of the event (start + length) is greater than the original end, adjust length.
                            Timecode originalEnd = trackEvent.Start + originalLength;
                            Timecode currentEnd = trackEvent.Start + currentLength;

                            if (currentEnd > originalEnd)
                            {
                                trackEvent.Length = originalLength;
                            }
                        }

                        // Adjust any associated audio events.
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {
                            foreach (TrackEvent otherEvent in otherTrack.Events)
                            {
                                // Check for audio events that start at the same time as the video event.
                                if (otherEvent.MediaType == MediaType.Audio && otherEvent.Start == trackEvent.Start)
                                {
                                    // Set the audio event's length to match the video event's original length.
                                    otherEvent.Length = originalLength;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    // Handle any errors silently or log them as needed
                }
            }
        }
    }
}

actually I need further

in this..

1. match length

2. then check if it is equal to 3 minute or more..if so then continue.

3 if its original length is less than 3 minute then we need to extend it to equal to 3 minutes but not paritlly.

i mean extend it with its origial lenght.

e.g. if original length is 2:58 then make it double extend in loop event it will become 5:56 but we will not make it 3:00 with just extend 2 second.

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 10/17/2024, 12:15 PM

@iEmby The "ActiveTake.Length" is the length of the event on the timeline...

In short, trackEvent.Length will equal trackEvent.ActiveTake.Length

If you want to fix this, try this instead:

Timecode originalLength = trackEvent.ActiveTake.Media.Length;

 

iEmby wrote on 10/17/2024, 1:16 PM

@iEmby The "ActiveTake.Length" is the length of the event on the timeline...

In short, trackEvent.Length will equal trackEvent.ActiveTake.Length

If you want to fix this, try this instead:

Timecode originalLength = trackEvent.ActiveTake.Media.Length;

 

thanks sir..

using System;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Loop through all tracks in the project.
        foreach (Track track in vegas.Project.Tracks)
        {
            // Loop through all events in the track.
            foreach (TrackEvent trackEvent in track.Events)
            {
                try
                {
                    // Only work on selected video events.
                    if (trackEvent.Selected && trackEvent.MediaType == MediaType.Video)
                    {
                        // Get the original length from the active take of the event.
                        Timecode originalLength = trackEvent.ActiveTake.Media.Length;

                        // Get the current length of the video event.
                        Timecode currentLength = trackEvent.Length;

                        // Calculate the original end of the media.
                        Timecode mediaEndTime = trackEvent.Start + originalLength;

                        // Calculate the current end of the event.
                        Timecode currentEndTime = trackEvent.Start + currentLength;

                        // Check if the event is trimmed but already ends at the media's end frame.
                        if (currentEndTime < mediaEndTime && currentEndTime != mediaEndTime)
                        {
                            // Extend the event only if it doesn't already end at the media's end frame.
                            trackEvent.Length = originalLength;
                        }

                        // Adjust any associated audio events.
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {
                            foreach (TrackEvent otherEvent in otherTrack.Events)
                            {
                                // Check for audio events that start at the same time as the video event.
                                if (otherEvent.MediaType == MediaType.Audio && otherEvent.Start == trackEvent.Start)
                                {
                                    // Set the audio event's length to match the video event's original length.
                                    otherEvent.Length = originalLength;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Log the exception for debugging purposes.
                    vegas.DebugOut("Error adjusting event: " + ex.Message);
                }
            }
        }
    }
}

but sir...

please check.

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)

jetdv wrote on 10/17/2024, 4:10 PM

Then you also need to check the "ActiveTake.Offset" and subtract that from the length when expanding the length.

Timecode mediaEndTime = trackEvent.Start + originalLength - trackEvent.ActiveTake.Offset;

 

iEmby wrote on 10/18/2024, 12:09 AM

Then you also need to check the "ActiveTake.Offset" and subtract that from the length when expanding the length.

Timecode mediaEndTime = trackEvent.Start + originalLength - trackEvent.ActiveTake.Offset;

 

Thanks sir...
I created this script for my personal purpose.
it works fine..
thanks to u.

can you please take a look to check all ok there ? bcoz i use chatgpt for all this..

using System;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Define the target duration (3 minutes).
        Timecode targetLength = Timecode.FromSeconds(180); // 3 minutes = 180 seconds

        // Check if any events are selected.
        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent selectedEvent in track.Events)
            {
                // Only process if the event is selected and is a video event.
                if (selectedEvent.Selected && selectedEvent.MediaType == MediaType.Video)
                {
                    try
                    {
                        // Get the original length of the media from the active take.
                        Timecode originalLength = selectedEvent.ActiveTake.Media.Length;

                        // Restore the selected event to its original length.
                        selectedEvent.Length = originalLength;

                        // Get the media associated with the selected event.
                        Media selectedMedia = selectedEvent.ActiveTake.Media;

                        // Loop through all tracks and events in the project.
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {
                            foreach (TrackEvent trackEvent in otherTrack.Events)
                            {
                                // Check if the event shares the same media as the selected event.
                                if (trackEvent.ActiveTake.Media.Equals(selectedMedia))
                                {
                                    // Restore to original length.
                                    trackEvent.Length = originalLength;

                                    // Check and set the nearest valid length.
                                    Timecode newLength = GetNearestValidLength(originalLength, targetLength);
                                    trackEvent.Length = newLength;

                                    // Adjust associated audio events to match the video event's new length.
                                    AdjustAssociatedAudioEvents(vegas, trackEvent);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // Log the exception for debugging purposes.
                        vegas.DebugOut(String.Format("Error adjusting event '{0}': {1}", selectedEvent.Name, ex.Message));
                    }
                }
            }
        }
    }

    // Function to calculate the nearest valid length based on the original length and target length.
    private Timecode GetNearestValidLength(Timecode originalLength, Timecode targetLength)
    {
        // Calculate how many times the original length fits into the target length.
        double multiple = Math.Ceiling(targetLength.ToMilliseconds() / originalLength.ToMilliseconds());

        // Calculate the potential new length based on the multiple.
        Timecode newLength = new Timecode((long)(multiple * originalLength.ToMilliseconds()));

        return newLength;
    }

    // Adjust associated audio events to match the video event's new length.
    private void AdjustAssociatedAudioEvents(Vegas vegas, TrackEvent videoEvent)
    {
        foreach (Track otherTrack in vegas.Project.Tracks)
        {
            foreach (TrackEvent otherEvent in otherTrack.Events)
            {
                // Check for audio events that start at the same time as the video event.
                if (otherEvent.MediaType == MediaType.Audio && otherEvent.Start == videoEvent.Start)
                {
                    // Set the audio event's length to match the video event's new length.
                    otherEvent.Length = videoEvent.Length;
                }
            }
        }
    }
}

 

this was my prompt to ChatGPT

my conditions are

Event should be equal to 3 minutes or more if equalling is not possible.

Extending or reducing event should only be done in original event length ratio.

As example i have 30 sec event on timeline but its original length is 1 minute. Then first restore its original length then check the closest length which can be equal or more than 3 mint of it.

In this case it will be equal which is 3 minute. so extend it to 3 min.

In some cases, if event is already extended like 3:10 minutes or 4:15 minutes. we have to take it back to its nearest length which is covering our conditions which is 3 mints. make it back to 3 minutes.

Now in other case. my event on time line is of 40 sec and its original length is 1:20 min.

So first restore its original length then check what will be neareast length equal to 3 minute or more... but never less than 3.

Because extending is only should be done in original length ratio so. 1:20 + 1:20 + 1:20 = 4 min. so in this case if it is overextended like 4:30 or 5:10 then make it back to nearest length which will be in this condition is 4:00 min. Keep in mind. We will never extend or reduce event partially or randomly it will be only in original length ratio.

Last changed by iEmby on 10/18/2024, 12:13 AM, changed a total of 3 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)

jetdv wrote on 10/18/2024, 8:41 AM

The only thing that really jumps out at me is the condition that caused the second issue I resolved for you. If an event is trimmed at the front, it will now extend it out to it's original ending point but it will NOT:

As example i have 30 sec event on timeline but its original length is 1 minute. Then first restore its original length then check the closest length which can be equal or more than 3 mint of it.

It would fully extend from that point forward but would not start "at the beginning" the first time based on the code above.

iEmby wrote on 10/18/2024, 9:59 AM

The only thing that really jumps out at me is the condition that caused the second issue I resolved for you. If an event is trimmed at the front, it will now extend it out to it's original ending point but it will NOT:

As example i have 30 sec event on timeline but its original length is 1 minute. Then first restore its original length then check the closest length which can be equal or more than 3 mint of it.

It would fully extend from that point forward but would not start "at the beginning" the first time based on the code above.

yes sir,, i got it later that i dont need that.. ..

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 10/18/2024, 10:13 AM

@jetdv

now sir need your help...
this code is working very well. with my conditions.

but when my selected events are below adjustment events then it only restore original length not extending to make it equal to 3 minute or above it..

 

using System;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        // Define the target duration (3 minutes).
        Timecode targetLength = Timecode.FromSeconds(180); // 3 minutes = 180 seconds

        // Check if any events are selected.
        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent selectedEvent in track.Events)
            {
                // Only process if the event is selected and is a video event or VEG file.
                if (selectedEvent.Selected && IsVideoOrVegFile(selectedEvent))
                {
                    try
                    {
                        // Get the original length of the media from the active take.
                        Timecode originalLength = selectedEvent.ActiveTake.Media.Length;

                        // Restore the selected event to its original length.
                        selectedEvent.Length = originalLength;

                        // Get the media associated with the selected event.
                        Media selectedMedia = selectedEvent.ActiveTake.Media;

                        // Loop through all tracks and events in the project.
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {
                            foreach (TrackEvent trackEvent in otherTrack.Events)
                            {
                                // Check if the event shares the same media as the selected event.
                                if (trackEvent.ActiveTake.Media.Equals(selectedMedia))
                                {
                                    // Restore to original length.
                                    trackEvent.Length = originalLength;

                                    // Check and set the nearest valid length.
                                    Timecode newLength = GetNearestValidLength(originalLength, targetLength);
                                    trackEvent.Length = newLength;

                                    // Adjust associated audio events to match the video event's new length.
                                    AdjustAssociatedAudioEvents(vegas, trackEvent);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // Log the exception for debugging purposes.
                        vegas.DebugOut(String.Format("Error adjusting event '{0}': {1}", selectedEvent.Name, ex.Message));
                    }
                }
            }
        }
    }

    // Function to check if an event is a video or VEG file.
    private bool IsVideoOrVegFile(TrackEvent trackEvent)
    {
        // Check if the event is a video type or has a media path that ends with ".veg".
        return trackEvent.MediaType == MediaType.Video || trackEvent.ActiveTake.MediaPath.EndsWith(".veg", StringComparison.OrdinalIgnoreCase);
    }

    // Function to calculate the nearest valid length based on the original length and target length.
    private Timecode GetNearestValidLength(Timecode originalLength, Timecode targetLength)
    {
        // Calculate how many times the original length fits into the target length.
        double multiple = Math.Ceiling(targetLength.ToMilliseconds() / originalLength.ToMilliseconds());

        // Calculate the potential new length based on the multiple.
        Timecode newLength = new Timecode((long)(multiple * originalLength.ToMilliseconds()));

        return newLength;
    }

    // Adjust associated audio events to match the video event's new length.
    private void AdjustAssociatedAudioEvents(Vegas vegas, TrackEvent videoEvent)
    {
        foreach (Track otherTrack in vegas.Project.Tracks)
        {
            foreach (TrackEvent otherEvent in otherTrack.Events)
            {
                // Check for audio events that start at the same time as the video event.
                if (otherEvent.MediaType == MediaType.Audio && otherEvent.Start == videoEvent.Start)
                {
                    // Set the audio event's length to match the video event's new length.
                    otherEvent.Length = videoEvent.Length;
                }
            }
        }
    }
}

please take a look what is happening.

I most of need video events and nested project for this process.

along with there corresponding audio.

for more clarification these are my conditions

  1. First restore selected event's length if it is not in original length (ignore trimmed things that is not needed now. all events will be untrimmed from front)
  2. Then check is this original length is equal or more than 3 min. If yes then return code.
  3. If not then make it equal to 3 min or more than 3 min if equal is not possible.
  4. Extending event & reducing event condition is like this. We only can extend or reduce event with original length ratio. If event's original length is 1 minute then it will be 3 min, if its 1:20 then it will be 3:60. and suppose if event already extended like to 6 min (original length is 1:20) then reduce it by 1:20 up to 3:20. we cant go below 3 mint.
  5. VEG & video events should be processed along with there corresponding audios
  6. Also if there is another same media event on same track then extend it too.

no need to adjust offsets now.

only adjustment event issue is problem as described in above video.

please sir help. and cross check as per conditions if you can.

thanks in advance.

Last changed by iEmby on 10/18/2024, 10:14 AM, 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)

jetdv wrote on 10/18/2024, 1:48 PM

Are you getting an error? You might change: vegas.DebugOut to MessageBox.Show so that you can see the actual error message if an error is happening.

It could also be related to the fact that you're going through everything while going through everything???


 

        //Going through all tracks!
        foreach (Track track in vegas.Project.Tracks)
        {

            //Going through all events in this track
            foreach (TrackEvent selectedEvent in track.Events)
            {
                // Only process if the event is selected and is a video event or VEG file.
                if (selectedEvent.Selected && IsVideoOrVegFile(selectedEvent))
                {
                    try
                    {
                        // Get the original length of the media from the active take.
                        Timecode originalLength = selectedEvent.ActiveTake.Media.Length;

                        // Restore the selected event to its original length.
                        selectedEvent.Length = originalLength;

                        // Get the media associated with the selected event.
                        Media selectedMedia = selectedEvent.ActiveTake.Media;

                        // Loop through all tracks and events in the project.

                        //Going through all tracks AGAIN???
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {

                            //Then going through all of these events again???
                            foreach (TrackEvent trackEvent in otherTrack.Events)
                            {


                                //But you're changing the event in this second loop and not the original loop in which we were gathering the information??? Why???

                                // Check if the event shares the same media as the selected event.
                                if (trackEvent.ActiveTake.Media.Equals(selectedMedia))
                                {
                                    // Restore to original length.
                                    trackEvent.Length = originalLength;

                                    // Check and set the nearest valid length.
                                    Timecode newLength = GetNearestValidLength(originalLength, targetLength);
                                    trackEvent.Length = newLength;

Seems like this would make more sense:

        foreach (Track track in vegas.Project.Tracks)
        {
            foreach (TrackEvent selectedEvent in track.Events)
            {
                // Only process if the event is selected and is a video event or VEG file.
                if (selectedEvent.Selected && IsVideoOrVegFile(selectedEvent))
                {
                    try
                    {
                        // Get the original length of the media from the active take.
                        Timecode originalLength = selectedEvent.ActiveTake.Media.Length;

                        // Restore the selected event to its original length.
                        selectedEvent.Length = originalLength;

                        // Check and set the nearest valid length.
                        Timecode newLength = GetNearestValidLength(originalLength, targetLength);
                        selectedEvent.Length = newLength;

                        // Adjust associated audio events to match the video event's new length.
                        AdjustAssociatedAudioEvents(vegas, trackEvent);

And totally get rid of the second pass through the tracks and events.

And your current code doesn't take into account an event that's originally been trimmed at the front...

iEmby wrote on 10/18/2024, 6:45 PM
// Get the media associated with the selected event.
                        Media selectedMedia = selectedEvent.ActiveTake.Media;

bcoz of this sir..

i want that it adjust length of all media of selected event which we have on timeline.

 

you will understand this from screen recording sir... i placed two same events on timeline. but apply code on one and other one applied automatically.

 

Are you getting an error? Y

no sir no error comes in front.

it just not works when adjustment event is above on the selected event.

please look at the screen recording i sent

..

thanks sir..

Last changed by iEmby on 10/18/2024, 6:49 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)

jetdv wrote on 10/19/2024, 8:21 AM

vegas.DebugOut won't show an error on the screen. MessageBox.Show will...

So you're wanting to adjust ALL selected events based on the FIRST selected event?

If yes, I'd still do it in a single loop...

(I see no screen recording)

iEmby wrote on 10/19/2024, 12:44 PM

(I see no screen recording)

see here sir

please take a look what is happening.

https://www.vegascreativesoftware.info/us/forum/simple-matching-event-length-script-is-not-working-properly--147511/#ca927292

Last changed by iEmby on 10/19/2024, 12:45 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)

jetdv wrote on 10/19/2024, 1:14 PM

@iEmby Ok, I see what you're doing.

Your script *IS* throwing an error that you are not seeing because you used "vegas.DebugOut" instead of "MessageBox.Show".

In the second part of the script, you added an "adjustment event" - an adjustment event HAS NO MEDIA (and has no ActiveTake.) So it will die on this line (as well as any others that access the media information of that event):

Media selectedMedia = selectedEvent.ActiveTake.Media;

There is no media length for an adjustment event. You can check to see if it's an adjustment event (or even an "empty event") by looking at selectedEvent.Takes.Count - if it equals zero, then don't try to get the media length because it does not exist.

If you want to change the length based on those events, just use selectedEvent.Length and don't try to access the "Media" at all which means most of your second loop would need to be changed.

My thought: if they select an adjustment event, pop up an error that says it's an adjustment event and they must choose an event that contains media. But you'll also need to adjust things like this in the second loop as you're going through to "adjust everything that matches":

trackEvent.ActiveTake.MediaPath.EndsWith(".veg", StringComparison.OrdinalIgnoreCase)

jetdv wrote on 10/19/2024, 1:21 PM

Actually if you had selected the "adjustment event" in the second part, it would be throwing an error here:

if (selectedEvent.Selected && IsVideoOrVegFile(selectedEvent))

Where it calls:

    private bool IsVideoOrVegFile(TrackEvent trackEvent)
    {
        // Check if the event is a video type or has a media path that ends with ".veg".
        return trackEvent.MediaType == MediaType.Video || trackEvent.ActiveTake.MediaPath.EndsWith(".veg", StringComparison.OrdinalIgnoreCase);
    }

Which tries to access the MediaPath and is before the "try". Because it was "not selected", it did not throw the error on the outside loop when it looked at it.

iEmby wrote on 10/19/2024, 1:40 PM

Sorry sir. I think maybe I am not understanding what you are saying if that is a solution, or you misunderstood what I wrong there.

in simple words sir.

I want my selected event should change its length as per conditions along with its same media on timeline.

I don't want adjustment to change. If its length will stay there, that's ok.

The main problem I am facing, Adjustment events are not letting selected event to change its length.

when there is any adjustment event on above track of the selected event then this script doesn't work.

but when I shift track to above those adjustment events then it works.

Because in my project I always will select an event which will have a media never an adjustment event same like in screen recording.

Please ask me if there is still any confusion.

Thanks in advance.

Last changed by iEmby on 10/19/2024, 1:41 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)

jetdv wrote on 10/19/2024, 2:13 PM

@iEmby, I fully understand. I think it's you not understanding so let me try to explain it a different way:

It's because inside the inner loops going through all tracks and events: for EVERY EVENT on the timeline, you are looking at the ACTIVE TAKE and the MEDIA of that event.

Adjustment Events DO NOT HAVE MEDIA so it's crashing

So, as I said above:

  1. Check the NUMBER of takes of an event
  2. If that number is zero DO NOT PROCESS that event!
                        // Loop through all tracks and events in the project.
                        foreach (Track otherTrack in vegas.Project.Tracks)
                        {
                            foreach (TrackEvent trackEvent in otherTrack.Events)
                            {
                                // Does this event have media????
                                if (trackEvent.Takes.Count > 0)
                                {

                                    // Check if the event shares the same media as the selected event.
                                    if (trackEvent.ActiveTake.Media.Equals(selectedMedia))  //Otherwise it will crash on this line!!!!
                                    {
                                        // Restore to original length.
                                        trackEvent.Length = originalLength;

                                        // Check and set the nearest valid length.
                                        Timecode newLength = GetNearestValidLength(originalLength, targetLength);
                                        trackEvent.Length = newLength;

                                        // Adjust associated audio events to match the video event's new length.
                                        AdjustAssociatedAudioEvents(vegas, trackEvent);
                                    }
                                }

                            }
                        }

I simply said above that *if you did choose the adjustment event* then you would have seen the error as it would have been outside the try/catch. And also if you change "vegas.DebugOut" to "MessageBox.Show" you also would have seen the error message.

                    catch (Exception ex)
                    {
                        // SHOW the exception for debugging purposes.
                        MessageBox.Show(String.Format("Error adjusting event '{0}': {1}", selectedEvent.Name, ex.Message));
                    }

 

iEmby wrote on 10/20/2024, 12:19 AM

Adjustment Events DO NOT HAVE MEDIA so it's crashing
 

thankyou so much sir..
it works now.. i just ignore that event which has no media

and it works..

thank you so much.

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)