http://www.pixelsplasher.com/_downloads/scripts/Sony.Vegas.export.regions.as.srt.subtitles.for.YouTube.script/Pixelsplasher.com[link] has kindly modified Sony's bundled "Export Regions as Subtitles" script to export as a SubRip .srt subtitle file which can be used for YouTube closed captioning and also to import to DVD authoring packages such as TMGEnc Authoring Works.
There were a couple of bugs in the modified script which I have fixed. Firstly the subtitle count was incorrect from 100-109, 200-209 etc.. Secondly the millisecond count was incorrect.
I have now got it working but the final digit always reads zero. I had a go at getting it to be accurate down the final digit (milliseconds) but my efforts failed.
That much accuracy is not very important but for the sake of completeness, if any of you bright sparks could take a look at it I suspect it would be fairly straightforward to get that working perfectly.
My code is below. Save as a .cs file. The lines that need work are 62-86.
Thanks!
There were a couple of bugs in the modified script which I have fixed. Firstly the subtitle count was incorrect from 100-109, 200-209 etc.. Secondly the millisecond count was incorrect.
I have now got it working but the final digit always reads zero. I had a go at getting it to be accurate down the final digit (milliseconds) but my efforts failed.
That much accuracy is not very important but for the sake of completeness, if any of you bright sparks could take a look at it I suspect it would be fairly straightforward to get that working perfectly.
My code is below. Save as a .cs file. The lines that need work are 62-86.
Thanks!
/**
* You can use this script to export Vegas regions in the standard .srt format
* for use in YouTube and various DVD Authoring programs.
*
* To use this script:
*
* 1) Create named Vegas regions.
* 2) Confirm no overlapped regions.
* 3) Vegas>tools>scripting>run script>ExportRegionsAsSRTSubtitles.cs; save
* 4) Import into YouTube or your DVD Authoring program.
*
* Revision Date: February 25, 2009.
* Added .srt format support by Pixelsplasher (www.pixelsplasher.com)
* for updates, visit http://www.pixelsplasher.com/_downloads/scripts/Sony.Vegas.export.regions.as.srt.subtitles.for.YouTube.script/
*
* Revision Date: October 18, 2009 by Nick Hope nick@bubblevision.com
* Fixed incorrect numbering for subtitles 101-109, 201-209 etc.
* Fixed incorrect millisecond count
* Removed ability to save to DVD Architect .sub format (to make it easier to read and edit the script). Sony's bundled script can do that anyway.
* Note: Script may benefit from accuracy in the final digit (milliseconds) which currently always reads zero. My efforts failed.
**/
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("SubRip (*.srt)|*.srt|" +
"Vegas Region List (*.txt)|*.txt",
"Save Regions as Subtitles", projName);
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() == ".SRT"))
ExportRegionsToSRT(exportFile);
else
ExportRegionsToTXT(exportFile);
}
}
String TimeToStringSRT(Timecode time) {
String[] decimalSeparators = new String[] {CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator};
Int64 nanosPerCentisecond = 100000;
// first round the time to the nearest centisecond
Int64 nanos = time.Nanos;
Double tmp = ((Double) nanos / (Double) nanosPerCentisecond) + 0.5;
nanos = (Int64) 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]));
sbRes.Append(((iCentiseconds / 100) >> 0 ) % 10);
sbRes.Append(((iCentiseconds / 10) >> 0 ) % 10);
sbRes.Append(((iCentiseconds / 1) >> 0 ) % 10);
return sbRes.ToString();
}
StreamWriter CreateStreamWriter(String fileName, Encoding encoding) {
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs, encoding);
return sw;
}
void ExportRegionsToSRT(String exportFile) {
StreamWriter streamWriter = null;
try {
streamWriter = CreateStreamWriter(exportFile, System.Text.Encoding.Unicode);
//streamWriter.WriteLine("Start\tEnd\tLength\tName");
int iSubtitle = 1;
foreach (Region region in myVegas.Project.Regions) {
StringBuilder tsv = new StringBuilder();
tsv.Append( iSubtitle );
tsv.Append('\n');
tsv.Append( TimeToStringSRT( region.Position ));
tsv.Append(' ');
tsv.Append('-');
tsv.Append('-');
tsv.Append('>');
tsv.Append(' ');
tsv.Append( TimeToStringSRT( region.End ));
tsv.Append('\n');
tsv.Append(region.Label);
streamWriter.WriteLine(tsv.ToString());
streamWriter.WriteLine();
iSubtitle++;
}
} finally {
if (null != streamWriter)
streamWriter.Close();
}
}
void ExportRegionsToTXT(String exportFile) {
StreamWriter streamWriter = null;
try {
streamWriter = CreateStreamWriter(exportFile, System.Text.Encoding.Unicode);
streamWriter.WriteLine("Start\tEnd\tLength\tName");
foreach (Region region in myVegas.Project.Regions) {
StringBuilder tsv = new StringBuilder();
tsv.Append( region.Position.ToString( RulerFormat.Time ));
tsv.Append('\t');
tsv.Append( region.End.ToString( RulerFormat.Time ));
tsv.Append('\t');
tsv.Append( region.Length.ToString( RulerFormat.Time ));
tsv.Append('\t');
tsv.Append(region.Label);
streamWriter.WriteLine(tsv.ToString());
}
} finally {
if (null != streamWriter)
streamWriter.Close();
}
}
// 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 (System.Windows.Forms.DialogResult.OK == saveFileDialog.ShowDialog()) {
return Path.GetFullPath(saveFileDialog.FileName);
} else {
return null;
}
}
}