Comments

SonyEPM wrote on 1/25/2003, 1:35 PM
Try the render image sequence script, but choose to output 1 frame- I think it does everything else you are asking for, or could easily be modified to do so.
FadeToBlack wrote on 1/25/2003, 3:31 PM
FadeToBlack wrote on 1/25/2003, 6:02 PM
aboukirev wrote on 1/25/2003, 7:52 PM
Here's the script that does one snapshot at current cursor. It also stores (and retrieves) the last used snapshot file name in Windows registry. I'm still working on autoincrement. Also, when I use Vegas.Project.MediaPool.Add(), it requires two parameters: Object key, Object value. I use (new Media(imageFileName)) for value, but anything for key gives me "This method is not supported." exception. The file is being added to Media Pool though.
Oh, and it does not require times in dialog, just file name.

// This script makes a snapshot from the timeline and adds it to mediapool

import System.Windows.Forms;
import SonicFoundry.Vegas.Script;
import System.IO;
import Microsoft.Win32;

// The first thing this script does is save off the preview & project
// settings that will be set later. This is so they can be restored
// after the script is complete.
var origPreviewRenderQuality = Vegas.Project.Preview.RenderQuality;
var origPreviewFillSize = Vegas.Project.Preview.FullSize;
var origFieldOrder = Vegas.Project.Video.FieldOrder;
var origProjectDeinterlaceMethod = Vegas.Project.Video.DeinterlaceMethod;
var currentTime = Vegas.Cursor;


// Last snapshot file name is stored in Windows registry.
var regKey = Registry.CurrentUser.CreateSubKey("Software\\Sonic Foundry\\Scripts");

var imageFileName = regKey.GetValue("Snapshot");
if (imageFileName == null) { // Running for the first time.
// The current project path seems to fit.
imageFileName = Path.GetDirectoryName(Vegas.Project.FilePath);
imageFileName += Path.DirectorySeparatorChar;
imageFileName += "Image001.png";
} else {
// Increment number.
}

// Set the preview quality and size.
Vegas.Project.Preview.RenderQuality = VideoRenderQuality.Best;
Vegas.Project.Preview.FullSize = true;

// Set the field order and deinterlace method
Vegas.Project.Video.FieldOrder = VideoFieldOrder.ProgressiveScan;
Vegas.Project.Video.DeinterlaceMethod = VideoDeinterlaceMethod.InterpolateFields;

// Show the script's dialog box.
var dialog = new SnapshotDialog(imageFileName);

// if the OK button was pressed...
if (dialog.ShowDialog() == DialogResult.OK) {
// Get the basis for output image file names
imageFileName = Path.GetFullPath(dialog.fileNameBox.Text);
}

// Get the output image file name extension and corresponding
// file format. ImageFileFormat.None indicates that the
// current prefs setting will be used but that may not
// correspond to the specified file extension.
var imageFileNameExt = Path.GetExtension(imageFileName);
var imageFormat = ImageFileFormat.None;
if (0 == String.Compare(imageFileNameExt, ".jpg", true)) {
imageFormat = ImageFileFormat.JPEG;
} else if (0 == String.Compare(imageFileNameExt, ".png", true)) {
imageFormat = ImageFileFormat.PNG;
}

// save a snapshot. The SaveSnapshot method returns a
// member of the RenderStatus enumeration. If the user
// hits the escape key or quits the app, exit the loop.
if (Vegas.SaveSnapshot(imageFileName, imageFormat, currentTime) == RenderStatus.Complete) {
regKey.SetValue("Snapshot", imageFileName);
Vegas.Project.MediaPool.Add("What should be here?", new Media(imageFileName));
}

// restore the project and preview settings
Vegas.Project.Preview.RenderQuality = origPreviewRenderQuality;
Vegas.Project.Preview.FullSize = origPreviewFillSize;
Vegas.Project.Video.FieldOrder = origFieldOrder;
Vegas.Project.Video.DeinterlaceMethod = origProjectDeinterlaceMethod;
Vegas.Cursor = currentTime;

// Button subclass that shows a save file dialog when clicked
class BrowseButton extends Button {
var myResultBox = null;

function BrowseButton(resultBox) {
myResultBox = resultBox;
}

protected override function OnClick(e: System.EventArgs) {
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "PNG File (*.png)|*.png|JPEG File (*.jpg)|*.jpg";
saveFileDialog.CheckPathExists = true;
saveFileDialog.AddExtension = true;
if (null != myResultBox) {
var filename = myResultBox.Text;
var initialDir = Path.GetDirectoryName(filename);
if (Directory.Exists(initialDir)) {
saveFileDialog.InitialDirectory = initialDir;
}
saveFileDialog.DefaultExt = Path.GetExtension(filename);
saveFileDialog.FileName = Path.GetFileNameWithoutExtension(filename);
}
if (System.Windows.Forms.DialogResult.OK == saveFileDialog.ShowDialog()) {
if (null != myResultBox) {
myResultBox.Text = Path.GetFullPath(saveFileDialog.FileName);
}
}
}
}

