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;
      }
    }
  }
}

8 Comments

  1. Mirko
    on April 24, 2008 at 6:32 am | Permalink

    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. on April 24, 2008 at 8:09 pm | Permalink

    Sorry, Mirko, I don’t. I’m running 8 here.

  3. on May 9, 2008 at 12:20 am | Permalink

    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,

  4. on May 9, 2008 at 10:31 am | Permalink

    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.

  5. on December 26, 2008 at 8:07 am | Permalink

    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.

  6. on December 26, 2008 at 8:15 am | Permalink

    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();
    }

  7. on May 27, 2009 at 12:57 am | Permalink

    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.

  8. on May 27, 2009 at 8:56 am | Permalink

    Excellent catch! I’ve updated the script to fix that. Thanks!

Post a Comment

Comments are moderated according to our moderation policy. Sometimes comments are delayed by our spam filter. We try to release them as soon as possible.

Your email is never published nor shared. Required fields are marked *

*
*