How to automatically match output aspect in the Pan/Crop window ?

MrInfinity wrote on 3/28/2024, 5:49 AM

All is in the title, is there a way to tell Vegas to automatically match output aspect for every media I load into my project without having to do it manually ? I use hundreds of different medias, all types of images with different sizes so it would be some time won to avoid the Pan/Crop > Right Click > Match Output Aspect everytime.

Thanks in advance !

Comments

3POINT wrote on 3/28/2024, 6:03 AM

Take a look in the preferences, there you can set that images and video automatically match the output aspect ratio.

MrInfinity wrote on 3/28/2024, 6:17 AM

I looked deep into all the Preferences panels but I can't find this exact option.

Former user wrote on 3/28/2024, 7:10 AM

@MrInfinity Have a look at this thread, there's a script that will poss/prob do what you want

https://www.vegascreativesoftware.info/us/forum/feature-request-match-output-aspect-without-rescaling--139164/?page=4#ca899743

Reyfox wrote on 3/28/2024, 7:34 AM

@MrInfinity I usually have photos and videos on the same editing timeline. So, in Preferences>Editing, I have ticked "Automatically crop still images added to the timeline".

Of course this will only crop to the timeline aspect ratio in the middle of the image. So you might have to readjust the cropping. This is easy. Event/Pan Crop is where this is done. Below is an image that is 4:3 cropped to UHD 4K as shown in the Event/Pan Crop. As you can see, you can readjust to get what you want.

I have a friend that struggled with doing all the images manually in Even/Pan Crop, until I showed him that this would speed up his editing. He said he wished he knew this earlier.

Last changed by Reyfox on 3/28/2024, 7:36 AM, changed a total of 1 times.

Newbie😁

Vegas Pro 22 (VP18-21 also installed)

Win 11 Pro always updated

AMD Ryzen 9 5950X 16 cores / 32 threads

32GB DDR4 3200

Sapphire RX6700XT 12GB Driver: 25.5.1

Gigabyte X570 Elite Motherboard

Panasonic G9, G7, FZ300

Former user wrote on 3/28/2024, 7:35 AM

@MrInfinity Here I've got two scripts prob written by @jetdv.

This is the orig pic in Pan/Crop.

-------

This script will do this

/**
 * Resize video events to match the aspect ratio of the project. This
 * removes any letterboxing or pillarboxing.  The script will operate
 * on the selected video event or, if none are selected, it will
 * operate on all video events.
 *
 * Revision Date: March 26, 2010.
 **/

using System;
using System.Collections.Generic;
using ScriptPortal.Vegas;

public class EntryPoint
{

    public void FromVegas(Vegas vegas)
    {
        double dWidthProject  = vegas.Project.Video.Width;
        double dHeightProject = vegas.Project.Video.Height;
        double dPixelAspect   = vegas.Project.Video.PixelAspectRatio;
        double dAspect        = dPixelAspect * dWidthProject / dHeightProject;

        List<VideoEvent> events = new List<VideoEvent>();
        AddSelectedVideoEvents(vegas, events);
        if (0 == events.Count)
        {
            AddAllVideoEvents(vegas, events);
        }

        foreach (VideoEvent videoEvent in events)
        {
            Take take = videoEvent.ActiveTake;
            if (null == take) continue;
            VideoStream videoStream = take.MediaStream as VideoStream;
            if (null == videoStream) continue;
            double dMediaPixelAspect = videoStream.PixelAspectRatio;
            foreach (VideoMotionKeyframe keyframe in videoEvent.VideoMotion.Keyframes)
            {
                MatchOutputAspect(keyframe, dMediaPixelAspect, dAspect);                                                                     
            }
        }
    }

    void AddSelectedVideoEvents(Vegas vegas, List<VideoEvent> events)
    {
        foreach (Track track in vegas.Project.Tracks)
        {
            if (track.IsVideo())
            {
                foreach (VideoEvent videoEvent in track.Events)
                {
                    if (videoEvent.Selected)
                    {
                        events.Add(videoEvent);
                    }
                }
            }
        }
    }

    void AddAllVideoEvents(Vegas vegas, List<VideoEvent> events)
    {
        foreach (Track track in vegas.Project.Tracks)
        {
            if (track.IsVideo())
            {
                foreach (VideoEvent videoEvent in track.Events)
                {
                    events.Add(videoEvent);
                }
            }
        }
    }