// Form subclass that is the dialog box for this script
class SnapshotDialog extends Form {
var browseButton;
var fileNameBox;

function SnapshotDialog(baseFileName) {
this.Text = "Render Image Sequence";
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Width = 480;

var buttonWidth = 80;
var buttonHeight = 24;

fileNameBox = addTextControl("Base File Name", 6, 380, 10, baseFileName);
browseButton = new BrowseButton(fileNameBox);
browseButton.Left = fileNameBox.Right + 4;
browseButton.Top = fileNameBox.Top - 2;
browseButton.Width = buttonWidth;
browseButton.Height = buttonHeight;
browseButton.Text = "Browse...";
Controls.Add(browseButton);

var buttonTop = fileNameBox.Bottom + 16;
var okButton = new Button();
okButton.Text = "OK";
okButton.Left = this.Width - (2*(buttonWidth+10));
okButton.Top = buttonTop;
okButton.Width = buttonWidth;
okButton.Height = buttonHeight;
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
AcceptButton = okButton;
Controls.Add(okButton);

var cancelButton = new Button();
cancelButton.Text = "Cancel";
cancelButton.Left = this.Width - (1*(buttonWidth+10));
cancelButton.Top = buttonTop;
cancelButton.Width = buttonWidth;
cancelButton.Height = buttonHeight;
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
CancelButton = cancelButton;
Controls.Add(cancelButton);

this.Height = okButton.Bottom + 30;

}

function addTextControl(labelName, left, width, top, defaultValue) {
var label = new Label();
label.AutoSize = true;
label.Text = labelName + ":";
label.Left = left;
label.Top = top + 4;
Controls.Add(label);

var textbox = new TextBox();
textbox.Multiline = false;
textbox.Left = label.Right;
textbox.Top = top;
textbox.Width = width - (label.Width);
textbox.Text = defaultValue;
Controls.Add(textbox);

return textbox;
}

}
FadeToBlack wrote on 1/25/2003, 9:44 PM
aboukirev wrote on 1/25/2003, 10:40 PM
That's exactly what I described in my post. That's an error produced by MediaPool.Add method. I'm still fighting it. I'm almost done with autoincrementing number in file name. I also eliminated custom dialog and use standard file save dialog instead (less code). When I'm done, I'll clean it up, add more error handling, etc. and then post it. Perhaps tomorrow morning.
Debugging script that is run from Vegas is a pain.
Just wait till tomorrow morning - it'll be much better and reliable.
Sorry for posting work-in-progress, I thought you were hacking on it too and would just pick up from where I was at the moment...

Alexei
Luxo wrote on 1/25/2003, 11:01 PM
Alexei,

Would it be too much trouble to make your script also copy the frame to the clipboard?
aboukirev wrote on 1/25/2003, 11:36 PM
OK, here we go.

// This script makes a snapshot from the timeline and adds it to mediapool

import System.Windows.Forms;
import SonicFoundry.Vegas.Script;
import System.IO;
import Microsoft.Win32;

// The first thing this script does is save off the preview & project
// settings that will be set later. This is so they can be restored
// after the script is complete.
var origPreviewRenderQuality = Vegas.Project.Preview.RenderQuality;
var origPreviewFillSize = Vegas.Project.Preview.FullSize;
var origFieldOrder = Vegas.Project.Video.FieldOrder;
var origProjectDeinterlaceMethod = Vegas.Project.Video.DeinterlaceMethod;
var currentTime = Vegas.Cursor;


try {
// Last snapshot file name is stored in Windows registry.
var regKey = Registry.CurrentUser.CreateSubKey("Software\\Sonic Foundry\\Scripts");

var imageFileName = regKey.GetValue("Snapshot");
if (imageFileName == null) { // Running for the first time.
// The current project path seems to fit.
imageFileName = Path.GetDirectoryName(Vegas.Project.FilePath);
imageFileName += Path.DirectorySeparatorChar;
imageFileName += "Image001.png";
} else {
// Increment number.
var filename = Path.GetFileNameWithoutExtension(imageFileName);
var i = filename.Length - 1;
while ((i >= 0) && System.Char.IsDigit(filename, i))
i--;
i++;
var j = filename.Length - i;
var imageNdex: int = parseInt(filename.Substring(i, j), 10) + 1;
var nextname = String.Format("{0}{1:d" + j + "}", filename.Substring(0, i), imageNdex);
imageFileName = imageFileName.Replace(filename, nextname);
}

// Set the preview quality and size.
Vegas.Project.Preview.RenderQuality = VideoRenderQuality.Best;
Vegas.Project.Preview.FullSize = true;

// Set the field order and deinterlace method
Vegas.Project.Video.FieldOrder = VideoFieldOrder.ProgressiveScan;
Vegas.Project.Video.DeinterlaceMethod = VideoDeinterlaceMethod.InterpolateFields;

// Show the script's dialog box.
var dialog = new SaveFileDialog();
dialog.Filter = "PNG File (*.png)|*.png|JPEG File (*.jpg)|*.jpg";
dialog.CheckPathExists = true;
dialog.AddExtension = true;
var initialDir = Path.GetDirectoryName(imageFileName);
if (Directory.Exists(initialDir)) {
dialog.InitialDirectory = initialDir;
}
dialog.DefaultExt = Path.GetExtension(imageFileName);
dialog.FileName = Path.GetFileNameWithoutExtension(imageFileName);

// if the OK button was pressed...
if (dialog.ShowDialog() == DialogResult.OK) {
// Get the basis for output image file names
imageFileName = Path.GetFullPath(dialog.FileName);

// Get the output image file name extension and corresponding
// file format. ImageFileFormat.None indicates that the
// current prefs setting will be used but that may not
// correspond to the specified file extension.
var imageFileNameExt = Path.GetExtension(imageFileName);
var imageFormat = ImageFileFormat.None;
if (0 == String.Compare(imageFileNameExt, ".jpg", true)) {
imageFormat = ImageFileFormat.JPEG;
} else if (0 == String.Compare(imageFileNameExt, ".png", true)) {
imageFormat = ImageFileFormat.PNG;
}

// save a snapshot. The SaveSnapshot method returns a
// member of the RenderStatus enumeration. If the user
// hits the escape key or quits the app, exit the loop.
if (Vegas.SaveSnapshot(imageFileName, imageFormat, currentTime) == RenderStatus.Complete) {
regKey.SetValue("Snapshot", imageFileName);
// This is just ridiculous, all you have to do to add media to Media Pool is create a Media object!
var media = new Media(imageFileName);
Vegas.UpdateUI();
}
}
} catch (e) {
MessageBox.Show(e);
}

