NEED HELP In Script: Transcode Media Inside VEGAS

iEmby wrote on 8/6/2024, 11:42 PM

        private void TranscodeMedia(RenderTemplate renderTemplate, string fileExtension)
        {
            List<TrackEvent> selectedEvents = GetSelectedEvents(vegas);
            if (selectedEvents == null || selectedEvents.Count == 0)
            {
                MessageBox.Show("No Event Selected.", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Create a list to store unique media objects
            List<Media> selectedMediaList = new List<Media>();

            // Iterate over the selected events and add their media to the list
            foreach (TrackEvent ev in selectedEvents)
            {
                Media media = ev.ActiveTake.Media;
                if (media != null && !selectedMediaList.Contains(media))
                {
                    selectedMediaList.Add(media);
                }
            }

            // Ask user if they want to replace the original media with the transcoded media
            DialogResult result = MessageBox.Show("Replace the original media with the transcoded media?", "Replace Media", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            bool replaceMedia = (result == DialogResult.Yes);

            foreach (Media media in selectedMediaList)
            {
                if (media == null)
                {
                    MessageBox.Show("Selected event has no media linked!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    continue;
                }

                // Calculate the new event position
                Timecode newEventStart = vegas.Project.Length + Timecode.FromMilliseconds(5000);

                // Create new tracks and add media as events
                VideoTrack videoTrack = new VideoTrack(vegas.Project.Tracks.Count, "TranscodeVideoTrack");
                vegas.Project.Tracks.Add(videoTrack);
                AudioTrack audioTrack = new AudioTrack(vegas.Project.Tracks.Count + 1, "TranscodeAudioTrack");
                vegas.Project.Tracks.Add(audioTrack);

                // Add new video and audio events
                TrackEvent newVideoEvent = videoTrack.AddVideoEvent(newEventStart, media.Length);
                TrackEvent newAudioEvent = audioTrack.AddAudioEvent(newEventStart, media.Length);

                // Set media streams for the new events
                MediaStream videoStream = media.GetVideoStreamByIndex(0);
                MediaStream audioStream = media.GetAudioStreamByIndex(0);
                if (videoStream != null)
                {
                    newVideoEvent.AddTake(videoStream);
                }
                if (audioStream != null)
                {
                    newAudioEvent.AddTake(audioStream);
                }

                // Create a loop region around the new event
                vegas.Transport.LoopRegionStart = newEventStart;
                vegas.Transport.LoopRegionLength = media.Length;

                // Define the output file path
                string mediaDirectory = Path.GetDirectoryName(media.FilePath);
                string outputFileName = $"{Path.GetFileNameWithoutExtension(media.FilePath)}_transcoded{fileExtension}";
                string outputFilePathForMedia = Path.Combine(mediaDirectory, outputFileName);

                // Setup render arguments
                RenderArgs renderArgs = new RenderArgs
                {
                    OutputFile = outputFilePathForMedia,
                    RenderTemplate = renderTemplate,
                    Start = newEventStart,
                    Length = media.Length
                };

                // Render the project
                RenderProject(renderArgs);

                // Replace or remove the event based on user's choice
                if (replaceMedia)
                {
                    Media newMedia = new Media(outputFilePathForMedia); // Create new Media object for the transcoded file

                    // Replace the original media in all events that use this media
                    foreach (TrackEvent selectedEvent in selectedEvents)
                    {
                        if (selectedEvent.ActiveTake.Media == media)
                        {
                            selectedEvent.ActiveTake.Media.ReplaceWith(newMedia);
                        }
                    }

                    // Remove the temporary events and tracks
                    videoTrack.Events.Remove(newVideoEvent);
                    audioTrack.Events.Remove(newAudioEvent);
                    vegas.Project.Tracks.Remove(videoTrack);
                    vegas.Project.Tracks.Remove(audioTrack);
                }
                else
                {
                    // Just remove the temporary events and tracks
                    videoTrack.Events.Remove(newVideoEvent);
                    audioTrack.Events.Remove(newAudioEvent);
                    vegas.Project.Tracks.Remove(videoTrack);
                    vegas.Project.Tracks.Remove(audioTrack);
                }
                vegas.UpdateUI();
            }
        }

 

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

VEGAS Scripts Collection By Me

GitHub Profile

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)

Comments

iEmby wrote on 8/7/2024, 1:37 AM

My Script is OK. This is a Bug

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

VEGAS Scripts Collection By Me

GitHub Profile

My YouTube Channel Dedicated to Only VEGAS Pro Tutorials

EDITROOM : My YouTube Channel (For VEGAS Tutorials)