    void MatchOutputAspect(VideoMotionKeyframe keyframe, double dMediaPixelAspect, double dAspectOut)
    {
        VideoMotionKeyframe keyframeSave = keyframe;
        try
        {
            double rotation = keyframe.Rotation;    
            
            // undo rotation so that we can get at correct aspect ratio.
            //
            keyframe.RotateBy(-rotation);

            double dWidth         = Math.Abs(keyframe.TopRight.X   - keyframe.TopLeft.X);
            double dHeight        = Math.Abs(keyframe.BottomLeft.Y - keyframe.TopLeft.Y);
            double dCurrentAspect = dMediaPixelAspect * dWidth / dHeight;
            double centerY        = keyframe.Center.Y;
            double centerX        = keyframe.Center.X;        
        
            double dFactor;
        
            VideoMotionBounds bounds = new VideoMotionBounds(keyframe.TopLeft, keyframe.TopRight, keyframe.BottomRight, keyframe.BottomLeft);

            if (dCurrentAspect < dAspectOut)
            {
                // alter y coords            
                dFactor = dCurrentAspect / dAspectOut;            
                        
                bounds.TopLeft.Y     = (float) ((bounds.TopLeft.Y     - centerY) * dFactor + centerY);
                bounds.TopRight.Y    = (float) ((bounds.TopRight.Y    - centerY) * dFactor + centerY);
                bounds.BottomLeft.Y  = (float) ((bounds.BottomLeft.Y  - centerY) * dFactor + centerY);
                bounds.BottomRight.Y = (float) ((bounds.BottomRight.Y - centerY) * dFactor + centerY);
            }
            else
            {                          
                // alter x coords
                dFactor = dAspectOut / dCurrentAspect;            
                        
                bounds.TopLeft.X     = (float) ((bounds.TopLeft.X     - centerX) * dFactor + centerX);
                bounds.TopRight.X    = (float) ((bounds.TopRight.X    - centerX) * dFactor + centerX);
                bounds.BottomLeft.X  = (float) ((bounds.BottomLeft.X  - centerX) * dFactor + centerX);
                bounds.BottomRight.X = (float) ((bounds.BottomRight.X - centerX) * dFactor + centerX);
            }
        
            // set it to new bounds
            keyframe.Bounds = bounds;
        
            // restore rotation.        
            keyframe.RotateBy (rotation);
        
        }
        catch (Exception e)
        {
            // restore original settings on error
            keyframe = keyframeSave;
        }    
    }

}


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

