Guest User

Untitled

a guest
Jan 28th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. /**
  2. * You can use this script to export Vegas regions in the .sub format
  3. * for use in DVDA as subtitles. This script can aslo export regions
  4. * as tab separated text.
  5. *
  6. * To use this script:
  7. *
  8. * 1) Create named Vegas regions.
  9. * 2) Confirm no overlapped regions.
  10. * 3) Vegas>tools>scripting>run script>ExportRegionsAsSubtitles.js; save
  11. * 4) Import into DVDA using the Import Subtitles button in the DVDA timeline.
  12. *
  13. * Revision Date: Juse 22, 2006.
  14. **/
  15. using System;
  16. using System.IO;
  17. using System.Text;
  18. using System.Collections;
  19. using System.Windows.Forms;
  20. using System.Globalization;
  21. using Sony.Vegas;
  22.  
  23. public class EntryPoint
  24. {
  25. Vegas myVegas;
  26.  
  27. public void FromVegas(Vegas vegas)
  28. {
  29. myVegas = vegas;
  30.  
  31. string projName;
  32.  
  33. string projFile = myVegas.Project.FilePath;
  34. if (string.IsNullOrEmpty(projFile))
  35. {
  36. projName = "Untitled";
  37. }
  38. else
  39. {
  40. projName = Path.GetFileNameWithoutExtension(projFile);
  41. }
  42.  
  43. string exportFile = ShowSaveFileDialog("DVD Architect Subtitle Script (*.sub)|*.sub|" +
  44. "Vegas Region List (*.txt)|*.txt",
  45. "Save Regions as Subtitles", projName + "-Regions");
  46.  
  47. if (null != exportFile)
  48. {
  49. string ext = Path.GetExtension(exportFile);
  50. // Works even if prev lastIndexOf fails or if the ext
  51. // contains but not equal to "sub"
  52. if ((null != ext) && (ext.ToUpper() == ".SUB"))
  53. {
  54. ExportRegionsToSub(exportFile);
  55. }
  56. else
  57. {
  58. ExportRegionsToTXT(exportFile);
  59. }
  60. }
  61. }
  62.  
  63.  
  64. string TimeToString(Timecode time)
  65. {
  66. string[] decimalSeparators = new string[] { CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator };
  67. Int64 nanosPerCentisecond = 100000;
  68.  
  69. // first round the time to the nearest centisecond
  70. long nanos = time.Nanos;
  71. double tmp = ((double)nanos / (double)nanosPerCentisecond) + 0.5;
  72. nanos = (long)tmp * nanosPerCentisecond;
  73. time = Timecode.FromNanos(nanos);
  74.  
  75. // {"hh:mm:ss", "ddd"}
  76. string[] rgTime = time.ToString(RulerFormat.Time).Split(decimalSeparators, StringSplitOptions.None);
  77. StringBuilder sbRes = new StringBuilder();
  78.  
  79. sbRes.Append(rgTime[0]);
  80. sbRes.Append(':');
  81.  
  82. int iCentiseconds = (int)Math.Round(double.Parse(rgTime[1]) / 10.0);
  83.  
  84. sbRes.Append(((iCentiseconds / 10) >> 0) % 10);
  85. sbRes.Append(((iCentiseconds / 1) >> 0) % 10);
  86.  
  87. return sbRes.ToString();
  88. }
  89.  
  90. void ExportRegionsToSub(string exportFile)
  91. {
  92. try
  93. {
  94. using (var stream = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.None))
  95. using (var streamWriter = new StreamWriter(stream, Encoding.Unicode))
  96. {
  97. int dialogueLines = 0;
  98. foreach (var t in myVegas.Project.Tracks)
  99. {
  100. if (t.IsAudio())
  101. {
  102. foreach (var e in t.Events)
  103. {
  104. var eventStart = e.Start;
  105. var eventEnd = e.End;
  106. var takeStart = e.ActiveTake.Offset;
  107. var takeEnd = e.ActiveTake.Offset + e.Length;
  108.  
  109. foreach (var sourceRegion in e.ActiveTake.Media.Regions)
  110. {
  111. if (
  112. (takeStart <= sourceRegion.Position && sourceRegion.Position < takeEnd) ||
  113. (takeStart < sourceRegion.End && sourceRegion.End <= takeEnd))
  114. {
  115. Timecode offsetDifference = sourceRegion.Position - takeStart;
  116. Timecode position = eventStart + offsetDifference;
  117. Timecode end = position + sourceRegion.Length;
  118. string label = sourceRegion.Label;
  119.  
  120. string line = string.Empty;
  121. line += dialogueLines.ToString().PadLeft(4, '0');
  122. line += '\t';
  123. line += TimeToString(position);
  124. line += '\t';
  125. line += TimeToString(end);
  126. line += '\t';
  127. line += label;
  128.  
  129. streamWriter.WriteLine(line);
  130. streamWriter.WriteLine();
  131. dialogueLines++;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. foreach (Region region in myVegas.Project.Regions)
  138. {
  139. streamWriter.WriteLine(RegionToSubLine(dialogueLines, region));
  140. streamWriter.WriteLine();
  141. dialogueLines++;
  142. }
  143. }
  144. }
  145. finally { }
  146. }
  147. string RegionToSubLine(int lineNumber, Region region)
  148. {
  149. string line = string.Empty;
  150. line += lineNumber.ToString().PadLeft(4, '0');
  151. line += '\t';
  152. line += TimeToString(region.Position);
  153. line += '\t';
  154. line += TimeToString(region.End);
  155. line += '\t';
  156. line += region.Label;
  157.  
  158. return line;
  159. }
  160.  
  161. string RegionToTxtLine(Region region)
  162. {
  163. string line = string.Empty;
  164. line += region.Position.ToString(RulerFormat.Time);
  165. line += '\t';
  166. line += region.End.ToString(RulerFormat.Time);
  167. line += '\t';
  168. line += region.Length.ToString(RulerFormat.Time);
  169. line += '\t';
  170. line += region.Label;
  171.  
  172. return line;
  173. }
  174.  
  175. void ExportRegionsToTXT(string exportFile)
  176. {
  177. try
  178. {
  179. using (var stream = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.None))
  180. using (var streamWriter = new StreamWriter(stream, Encoding.Unicode))
  181. {
  182. streamWriter.WriteLine("Start\tEnd\tLength\tName");
  183. foreach (Region region in myVegas.Project.Regions)
  184. {
  185. streamWriter.WriteLine(RegionToTxtLine(region));
  186. }
  187. }
  188.  
  189. }
  190. finally { }
  191. }
  192.  
  193. // an example filter: "PNG File (*.png)|*.png|JPEG File (*.jpg)|*.jpg"
  194. string ShowSaveFileDialog(string filter, string title, string defaultFilename)
  195. {
  196. SaveFileDialog saveFileDialog = new SaveFileDialog();
  197. if (null == filter)
  198. {
  199. filter = "All Files (*.*)|*.*";
  200. }
  201. saveFileDialog.Filter = filter;
  202. if (null != title)
  203. {
  204. saveFileDialog.Title = title;
  205. }
  206. saveFileDialog.CheckPathExists = true;
  207. saveFileDialog.AddExtension = true;
  208. if (null != defaultFilename)
  209. {
  210. string initialDir = Path.GetDirectoryName(defaultFilename);
  211. if (Directory.Exists(initialDir))
  212. {
  213. saveFileDialog.InitialDirectory = initialDir;
  214. }
  215. saveFileDialog.DefaultExt = Path.GetExtension(defaultFilename);
  216. saveFileDialog.FileName = Path.GetFileName(defaultFilename);
  217. }
  218. if (DialogResult.OK == saveFileDialog.ShowDialog())
  219. {
  220. return Path.GetFullPath(saveFileDialog.FileName);
  221. }
  222. else
  223. {
  224. return null;
  225. }
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment