Directory Converter Customization

speedymedia wrote on 5/11/2007, 1:53 AM
Hey Guys,

I have ONE specific task I need to accomplish with Vegas 7 and I'm hoping some brilliant minds on this forum can help me out.

All I want to do is take a directory of .mov files and speed them up using the Playback Rate (to say 1.6) and render them out.

The directory Converter script that comes with the Vegas 6 scripting SDK is really close but it's missing the Playback Rate changer.

I've searched the forums for scripts that change the playback rate and all I found was this...

http://www.sonycreativesoftware.com/forums/ShowMessage.asp?ForumID=21&MessageID=264536

and it correctly changes the playback rate of the clip, but somethings wrong with the event length section. It makes insanely long loops of the modified clip.

Could someone help me out? Thanks!

Comments

johnmeyer wrote on 5/11/2007, 6:40 AM
I have a script that I got somewhere that renders every file in a folder to a new format. I didn't write it, and it's pretty crude, but I think I used it 3-4 years ago, and it worked fine.

I just looked at it, and I think it could be modified, with the addition of 2-4 lines of code, to change the playback rate and length. Thus, each clip would be made shorter by a factor of 1.6 (or whatever) and also sped up by the same amount.

Is that what you are looking for?

FWIW, here's the script (without the modifications):
/**
* This script renders media files found in an input directory using a
* single render template and saves the results in an output
* directory. To change the input directory, output directory, and
* render template, edit the variables below.
*
* NOTE!!! This script requires Vegas 4.0c.
*
* Revision Date: May 01, 2003.
**/

import System;
import System.Text;
import System.IO;
import System.Windows.Forms;
import Sony.Vegas;

// The inputDirectory variable specifies where the rendered files will
// be created (do not include the trailing back-slash)
var inputDirectory = "J:\\HDV Test";

// The outputDirectory variable specifies where the rendered files
// will be created (do not include the trailing back-slash)
var outputDirectory = "I:\\HDV Intermediates";

// The rendererRegexp and templateRegexp variable are regular
// expressions used to match renderer file type names and template
// names.
var rendererRegexp = /Dolby Digital AC-3/;
var templateRegexp = /Stereo/;

// The inputFileRegexp is used to filter the input files. Only those
// whose file name matches using this regular expression will be
// converted. The following will match all files the end with avi
// (ignoring case):
var inputFileRegexp = /.mp3$/i;

// This version will match all input files.
//var inputFileRegexp = /.*/;

// The overwriteExistingFiles variable determines whether or not it is
// OK to overwrite files that may already exist whose name is the same
// as ones created by running this script. For safety, the default is
// false. Set the variable to true to allow overwrites.
var overwriteExistingFiles = false;


try {

// make sure the output directory exists
if (!Directory.Exists(inputDirectory))
{
var msg = new StringBuilder("The input directory (");
msg.Append(outputDirectory);
msg.Append(") does not exist.\n");
msg.Append("Please edit the script to specify an existing directory.");
throw msg;
}

// make sure the output directory exists
if (!Directory.Exists(outputDirectory))
{
var msg = new StringBuilder("The output directory (");
msg.Append(outputDirectory);
msg.Append(") does not exist.\n");
msg.Append("Please edit the script to specify an existing directory.");
throw msg;
}

var renderer = FindRenderer(rendererRegexp);
if (null == renderer) {
throw "Failed to find renderer";
}

var renderTemplate = FindRenderTemplate(renderer, templateRegexp);
if (null == renderTemplate) {
throw "Failed to find render template";
}

var newExtension = renderer.FileExtension.substring(1);

// create a new project with one video track and one audio track.
var proj = new Project();

var videoTrack = new VideoTrack();
proj.Tracks.Add(videoTrack);

var audioTrack = new AudioTrack();
proj.Tracks.Add(audioTrack);

// save the new project to the output directory
//Vegas.SaveProject(Path.Combine(outputDirectory, "temp.veg"));

// enumerate the files in the input directory
var fileEnum = new Enumerator(Directory.GetFiles(inputDirectory));
while (!fileEnum.atEnd()) {
var inputFile = fileEnum.item();

// skip files that don't end with the right extension
if (null == inputFile.match(inputFileRegexp)) {
fileEnum.moveNext();
continue;
}

// skip files that are not valid media files.
var media = new Media(inputFile);
if (!media.IsValid()) {
fileEnum.moveNext();
continue;
}

var videoStream = media.Streams.GetItemByMediaType(MediaType.Video, 0);
var audioStream = media.Streams.GetItemByMediaType(MediaType.Audio, 0);

// if needed, add a video event and associate video stream
if (null != videoStream) {
var videoLength = videoStream.Length;
var videoEvent = new VideoEvent(new Timecode(), videoLength);
videoTrack.Events.Add(videoEvent);
var videoTake = new Take(videoStream);
videoEvent.Takes.Add(videoTake);
}

// if needed, add a audio event and associate audio stream
if (null != audioStream) {
var audioLength = audioStream.Length;
var audioEvent = new AudioEvent(new Timecode(), audioLength);
audioTrack.Events.Add(audioEvent);
var audioTake = new Take(audioStream);
audioEvent.Takes.Add(audioTake);
}

var outputFileName = Path.GetFileNameWithoutExtension(inputFile) + newExtension;
var outputPath = Path.Combine(outputDirectory, outputFileName);

var status = DoRender(outputPath, renderer, renderTemplate);
if (status == RenderStatus.Canceled) {
// may want have a dialog here allowing user to
// continue with remaining files.
break;
} else if (status != RenderStatus.Complete) {
throw "Failed on input file: " + inputFile;
}

// clean up the project.
videoTrack.Events.Clear();
audioTrack.Events.Clear();
proj.MediaPool.Remove(inputFile);

fileEnum.moveNext();
}

} catch (e) {
if (!e.skipMessageBox)
MessageBox.Show(e);
}

