Opening files with scripts

ChrisFontenot13 wrote on 12/1/2004, 2:31 PM
How do you get vegas to open a file (mpg) using JS in 4.0e?
I'm trying to write a quick "trimmer" than renders to a temp file, closes the project, deletes the formerly opened file, renames the temp to the name of the formerly opened file, and then opens the file. The open command is all I am missing.

I was wondering if there was any way to get vegas to grab the "next file in line" in a directory? Or to have it pop to the "Open" screen? Opening everything on a single timeline isn't practical for me, and I often have fifteen or so videos to run through in an afternoon... any ideas?

Comments

ChrisFontenot13 wrote on 12/2/2004, 2:49 PM
Boy, is my face red.

Vegas.OpenProject();

Got the trimmer written if anyone's interested. (4.0e)

Still don't know how to read in all files in a directory, but I'm sure someone out there knows something.
rcampbel wrote on 12/2/2004, 3:04 PM
System.IO.Directory.GetFiles("C:\");

will read all of the files in a directory and return them as a string array.

The .Net Framework SDK documentation has a lot of info about the various .Net classes.

Randall
ChrisFontenot13 wrote on 12/3/2004, 11:30 AM
Thanks for the reference!

Ok, I'm digging through the .Net Documentation, but I can't figure out how to step through each instance of the items to retreive individual videos stored in the string array. I'm VERY new to all this.
rcampbel wrote on 12/4/2004, 6:00 PM
You might want to read the JScript reference material in the .Net documentation. It is in the contents under Reference, then Compiler and Language Reference, then JScript. Look for the "for...in" statement.

But, I will give you the answer here. The quick way is:

var files = Directory.GetFiles("C:\");
for (var file in files)
{
Vegas.OpenProject(file);
}
ChrisFontenot13 wrote on 12/13/2004, 9:18 AM
Wow! I've gotten that script off the ground, and very well. This is my best chunk of code to date. Thanks for the references.
hdguru wrote on 12/15/2004, 9:45 PM
I would like to see the script. Would you mind posting the code? Thanks.
ChrisFontenot13 wrote on 12/16/2004, 8:14 AM
This is the first script I've written myself. It's a bit of a hack, but it works. It will require some editing to personalize (I'm hoping to improve its portability). We often read 30 or 40 videos in at a time, then open each in turn and save a few jpgs from it using (filename + "P00.jpg"). I wrote this to check for the first one that does not have at least one Jpeg saved from it and open that one.





/**
* Code written by Christopher M. Fontenot
* 12-10-04
*
* This script searches in a sourcefile directory for MPG's,
* then checks each of those for JPGs that are named similarly.
* If there is no associated JPG, it opens the current MPG.
*
* It is easily adaptable into a function that returns the
* name of the file to open; that's how I usually use it.
*
* I know its a bit rough; this is my first script without
* outsourcing. Hope it proves useful!
*
* Questions, comments, or advice to ChrisFontenot13@yahoo.com
* http://groups.yahoo.com/group/AcadianaFreecycle
**/



import System.IO;


import System.Object;

import Microsoft.Win32;

import SonicFoundry.Vegas;

import System.Windows.Forms;




// This looks in our sourcefile directory and loads all MPGs into an array
var MpegsInStage = Directory.GetFiles("c:\\stage\\", "*.mpg");

// If there are no MPGS in the sourcefile directory, throw an error message
if (MpegsInStage.length==0) throw ("There are no videos in C:\\Stage!");


var JpegsWithSameName = null;
var JpegCount = 0;
var CurrentMpeg = null;
var FileToOpen = null;

try
{
var MpegWhileCount=0;
while (MpegWhileCount != MpegsInStage.length)
{

// CurrentMpeg = drive, directory name, FILENAME, extension, this gets out FILENAME only

CurrentMpeg = (MpegsInStage[MpegWhileCount].substr(9,20));
JpegsWithSameName = Directory.GetFiles("c:\\stage\\", (CurrentMpeg + "P??.jpg"));
JpegCount = JpegsWithSameName.length;

// All of our MPEGS are named SampleMpeg.Mpg
// We save snapshots like so: SampleMpegP00.Jpg





if (JpegCount==0)
{
// Set the current MPEG to Open and Tell the While loop to end
var FileToOpen = MpegsInStage[MpegWhileCount];
MpegWhileCount = MpegsInStage.length;
}
else
{
// if associated JPGS exist, go on to the next MPG
MpegWhileCount++;
}
}

Vegas.OpenProject(FileToOpen);

Vegas.UpdateUI();

} catch(e) {MessageBox.Show(e);}

ChrisFontenot13 wrote on 12/16/2004, 11:43 AM
Here's the Trimmer, Thanks to JetDV for Permission to use his code!


/**
* This Script grabs the open track, pulls the name from the
* Media Pool (if this is the only open file it works...), and
* renders it to (FileName +".tmp"), then reopens the file.
*
* Written 12-04-04 by Christopher M. Fontenot
* Questions, comments, or advice email ChrisFontenot13@yahoo.com
* http://groups.yahoo.com/group/AcadianaFreecycle
*
* Modified from code posted on http://www.jetdv.com/vegas
*
**/

import System;

import System.IO;


import System.Object;

import Microsoft.Win32;
import System.Diagnostics;
import SonicFoundry.Vegas;

import System.Windows.Forms;








try {


var mediaEnum = new Enumerator(Vegas.Project.MediaPool);
while (!mediaEnum.atEnd()) {
var media = mediaEnum.item();
var mediaPath = media.FilePath;
mediaEnum.moveNext();
}

var track = FindSelectedTrack();

var fileToBeOpened=mediaPath;
var TempFile=(fileToBeOpened + ".tmp")
var NewFileDirectory = (mediaPath.substr(0,9)) // reads in first 9 chars, length of "c:\\stage" This will need to be modified for your use
var videoRendererRegexp = /MPEG-1/;
var videoTemplateRegexp = /HUB/; // This will need to be modified for your use
var renderStart = new Timecode();
var renderLength = Vegas.Project.Length;

if (Vegas.SelectionLength > new Timecode()) {
var msgBoxResult = MessageBox.Show("Render selected region only?", "Region Selected", MessageBoxButtons.YesNo);
if (msgBoxResult == DialogResult.Yes) {
renderStart = Vegas.SelectionStart;
renderLength = Vegas.SelectionLength;
}
}

var projDir, projName, videoOutputFile;
var projFile = Vegas.Project.FilePath;
if ((null == projFile) || (0 == projFile.length)) {
projDir = "";
projName = "Untitled";
} else {
projDir = NewFileDirectory;
projName = "Untitled";
}

videoOutputFile = (TempFile);

var mpegRenderer = findRenderer(videoRendererRegexp);
if (null == mpegRenderer)
throw "failed to find MPEG-1 renderer";

var videoTemplate = findTemplate(mpegRenderer, videoTemplateRegexp);
if (null == videoTemplate)
throw "failed to find video template";
var renderStatus = Vegas.Render(videoOutputFile, videoTemplate, renderStart, renderLength);
if (renderStatus != RenderStatus.Complete)
throw "render not complete";

// Start a new project without prompting
Vegas.NewProject(false, false);

// Delete the old file, move the temp into its place
File.Delete(fileToBeOpened);
File.Move(TempFile, fileToBeOpened);
Vegas.UpdateUI();
Vegas.OpenProject(fileToBeOpened);

} catch (e) { MessageBox.Show(e); }


//



// FUNCTIONS
//
// find a render template that matches the given regular expression.

function findTemplate(renderer, re) {
var templateEnum = new Enumerator(renderer.Templates);
while (!templateEnum.atEnd()) {
var template = templateEnum.item();
if (null != template.Name.match(re)) {
return template;
}
templateEnum.moveNext();
}
return null;
}


// find a renderer that matches the given regular expression.

function findRenderer(re) {
var rendererEnum = new Enumerator(Vegas.Renderers);
while (!rendererEnum.atEnd()) {
var renderer = rendererEnum.item();
if (null != renderer.FileTypeName.match(re)) {
return renderer;
}
rendererEnum.moveNext();
}
return null;
}


// finds a selected track





function FindSelectedTrack() : Track {
var trackEnum = new Enumerator(Vegas.Project.Tracks);

while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());
if (track.Selected) {
return track;
}

trackEnum.moveNext();
}
return null;
}