Sony Vegas Script to Change the Pixel Aspect Ratio of Images

I spent far too long getting this to work again because I forgot to transfer the script from one computer to another, and all of the web references to this technique are, shall we say, broken like a character on Battlestar Galactica. So for anyone else who might need this, I give you a Sony Vegas script to change the pixel aspect ratio of all images in your media pool, written in C#.


/**
 * This script sets the Pixel Aspect Ratio of all images to be 0.9091.
 *
 * Author: Stephen Granade      Date: 21 April 2008
 **/

using System.Text.RegularExpressions;
using Sony.Vegas;

public class EntryPoint {
  public void FromVegas(Vegas vegas) {
    foreach (Media media in vegas.Project.MediaPool) {
      Match m = Regex.Match(media.FilePath, "(jpe?g|png|gif)");
      if (m.Success) {
        VideoStream vs = media.GetVideoStreamByIndex(0);
        vs.PixelAspectRatio = 0.9091;
      }
    }
  }
}

10 thoughts on “Sony Vegas Script to Change the Pixel Aspect Ratio of Images

  1. Hey this sounds extremely useful, but I tried this in Vegas 6 with a bunch of photos on my timeline but it gives the following error:
    Compilation errors:

    ‘System.Text.RegularExpressions.Regex’ does not contain a definition for ‘match’
    ‘Sony.Vegas.Media’ does not contain a definition for ‘GetVideoStreamByIndex’

    System.ApplicationException: Failed to compile.
    at Sony.Vegas.CodeDomScriptManager.Compile()
    at Sony.Vegas.ScriptHost.RunScript(Boolean fCompileOnly)

    Do you know a quick fix? I have zero experience with scripts in Vegas…
    Cheers, Mirko

  2. Hi Stephen

    This looks exactly what I need! But I need it to change the p.a.r. on a video file.

    Could it be modified to work with a .MP4 file?

    See link above for more info.

    Also, do I need to compile it in Visual c#??

    Thanks a lot,

  3. You should be able to modify it to find .mp4 files instead of still images. Try changing “(jpe?g|png|gif)” to “mp4” in the script. And Vegas 8 should do the compilation for you. You shouldn’t need to compile it yourself.

  4. C:\Program Files (x86)\Sony\Vegas Pro 8.0\Script Menu\Change pixel aspect ratio.cs(13) : ‘System.Text.RegularExpressions.Regex’ does not contain a definition for ‘match’

    ____________

    In Vegas 8.

  5. I google work script:

    /**
    * This script sets the Pixel Aspect Ratio all media with a video
    * stream to the desired value. The value must be edited below.
    * The file extension of this file needs to be changed from .txt to .js
    * and then drag it into the appropriate script folder, for example
    * C:/Program Files/Sony/Vegas 7.0/Script Menu
    *
    * c.2006 DVFilm
    *
    **/
    import Sony.Vegas;

    // Set the following variable to the Pixel Aspect Ratio you wish to
    // have all video streams use.

    var targetPAR = 1.0926;

    var mediaEnum = new Enumerator(Vegas.Project.MediaPool);
    while (!mediaEnum.atEnd()) {
    var media = mediaEnum.item();
    if (media.HasVideo())
    {
    var videoStream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
    videoStream.PixelAspectRatio = targetPAR;
    }
    mediaEnum.moveNext();
    }

  6. This error:
    C:\Program Files (x86)\Sony\Vegas Pro 8.0\Script Menu\Change pixel aspect ratio.cs(13) : ‘System.Text.RegularExpressions.Regex’ does not contain a definition for ‘match’

    Is caused because of a typo in the script. It should be Regex.Match, not Regex.match. It needs to be capital M.

  7. This works like a charm… switched to .mov, it didn’t even blinked and BAM, 25 Mov files go from 4:3 to 16:9

    You rule 🙂 thanks a lot!

Comments are closed.