Regions-as-subtitles script

jeremyk wrote on 8/11/2004, 8:38 AM
A while back I posted this message about problems getting regions containing special characters correctly rendered as subtitles.

Nobody responded to my post, so I submitted a problem report to Sony. Finally heard back from them this morning.

Sony says:

Customer Service Response (Brian) - 08/11/2004 07:49 AM

Actually, I think the problem is that the script DOES output in UTF8 format and DVD Architect expects them in ASCII (or, shall we say, "extended ASCII").

And then there's the drop-frame vs. non-drop-frame timecode problem that results in subtitles winding up in the wrong place. Since Sony evidently doesn't actually support the scripts they ship with their products, can anyone in this forum help me fix this thing?

Hopefully,

Jeremy

Comments

jeremyk wrote on 8/12/2004, 4:06 PM
Obviously no one reads my posts, so I'll just launch this into the empty air.

Sony sent me a script that outputs the .sub file in Unicode format. That fixes the special character problem.

With my son's help, I figured out how to change the script to export multi-line subtitles to the .sub file.

In the fullness of time, I'll attack the timecode settings problem in the script, but I'm too tired right now.

If anyone wants a copy of the script, they're welcome to bark at the moon ^H^H^H^H^H^H^H^H^H^H^H post a message in the Forum.

Jeremy
jetdv wrote on 8/12/2004, 6:13 PM
Well... I read your message(s) but agree that I did not reply. Also wouldn't mind seeing the revised script. Might even be able to help you wih some of your problems. You can find me here
alexz wrote on 8/13/2004, 3:43 AM
No barking, I'd love to have a copy of the script - and at the same time soothe your
injured pride by writing a reply:)

Would be great to see how it works, and hopefully improve on it (I posted a message about getting subtitles axported as FAB text (format: tim in, time out, carriage return, text), but I still haven't had any takers.

I'll keep watching this space. Thanks, Alexz
jeremyk wrote on 8/13/2004, 8:59 AM
OK, fellas, thanks for replying.

Here're the whole script. The changes are all in the "ExportRegionsToSUB" function. I bet there's a way to (1) remember what the timecode format it, (2) change it to non-drop before exporting the region labels, and (3) change it back afterwards. I think that's all that remains to be done here.

Thanks,
Jeremy

----------------

/**
* 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: Feb. 25, 2004.
*
* 12 August 2004 - changes by Sony to output .sub files in Unicode format.
*
* 12 August 2004 - J. Knight - Change instances of the string "\n" in region labels
* to new lines to allow exporting multiline subtitles.
**/
import System.IO;
import System.Text;
import System.Collections;
import System.Windows.Forms;
import System.Globalization;
import Sony.Vegas;

function TimeToString(time) : String
{
var decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

var rgTime = time.ToString( RulerFormat.Time ).split(decimalSeparator); // {"hh:mm:ss", "ddd"}
var sbRes : StringBuilder = new StringBuilder();

sbRes.Append( rgTime[0]);
sbRes.Append( ':' );

var iCentiseconds = Math.round( rgTime[1]/ 10);

sbRes.Append((( iCentiseconds / 10 ) >> 0 )% 10 );
sbRes.Append((( iCentiseconds / 1 ) >> 0 )% 10 );

return sbRes;
}

function CreateStreamWriter(fileName : String, encoding : Encoding) : StreamWriter {
var fs : FileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
var sw : StreamWriter = new StreamWriter(fs, encoding);
return sw;
}

function ExportRegionsToSUB(exportFile : String) {
var streamWriter : StreamWriter = null;
try {
streamWriter = CreateStreamWriter(exportFile, System.Text.Encoding.Unicode);
//streamWriter.WriteLine("Start\tEnd\tLength\tName");
var enumRegions : IEnumerator = Vegas.Project.Regions.GetEnumerator();
var iSubtitle = 0;
var newline = String.fromCharCode(13,10);
while (enumRegions.MoveNext())
{
var region : Region = enumRegions.Current;
var tsv : StringBuilder = new StringBuilder();

tsv.Append((( iSubtitle / 1000 )>> 0) % 10 );
tsv.Append((( iSubtitle / 100 )>> 0) % 10 );
tsv.Append((( iSubtitle / 10 )>> 0) % 10 );
tsv.Append((( iSubtitle / 1 )>> 0) % 10 );
tsv.Append('\t');

tsv.Append( TimeToString( region.Position ));
tsv.Append('\t');
tsv.Append( TimeToString( region.End ));
tsv.Append('\t');
tsv.Append(region.Label.replace(/\\n/g,newline)); // new line for "\n"
streamWriter.WriteLine(tsv.ToString());
streamWriter.WriteLine();

iSubtitle++;
}
} finally {
if (null != streamWriter)
streamWriter.Close();
}
}

function ExportRegionsToTXT(exportFile : String) {
var streamWriter : StreamWriter = null;
try {
streamWriter = CreateStreamWriter(exportFile, System.Text.Encoding.Unicode);
streamWriter.WriteLine("Start\tEnd\tLength\tName");
var enumRegions : IEnumerator = Vegas.Project.Regions.GetEnumerator();
while (enumRegions.MoveNext())
{
var region : Region = enumRegions.Current;
var tsv : StringBuilder = 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"
function ShowSaveFileDialog(filter, title, defaultFilename) : String {
var 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) {
var 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;
}
}

var projName;

var projFile = Vegas.Project.FilePath;
if ((null == projFile) || (0 == projFile.length))
{
projName = "Untitled";
}
else
{
projName = Path.GetFileNameWithoutExtension(projFile);
}

var strExportFile : String = ShowSaveFileDialog
( "DVD Architect Subtitle Script (*.sub)|*.sub|" +
"Vegas Region List (*.txt)|*.txt",
"Save Regions as Subtitles", projName + "-Regions"
);

if (null != strExportFile)
{
var iExt = strExportFile.lastIndexOf('.');
if( strExportFile.substr( iExt+1,3+1 ).toUpperCase() == "SUB" ) // Works even if prev
// lastIndexOf fails or if the ext contains but not equal to "sub"
ExportRegionsToSUB(strExportFile);
else
ExportRegionsToTXT(strExportFile);
}
jetdv wrote on 8/13/2004, 10:14 AM
PS: After posting, I see that all the whitespace has been taken out of the script by the forum software. Is there a way to get the script in there with the formatting intact?

Yes there is. Put
 before the code and 
after the code (changing [ with < and ] with > when you actually type it.)
jeremyk wrote on 8/13/2004, 10:22 AM
Thanks! The window got a bit wide, but it's much more readable now.

j