// Perform the render. The Render method returns a member of the
// RenderStatus enumeration which is, in turn, returned by this
// function.
function DoRender(fileName, rndr, rndrTemplate) {
ValidateFileName(fileName);

// make sure the file does not already exist
if (!overwriteExistingFiles && File.Exists(fileName)) {
throw "File already exists: " + fileName;
}

// perform the render. The Render method returns
// a member of the RenderStatus enumeration. If
// it is anything other than OK, exit the loops.
//var status = Vegas.Render(fileName, rndrTemplate);
var status = Vegas.Render(fileName, rndrTemplate);
return status;
}

function ValidateFileName(fileName : System.String) {
if (fileName.length > 260)
throw "file name too long: " + fileName;
var illegalCharCount = Path.InvalidPathChars.Length;
var i = 0;
while (i < illegalCharCount) {
if (0 <= fileName.IndexOf(Path.InvalidPathChars[i])) {
throw "invalid file name: " + fileName;
}
i++;
}
}

function FindRenderer(rendererRegExp : RegExp) : Renderer {
var rendererEnum : Enumerator = new Enumerator(Vegas.Renderers);
while (!rendererEnum.atEnd()) {
var renderer : Renderer = Renderer(rendererEnum.item());
if (null != renderer.FileTypeName.match(rendererRegExp)) {
return renderer;
}
rendererEnum.moveNext();
}
return null;
}

function FindRenderTemplate(renderer : Renderer, templateRegExp : RegExp) : RenderTemplate {
var templateEnum : Enumerator = new Enumerator(renderer.Templates);
while (!templateEnum.atEnd()) {
var renderTemplate : RenderTemplate = RenderTemplate(templateEnum.item());
if (null != renderTemplate.Name.match(templateRegExp)) {
return renderTemplate;
}
templateEnum.moveNext();
}
return null;
}




johnmeyer wrote on 5/11/2007, 9:23 AM
OK, I looked at the script you linked to:

Change Playrate and Keep event Content

and it contains lots of errors. The big one that keeps the script from working correctly is failing to type the ClipLen variable as Double. Here's a script, based on that one, that does work. You should be able to "bolt in" the 3-4 lines you need from this script into the directory converter script. Since you are going to be using this with the directory convertor script, there will only be two tracks and only one event on each track, and you don't have to worry about offsets (something that Ed pointed out, in the post you referenced, that you would normally need to worry about -- along with takes). You don't have to deal with either thing in this case, so this simple code should do the trick.

I commented out the line that forces the script to only look at video tracks. This way, ALL tracks, including audio, will be changed.


// Change playback rate of all clips in project.
// Modify the newPlaybackRate variable below as needed.

// Based on code from previous script.
// John H. Meyer
// May 11, 2007


import System;
import System.IO;
import Sony.Vegas;
import System.Windows.Forms;

// Change this line to change playback rate:
var newPlaybackRate = 1.6;

try
{
// step through all selected video events:
for (var track in Vegas.Project.Tracks) {
for (var evnt in track.Events) {
// if (!evnt.Selected || evnt.MediaType != MediaType.Video) continue;
var ClipLen: Double = evnt.Length.ToMilliseconds();
ClipLen = ClipLen / newPlaybackRate ;
evnt.PlaybackRate = newPlaybackRate;
evnt.Length = new Timecode(ClipLen);
}
}
}

catch (errorMsg)
{
MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}




speedymedia wrote on 5/11/2007, 6:38 PM
Thanks John,

That second script did the trick. I dropped it in the directory converter script and it works like a charm! Now I can speed up all those lynda.com tutorial .mov files to a speed that doesn't put me to sleep! :)

Cheers!
johnmeyer wrote on 5/11/2007, 8:23 PM
Glad to help!