This other script will do this

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;

            float scrWidth = myVegas.Project.Video.Width;
            float scrHeight = myVegas.Project.Video.Height;

            foreach (Track myTrack in myVegas.Project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (VideoEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            MatchAspect(evnt);

                            VideoEvent videoEvent = evnt as VideoEvent;
                            VideoMotionKeyframes keyframes = videoEvent.VideoMotion.Keyframes;
                            int cKeyframes = keyframes.Count;
                            for (int jj = cKeyframes - 1; jj >= 0; jj--)
                            {
                                VideoMotionKeyframe thisKeyframe = keyframes[jj] as VideoMotionKeyframe;

                                float dWidth = Math.Abs(thisKeyframe.TopRight.X - thisKeyframe.TopLeft.X);
                                float dHeight = Math.Abs(thisKeyframe.BottomLeft.Y - thisKeyframe.TopLeft.Y); ;

                                MediaStream mediaStream = GetActiveMediaStream(videoEvent);
                                VideoStream videoStream = mediaStream as VideoStream;
                                VideoOutputRotation vRotation = videoStream.Rotation;

                                float dFullHeight = videoStream.Height;
                                float dFullWidth = videoStream.Width;
                                if ((vRotation == VideoOutputRotation.QuarterTurnClockwise) || (vRotation == VideoOutputRotation.QuarterTurnCounterclockwise))
                                {
                                    dFullHeight = videoStream.Width;
                                    dFullWidth = videoStream.Height;
                                }

                                float pwid = 0.0F;
                                float wpwid = 0.0F;
                                bool smaller = false;

                                if (dFullHeight > scrHeight)
                                {
                                    pwid = dFullHeight / dHeight * 100;
                                }
                                else
                                {
                                    pwid = dHeight / dFullHeight * 100;
                                    smaller = true;
                                }

                                if (dFullWidth > scrWidth)
                                {
                                    smaller = false;
                                    wpwid = dFullWidth / dWidth * 100;
                                }
                                else
                                {
                                    wpwid = dWidth / dFullWidth * 100;
                                }

                                if (wpwid > pwid && !smaller)
                                {
                                    pwid = wpwid;
                                }
                                if (wpwid < pwid && smaller)
                                {
                                    pwid = wpwid;
                                }

                                ScaleKeyframe(thisKeyframe, pwid, 0);
                            }
                        }
                    }
                }
            }
        }

        public void MatchAspect(TrackEvent trackEvent)
        {
            float dWidthProject = myVegas.Project.Video.Width;
            float dHeightProject = myVegas.Project.Video.Height;
            double dPixelAspect = myVegas.Project.Video.PixelAspectRatio;
            double dAspect = dPixelAspect * dWidthProject / dHeightProject;

            MediaStream mediaStream = GetActiveMediaStream(trackEvent);
            if (!(mediaStream == null))
            {
                VideoStream videoStream = mediaStream as VideoStream;

                double dMediaPixelAspect = videoStream.PixelAspectRatio;
                VideoEvent videoEvent = trackEvent as VideoEvent;
                VideoMotionKeyframes keyframes = videoEvent.VideoMotion.Keyframes; ;

                int cKeyframes = keyframes.Count;

                for (int jj = cKeyframes - 1; jj >= 0; jj--)
                {
                    VideoMotionKeyframe thisKeyframe = keyframes[jj] as VideoMotionKeyframe;
                    MatchOutputAspect(thisKeyframe, dMediaPixelAspect, dAspect);
                }
            }
            myVegas.UpdateUI();
        }

        public MediaStream GetActiveMediaStream(TrackEvent trackEvent)
        {
            try
            {
                if (!(trackEvent.ActiveTake.IsValid()))
                {
                    throw new ArgumentException("empty or invalid take");
                }

                Media media = myVegas.Project.MediaPool.Find(trackEvent.ActiveTake.MediaPath);
                if (null == media)
                {
                    MessageBox.Show("missing media");
                    throw new ArgumentException("missing media");
                }

                MediaStream mediaStream = media.Streams.GetItemByMediaType(MediaType.Video, trackEvent.ActiveTake.StreamIndex);
                return mediaStream;
            }
            catch (Exception e)
            {
                MessageBox.Show("{0}", e.Message);
                return null;
            }
        }


        public void MatchOutputAspect(VideoMotionKeyframe keyframe, double dMediaPixelAspect, double dAspectOut)
        {
            VideoMotionKeyframe keyframeSave = keyframe;

            try
            {
                double rotation = keyframe.Rotation;
                keyframe.RotateBy(-rotation);

                float dWidth = Math.Abs(keyframe.TopRight.X - keyframe.TopLeft.X);
                float dHeight = Math.Abs(keyframe.BottomLeft.Y - keyframe.TopLeft.Y);
                double dCurrentAspect = dMediaPixelAspect * dWidth / dHeight;
                float centerY = keyframe.Center.Y;
                float centerX = keyframe.Center.X;
                double dFactor;

                VideoMotionBounds bounds = new VideoMotionBounds(keyframe.TopLeft, keyframe.TopRight, keyframe.BottomRight, keyframe.BottomLeft);
                if (dCurrentAspect < dAspectOut)
                {
                    // alter y coords            
                    dFactor = dCurrentAspect / dAspectOut;

                    bounds.TopLeft.Y = (bounds.TopLeft.Y - centerY) * (float)dFactor + centerY;
                    bounds.TopRight.Y = (bounds.TopRight.Y - centerY) * (float)dFactor + centerY;
                    bounds.BottomLeft.Y = (bounds.BottomLeft.Y - centerY) * (float)dFactor + centerY;
                    bounds.BottomRight.Y = (bounds.BottomRight.Y - centerY) * (float)dFactor + centerY;
                }
                else
                {
                    // alter x coords
                    dFactor = dAspectOut / dCurrentAspect;

                    bounds.TopLeft.X = (bounds.TopLeft.X - centerX) * (float)dFactor + centerX;
                    bounds.TopRight.X = (bounds.TopRight.X - centerX) * (float)dFactor + centerX;
                    bounds.BottomLeft.X = (bounds.BottomLeft.X - centerX) * (float)dFactor + centerX;
                    bounds.BottomRight.X = (bounds.BottomRight.X - centerX) * (float)dFactor + centerX;
                }

                // set it to new bounds
                keyframe.Bounds = bounds;

                // restore rotation.        
                keyframe.RotateBy(rotation);
            }
            catch
            {
                // restore original settings on error
                keyframe = keyframeSave;
            }
        }

        public void ScaleKeyframe(VideoMotionKeyframe keyframe, float szChange, float rotAngle)
        {
            float cWidth = (1 / (szChange / 100));
            float cHeight = (1 / (szChange / 100));

            if (szChange > 100)
            {
                cWidth = (szChange / 100);
                cHeight = (szChange / 100);
            }

            VideoMotionVertex bounds = new VideoMotionVertex(cWidth, cHeight);

            keyframe.ScaleBy(bounds);
            keyframe.RotateBy((rotAngle * (Math.PI / 180)));
        }

    }
}

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

 

