Script deletes all audio events instead of removing silent areas HELP

andy-0 wrote on 6/13/2024, 11:28 AM

So, this script is made by jetdv (awesome script by the way, thank you for creating it jetdv). It removes silent areas in the project, but every time I try to execute it, for some reason, it deletes all the audio events. Yes, I've tried adjusting the dB levels to see if there was any change, and incredibly, the script only worked when I set the dB level to -3897.98. I don't know why this error occurs, but I saw a comment from another user saying they had the same issue, and I wanted to know if there's anything I can do to fix this error and make the script work again.

 

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;
        Renderer myRenderer = null;
        RenderTemplate rndrTemplate = null;
        double QuietLimit = -40.0;
        Timecode logOffset = Timecode.FromFrames(10);
        Timecode minLength = Timecode.FromSeconds(1.5);
        bool[,] trackStatus = new bool[1000, 2];

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

            FindRenderers();

            string tempFile = myVegas.TemporaryFilesPath + Path.DirectorySeparatorChar + "temp.mp3";
            string tempLog = myVegas.TemporaryFilesPath + Path.DirectorySeparatorChar + "temp_loud.txt";

            SaveTrackStatus();

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsAudio() && myTrack.Selected)
                {
                    myTrack.Mute = false;

                    RenderTempFile(tempFile);
                    ProcessLog(tempLog, myTrack);

                    File.Delete(tempFile);
                    File.Delete(tempLog);

                    myTrack.Mute = true;
                }
            }


            RecallTrackStatus();
        }

        public void FindRenderers()
        {
            try
            {
                foreach (Renderer renderer in myVegas.Renderers)
                {
                    //MP3
                    if ("adfa6a4b-a99b-42e3-ae1f-081123ada04b" == renderer.ClassID.ToString())
                    {
                        myRenderer = renderer;

                        try
                        {
                            foreach (RenderTemplate renderTemplate in renderer.Templates)
                            {
                                if (renderTemplate.IsValid())
                                {
                                    //192 Kbps, CD Transparent Audio
                                    if ("8ab64a16-81f5-46e6-8155-1611d592088c" == renderTemplate.TemplateGuid.ToString())
                                    {
                                        rndrTemplate = renderTemplate;
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            catch
            {
            }
        }

        public void SaveTrackStatus()
        {
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsAudio())
                {
                    int myIndex = myTrack.Index;
                    trackStatus[myIndex, 0] = myTrack.Mute;
                    trackStatus[myIndex, 1] = myTrack.Solo;

                    myTrack.Mute = true;
                    myTrack.Solo = false;
                }
            }
        }

        public void RecallTrackStatus()
        {
            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsAudio())
                {
                    int myIndex = myTrack.Index;
                    myTrack.Mute = trackStatus[myIndex, 0];
                    myTrack.Solo = trackStatus[myIndex, 1];
                }
            }
        }

        public void RenderTempFile(string tempFile)
        {
            RenderArgs args = new RenderArgs();
            args.OutputFile = tempFile;
            args.RenderTemplate = rndrTemplate;
            args.Start = Timecode.FromFrames(0);
            args.Length = myVegas.Project.Length;
            args.IncludeMarkers = false;
            args.StretchToFill = false;
            args.GenerateLoudnessLog = true;

            RenderStatus status = myVegas.Render(args);
        }

        public void ProcessLog(string path, Track myTrack)
        {
            var lines = File.ReadLines(path);

            bool foundfirst = false;
            bool FoundQuiet = false;
            Timecode QuietStart = Timecode.FromFrames(0);
            Timecode QuietEnd = Timecode.FromFrames(0);
            Timecode PreviousTC = Timecode.FromFrames(0);

            foreach (string line in lines)
            {
                if (line.StartsWith("------------"))
                {
                    if (FoundQuiet)
                    {
                        QuietEnd = PreviousTC;
                        ProcessSegment(QuietStart, myVegas.Project.Length + logOffset, myTrack);
                    }
                    break;
                }
                if (line.StartsWith("              Pos."))
                {
                    foundfirst = true;
                }
                else
                {
                    if (foundfirst)
                    {
                        if (line.Length > 5)
                        {
                            string[] pieces = line.Split('\t');
                            Timecode trackTime = Timecode.FromString(pieces[1]);
                            double trackVolume = 0;
                            if (pieces[2].Contains("Inf"))
                            {
                                trackVolume = -100;
                            }
                            else
                            {
                                trackVolume = Convert.ToDouble(pieces[2]);
                            }

                            if (trackVolume < QuietLimit)
                            {
                                if (!FoundQuiet)
                                {
                                    FoundQuiet = true;
                                    QuietStart = trackTime;
                                }
                            }
                            else
                            {
                                if (FoundQuiet)
                                {
                                    FoundQuiet = false;
                                    QuietEnd = PreviousTC;

                                    ProcessSegment(QuietStart, QuietEnd, myTrack);
                                }
                            }
                            PreviousTC = trackTime;
                        }
                    }
                }
            }
        }

        private void ProcessSegment(Timecode QuietStart, Timecode QuietEnd, Track myTrack)
        {
            Timecode startTC = QuietStart - logOffset;
            if (startTC < Timecode.FromFrames(0))
            {
                startTC = Timecode.FromFrames(0);
            }

            Timecode endTC = QuietEnd - logOffset;
            Timecode regionLen = endTC - startTC;

            if (regionLen > minLength)
            {
                foreach (TrackEvent evnt in myTrack.Events)
                {
                    if (evnt.Start < startTC && evnt.End > startTC)
                    {
                        evnt.Split(startTC - evnt.Start);
                    }
                    if (evnt.Start < endTC && evnt.End > endTC)
                    {
                        evnt.Split(endTC - evnt.Start);
                    }
                }
                for (int i = myTrack.Events.Count - 1; i >= 0; i--)
                {
                    TrackEvent evnt = myTrack.Events[i];
                    if (evnt.Start >= startTC && evnt.End <= endTC)
                    {
                        myTrack.Events.Remove(evnt);
                    }
                }
            }
        }

    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    //public void FromVegas(Vegas vegas, String scriptFile, XmlDocument scriptSettings, ScriptArgs args)
    {
        Test_Script.Class1 test = new Test_Script.Class1();
        test.Main(vegas);
    }
}

 

What a user with the same problem in the script commented ("This workaround doesn't work for me; I already tested it").

Comments

jetdv wrote on 6/13/2024, 12:18 PM

First of all, I don't know why a "workaround" would be needed. The audio should render out with no decreases in volume unless you've added effects to decrease the volume. Naturally this script would be best used before applying any effects.

Next, I would comment out this line and then run the script:

File.Delete(tempLog);

Once the script has run, open that log file and see the actual values you are getting (You could use a MessageBox.Show(tempLog); to see the actual location and name of the file. That should better help you get the proper numbers to use and will also tell you the values the script is actually seeing and the number that might be useful for your "Quiet Limit".

andy-0 wrote on 6/13/2024, 2:24 PM

First of all, I don't know why a "workaround" would be needed. The audio should render out with no decreases in volume unless you've added effects to decrease the volume. Naturally this script would be best used before applying any effects.

Next, I would comment out this line and then run the script:

File.Delete(tempLog);

Once the script has run, open that log file and see the actual values you are getting (You could use a MessageBox.Show(tempLog); to see the actual location and name of the file. That should better help you get the proper numbers to use and will also tell you the values the script is actually seeing and the number that might be useful for your "Quiet Limit".

Jetdv this is happening when I try to run the script

.

 

jetdv wrote on 6/14/2024, 8:36 AM

Did you look in the templog file and see what values are there?

andy-0 wrote on 6/14/2024, 11:22 AM

Did you look in the templog file and see what values are there?


@jetdv (google drive link to the script) Here is the link to the Google Drive file. In it, I've placed a video of yours that I was using as an example to test the script, the script for removing silent areas that I'm using, and the loudness log file generated by the script. I still haven't figured out what's causing the audio event to be completely deleted even though it has values greater than the threshold.

jetdv wrote on 6/14/2024, 11:35 AM

First of all, my videos are not good ones in which to use for testing as there's very little silence there. However, using that video running your exact version of the script, this is my results. You can see one small "silent" area was cut out roughly from 3:05 to 3:08..

andy-0 wrote on 6/14/2024, 11:45 AM

First of all, my videos are not good ones in which to use for testing as there's very little silence there. However, using that video running your exact version of the script, this is my results. You can see one small "silent" area was cut out roughly from 3:05 to 3:08..


@jetdv I tried to use the script again in the video and I still came across the previous error where it deleted all the audio even though it had -40 in the quietlimit. Now I'm really curious why for some the script works normally and for others it presents this error

jetdv wrote on 6/14/2024, 11:49 AM

All I did was drag the file to the timeline, run the script, and that is the results I got. You might check that you don't have any automatic effects. I only have the three "default" ones that are set to "do nothing".

andy-0 wrote on 6/14/2024, 11:55 AM

@jetdv The only ones I have are the 3 default Vegas Pro audio effects that it always adds to the audio track, none of them have any function, they are always at default values, I almost never use them, so I find the audio track effects can be discarded as a variable that could be causing the error and yes, I already tried removing all 3 effects from the audio track and still got the same result

Robert Johnston wrote on 6/14/2024, 1:31 PM

@andy-0 @jetdv In the other thread you started on the same subject, I looked at the screen recording you uploaded, and I noticed that the "Render As" progress dialog is totally different than any other I have seen in any version of Vegas I've ever used. In other words, that version doesn't look right.

What version of Vegas are you using anyway? Have you tried using Vegas Pro 21?

Intel Core i7 10700K CPU @ 3.80GHz (to 4.65GHz), NVIDIA GeForce RTX 2060 SUPER 8GBytes. Memory 32 GBytes DDR4. Also Intel UHD Graphics 630. Mainboard: Dell Inc. PCI-Express 3.0 (8.0 GT/s) Comet Lake. Bench CPU Multi Thread: 5500.5 per CPU-Z.

Vegas Pro 21.0 (Build 108) with Mocha Vegas

Windows 11 not pro

andy-0 wrote on 6/14/2024, 3:22 PM

@andy-0 @jetdv In the other thread you started on the same subject, I looked at the screen recording you uploaded, and I noticed that the "Render As" progress dialog is totally different than any other I have seen in any version of Vegas I've ever used. In other words, that version doesn't look right.

What version of Vegas are you using anyway? Have you tried using Vegas Pro 21?

@Robert Johnston ahhh now I understand what you were talking about, about the render window it looks like this because it is an extension that I am using and that gives me detailed information when rendering a file in Vegas Pro 17 its name and SeMW Extensions is a very simple extension I only installed it because I like having more information when I'm rendering a video.
In my own experience this extension has never interfered with Vegas Pro at all.

Robert Johnston wrote on 6/15/2024, 1:52 PM

@andy-0 I suppose you would have to remove that extension and then try the script again. Like for jetdv, the script works for me.

Intel Core i7 10700K CPU @ 3.80GHz (to 4.65GHz), NVIDIA GeForce RTX 2060 SUPER 8GBytes. Memory 32 GBytes DDR4. Also Intel UHD Graphics 630. Mainboard: Dell Inc. PCI-Express 3.0 (8.0 GT/s) Comet Lake. Bench CPU Multi Thread: 5500.5 per CPU-Z.

Vegas Pro 21.0 (Build 108) with Mocha Vegas

Windows 11 not pro

andy-0 wrote on 6/15/2024, 1:56 PM

@Robert Johnston i already tried this and the problem continued

Robert Johnston wrote on 6/15/2024, 5:39 PM

@andy-0 At this point, I would uninstall 17 and then reinstall it, just in case something was left behind from that extension. Also, reset it and clear the cache.

Intel Core i7 10700K CPU @ 3.80GHz (to 4.65GHz), NVIDIA GeForce RTX 2060 SUPER 8GBytes. Memory 32 GBytes DDR4. Also Intel UHD Graphics 630. Mainboard: Dell Inc. PCI-Express 3.0 (8.0 GT/s) Comet Lake. Bench CPU Multi Thread: 5500.5 per CPU-Z.

Vegas Pro 21.0 (Build 108) with Mocha Vegas

Windows 11 not pro

fbk wrote on 8/13/2024, 3:35 PM

So i had the exact same problem with the silence removal script, and after hours of figuring out the problem, i found out that somehow the script fails to convert the strings from the logfile into real doubles, means if at some spot the audio level is at "-22.60" as it says in the log, in the script it will get converted into "-2260" – therefore it is lower than the -40 quiet limit and will cut that out.

simplest way to fix this is to set the quiet limit to "-4000" instead of "-40.0"

jetdv wrote on 8/14/2024, 8:53 AM

@fbk Could it be that your log file is showing -22,60 instead of -22.60? It could be that the comma needs to be changed to a period before converting. I've seen that happen before. You might try making this one change (the bolded line):

            if (pieces[2].Contains("Inf"))
            {
                trackVolume = -100;
            }
            else
            {
                pieces[2] = pieces[2].replace(",", ".");

                trackVolume = Convert.ToDouble(pieces[2]);
            }