// restore the project and preview settings
Vegas.Project.Preview.RenderQuality = origPreviewRenderQuality;
Vegas.Project.Preview.FullSize = origPreviewFillSize;
Vegas.Project.Video.FieldOrder = origFieldOrder;
Vegas.Project.Video.DeinterlaceMethod = origProjectDeinterlaceMethod;
Vegas.Cursor = currentTime;
aboukirev wrote on 1/25/2003, 11:55 PM
Luxo,

just add this
Clipboard.SetDataObject(System.Drawing.Image.FromFile(imageFileName), true);

right after
regKey.SetValue("Snapshot", imageFileName);

in the last version of my script.

Alexei
SonyPJM wrote on 1/26/2003, 12:30 PM

I'm glad you figured out how to add media to the media pool. I realize
this is not consistent with how objects are added to other collections
(like tracks and events). I am considering making the media pool more
consistent.
SonyPJM wrote on 1/26/2003, 12:36 PM

Nice! I like to see people making use of the larger .Net framework!

But I may add the option to have Vegas save to the clipboard rather
than making the script write & read a file.

I actually had to go a little out of my way to avoid having snapshots
automatically added to the media pool... behavior which it appears
some of you want... that may too become a option for scripts.
aboukirev wrote on 1/26/2003, 1:19 PM
I'm much better at C# in .NET. But JScript is progressing fine.

Since I got myself into scripting Vegas 4, I might take a shot at creating EDL export. I'll work on CMX version as it's 'standard' and I have some documentation on it. I'm not going to get into particular FX codes and such, just media, cuts, wipes (should be only two types) and dissolves. And no import yet. I'll use Adobe Premiere 6.5 to test/compare EDL I'm creating.

Also, a question. Is there any way to place Media into particular Bin and/or to find out in which Bin it is located currently? I assume we'll have something when Bin functionality is wrapped up.

Alexei
FadeToBlack wrote on 1/26/2003, 2:18 PM
aboukirev wrote on 1/26/2003, 3:01 PM
There are several reasons why it's slow:
- .NET is not the fastest/leanest scripting engine (something like Python used by many 3D packages seems more appropriate)
- add COM (another MS monster) interface that seats between .NET scripting and Vegas
- while making snapshot you are essentially rendering one frame, Vegas is not the fastest NLE when it comes to rendering (I'm not talking about preview)

So, while scripting is a wonderful addition to Vegas, it is only good for long repetitious actions or to do something that is not otherwise possible with regular plugin.

Adding ability to create import/export and general type plugins to Vegas 4 SDK may be what we are looking for. But such SDK will always stay somewhat limited. That's where scripting comes in.

Alexei
SonyPJM wrote on 1/27/2003, 9:01 AM

I still need to add the media bin functionality to the scripting
API... it is one of my "to do" items.


There will certainly be some limitations to what you and import/export
to a CMX EDL.
SonyPJM wrote on 1/27/2003, 9:10 AM

I don't think .NET and COM are major factors in the speed of this
script... especially after the first time you run the script (which is
when the CLR gets loaded and cached). The slowness comes from
rendering at full size, best quality.
FadeToBlack wrote on 1/27/2003, 2:19 PM
rfogg wrote on 1/28/2003, 3:58 PM
where can i find the render image sequence script?
SonyPJM wrote on 1/28/2003, 5:37 PM
Yea, same here... I spent several hours yesterday trying to track down
the problem but to no luck so far. Pretty much all of the time
appears to be spent in the Vegas.SaveSnapshot call. That call works
on multiple threads making it harder to debug, especially for an issue
related to time.