MrInfinity wrote on 3/28/2024, 8:35 AM

@Reyfox Thank you ! This is exactly what I was looking for. It seems I don't have the parameter for videos in my version of Vegas Pro (19.0), but the one for still images will already make me win some time !

@Former user Thank you for the scripts ! I will look into it, especially the second one but first I need to learn how scripts works in Vegas Pro. 😅

Former user wrote on 3/28/2024, 8:46 AM

@MrInfinity It's fairly easy, have a look in Documents - Vegas Script menu, C:\Users\username\OneDrive\Documents\Vegas Script Menu

  1. If there's a script there, copy & paste it to the same folder, (or right click - New - Text Document)
  2. Rename it whatever you want with .cs at the end (Match output without cropping.cs) & open it with Notepad,
  3. Delete the contents & paste in the script above.

This should show in Vegas - Tools - Scripting, select your event/s on the timeline, click the script. (If Pan/Crop is already open on that event you'll have to click Pan/Crop on the event to update the Pan/Crop window.

------

PS. Script buttons can be added to the toolbar at the top to make it easier.

Reyfox wrote on 3/28/2024, 9:00 AM

@MrInfinity, the auto cropping of images and/or videos is also present in VP19. I just checked. It will crop to whatever the Project Video Properties are.

This is from VP19.

Newbie😁

Vegas Pro 22 (VP18-21 also installed)

Win 11 Pro always updated

AMD Ryzen 9 5950X 16 cores / 32 threads

32GB DDR4 3200

Sapphire RX6700XT 12GB Driver: 25.5.1

Gigabyte X570 Elite Motherboard

Panasonic G9, G7, FZ300

MrInfinity wrote on 3/28/2024, 9:16 AM

@Former user Nice ! I just added the second script, it works fine ! I was about to write that the only problem is that it takes as much time to open panels and run the script as manually resize the images but this script button will be even faster !

@Reyfox Well, for unknown reason it doesn't show up in mine then. It's in french but you can see I only have the parameter for "images" and none for "videos" (same word in french)

Former user wrote on 3/28/2024, 9:25 AM

@MrInfinity Yep 👍 I like to play with fxs & a lot need the event to be the size of the project in order to work correctly, so I us this script quite a lot 👍

Reyfox wrote on 3/28/2024, 9:26 AM

@MrInfinity thinking that this might be one of those language differences. People who have installed with the French language have had other similar issues.

If you feel "experimental", you can uninstall and reinstall in English to see if it is there. Or, install on another computer (you can install on 2 separate computers) in English to see if it appears.

You can afterwards, "deactivate" it on the other PC from Help>Deactivate the Software on this PC".

Newbie😁

Vegas Pro 22 (VP18-21 also installed)

Win 11 Pro always updated

AMD Ryzen 9 5950X 16 cores / 32 threads

32GB DDR4 3200

Sapphire RX6700XT 12GB Driver: 25.5.1

Gigabyte X570 Elite Motherboard

Panasonic G9, G7, FZ300

MrInfinity wrote on 3/28/2024, 9:33 AM

@Reyfox Oh ok thank you for the tips, I don't feel enough experimental for today haha but I might try that some day later !

mark-y wrote on 3/28/2024, 11:27 AM

@Reyfox I wish I had known that too -- like for the past ten years!