-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export Regions as Subtitles.cs
161 lines (143 loc) · 5.99 KB
/
Export Regions as Subtitles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* 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 ScriptPortal.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
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]) / 10.0);
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 ExportRegionsToSUB(String exportFile) {
StreamWriter streamWriter = null;
try {
streamWriter = CreateStreamWriter(exportFile, System.Text.Encoding.Unicode);
//streamWriter.WriteLine("Start\tEnd\tLength\tName");
int iSubtitle = 0;
foreach (Region region in myVegas.Project.Regions) {
StringBuilder tsv = 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);
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;
}
}
}