Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * You can use this script to export Vegas regions in the .sub format
- * for use in DVDA as subtitles. This script can aslo export regions
- * as tab separated text.
- *
- * To use this script:
- *
- * 1) Create named Vegas regions.
- * 2) Confirm no overlapped regions.
- * 3) Vegas>tools>scripting>run script>ExportRegionsAsSubtitles.js; save
- * 4) Import into DVDA using the Import Subtitles button in the DVDA timeline.
- *
- * Revision Date: Juse 22, 2006.
- **/
- using System;
- using System.IO;
- using System.Text;
- using System.Collections;
- using System.Windows.Forms;
- using System.Globalization;
- using Sony.Vegas;
- public class EntryPoint
- {
- Vegas myVegas;
- public void FromVegas(Vegas vegas)
- {
- myVegas = vegas;
- string projName;
- string projFile = myVegas.Project.FilePath;
- if (string.IsNullOrEmpty(projFile))
- {
- projName = "Untitled";
- }
- else
- {
- projName = Path.GetFileNameWithoutExtension(projFile);
- }
- string exportFile = ShowSaveFileDialog("DVD Architect Subtitle Script (*.sub)|*.sub|" +
- "Vegas Region List (*.txt)|*.txt",
- "Save Regions as Subtitles", projName + "-Regions");
- if (null != exportFile)
- {
- string ext = Path.GetExtension(exportFile);
- // Works even if prev lastIndexOf fails or if the ext
- // contains but not equal to "sub"
- if ((null != ext) && (ext.ToUpper() == ".SUB"))
- {
- ExportRegionsToSub(exportFile);
- }
- else
- {
- ExportRegionsToTXT(exportFile);
- }
- }
- }
- string TimeToString(Timecode time)
- {
- string[] decimalSeparators = new string[] { CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator };
- Int64 nanosPerCentisecond = 100000;
- // first round the time to the nearest centisecond
- long nanos = time.Nanos;
- double tmp = ((double)nanos / (double)nanosPerCentisecond) + 0.5;
- nanos = (long)tmp * nanosPerCentisecond;
- time = Timecode.FromNanos(nanos);
- // {"hh:mm:ss", "ddd"}
- string[] rgTime = time.ToString(RulerFormat.Time).Split(decimalSeparators, StringSplitOptions.None);
- StringBuilder sbRes = new StringBuilder();
- sbRes.Append(rgTime[0]);
- sbRes.Append(':');
- int iCentiseconds = (int)Math.Round(double.Parse(rgTime[1]) / 10.0);
- sbRes.Append(((iCentiseconds / 10) >> 0) % 10);
- sbRes.Append(((iCentiseconds / 1) >> 0) % 10);
- return sbRes.ToString();
- }
- void ExportRegionsToSub(string exportFile)
- {
- try
- {
- using (var stream = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.None))
- using (var streamWriter = new StreamWriter(stream, Encoding.Unicode))
- {
- int dialogueLines = 0;
- foreach (var t in myVegas.Project.Tracks)
- {
- if (t.IsAudio())
- {
- foreach (var e in t.Events)
- {
- var eventStart = e.Start;
- var eventEnd = e.End;
- var takeStart = e.ActiveTake.Offset;
- var takeEnd = e.ActiveTake.Offset + e.Length;
- foreach (var sourceRegion in e.ActiveTake.Media.Regions)
- {
- if (
- (takeStart <= sourceRegion.Position && sourceRegion.Position < takeEnd) ||
- (takeStart < sourceRegion.End && sourceRegion.End <= takeEnd))
- {
- Timecode offsetDifference = sourceRegion.Position - takeStart;
- Timecode position = eventStart + offsetDifference;
- Timecode end = position + sourceRegion.Length;
- string label = sourceRegion.Label;
- string line = string.Empty;
- line += dialogueLines.ToString().PadLeft(4, '0');
- line += '\t';
- line += TimeToString(position);
- line += '\t';
- line += TimeToString(end);
- line += '\t';
- line += label;
- streamWriter.WriteLine(line);
- streamWriter.WriteLine();
- dialogueLines++;
- }
- }
- }
- }
- }
- foreach (Region region in myVegas.Project.Regions)
- {
- streamWriter.WriteLine(RegionToSubLine(dialogueLines, region));
- streamWriter.WriteLine();
- dialogueLines++;
- }
- }
- }
- finally { }
- }
- string RegionToSubLine(int lineNumber, Region region)
- {
- string line = string.Empty;
- line += lineNumber.ToString().PadLeft(4, '0');
- line += '\t';
- line += TimeToString(region.Position);
- line += '\t';
- line += TimeToString(region.End);
- line += '\t';
- line += region.Label;
- return line;
- }
- string RegionToTxtLine(Region region)
- {
- string line = string.Empty;
- line += region.Position.ToString(RulerFormat.Time);
- line += '\t';
- line += region.End.ToString(RulerFormat.Time);
- line += '\t';
- line += region.Length.ToString(RulerFormat.Time);
- line += '\t';
- line += region.Label;
- return line;
- }
- void ExportRegionsToTXT(string exportFile)
- {
- try
- {
- using (var stream = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.None))
- using (var streamWriter = new StreamWriter(stream, Encoding.Unicode))
- {
- streamWriter.WriteLine("Start\tEnd\tLength\tName");
- foreach (Region region in myVegas.Project.Regions)
- {
- streamWriter.WriteLine(RegionToTxtLine(region));
- }
- }
- }
- finally { }
- }
- // an example filter: "PNG File (*.png)|*.png|JPEG File (*.jpg)|*.jpg"
- string ShowSaveFileDialog(string filter, string title, string defaultFilename)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- if (null == filter)
- {
- filter = "All Files (*.*)|*.*";
- }
- saveFileDialog.Filter = filter;
- if (null != title)
- {
- saveFileDialog.Title = title;
- }
- saveFileDialog.CheckPathExists = true;
- saveFileDialog.AddExtension = true;
- if (null != defaultFilename)
- {
- string initialDir = Path.GetDirectoryName(defaultFilename);
- if (Directory.Exists(initialDir))
- {
- saveFileDialog.InitialDirectory = initialDir;
- }
- saveFileDialog.DefaultExt = Path.GetExtension(defaultFilename);
- saveFileDialog.FileName = Path.GetFileName(defaultFilename);
- }
- if (DialogResult.OK == saveFileDialog.ShowDialog())
- {
- return Path.GetFullPath(saveFileDialog.FileName);
- }
- else
- {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment