-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export Closed Captioning for DVD Architect.cs
235 lines (204 loc) · 8.22 KB
/
Export Closed Captioning for DVD Architect.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
This script will export project 608CC1 and 608CC3 command markers to
* two .sub files which can be used Closed Caption with YouTube video.
Last Modified: April 2010.
*/
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using ScriptPortal.Vegas;
public class EntryPoint
{
public void FromVegas(Vegas vegas)
{
Project proj = vegas.Project;
String sProjName;
String sProjFile = vegas.Project.FilePath;
if (String.IsNullOrEmpty(sProjFile))
{
sProjName = "Untitled";
}
else
{
sProjName = Path.GetFileNameWithoutExtension(sProjFile);
}
String sExportFile = ShowSaveFileDialog("DVD Architect Subtitles (*.sub)|*.sub", "Save Closed Caption as DVD Architect Subtitles", sProjName);
if (null != sExportFile)
{
StreamWriter streamWriter = null;
String sExt = Path.GetExtension(sExportFile);
if ( ((null != sExt) && (sExt.ToUpper() != ".SUB")) || null == sExt )
{
sExportFile = Path.Combine(Path.GetDirectoryName(sExportFile),Path.GetFileNameWithoutExtension(sExportFile) + ".sub");
}
try
{
FileStream filestream = null;
List<CommandMarker> cc1List = new List<CommandMarker>();
List<CommandMarker> cc3List = new List<CommandMarker>();
bool bCC1HasText = false;
bool bCC3HasText = false;
foreach (CommandMarker commandmarker in proj.CommandMarkers)
{
if (MarkerCommandType.CEA608CC1.ToString() == commandmarker.CommandType.ToString())
{
cc1List.Add(commandmarker);
if (!bCC1HasText && 0 != (commandmarker.CommandParameter.ToString()).Length)
{
bCC1HasText = true;
}
}
if (MarkerCommandType.CEA608CC3.ToString() == commandmarker.CommandType.ToString())
{
cc3List.Add(commandmarker);
if (!bCC3HasText && 0 != (commandmarker.CommandParameter.ToString()).Length)
{
bCC3HasText = true;
}
}
}
if( bCC1HasText )
{
filestream = new FileStream(sExportFile, FileMode.Create, FileAccess.Write, FileShare.Read);
streamWriter = new StreamWriter(filestream, System.Text.Encoding.Unicode);
int n = 0;
int iSubtitle = 1;
for (n = 0; n < cc1List.Count; n++)
{
if (0 != (cc1List[n].CommandParameter.ToString()).Length)
{
StringBuilder sOut = new StringBuilder();
sOut.AppendFormat("{0:d4}", iSubtitle);
sOut.Append(" ");
sOut.Append(TimecodeToSUBString(cc1List[n].Position, false));
sOut.Append(" ");
if(cc1List.Count - 1 != n)
{
sOut.Append(TimecodeToSUBString(cc1List[n + 1].Position, false));
}
else
{
sOut.Append(TimecodeToSUBString(cc1List[n].Position, true));
}
sOut.Append(" ");
string sText = cc1List[n].CommandParameter;
sText = sText.Replace("[br]", " ");
sOut.Append(sText);
streamWriter.WriteLine(sOut.ToString());
streamWriter.WriteLine();
iSubtitle++;
}
}
}
if (bCC3HasText)
{
if (bCC1HasText)
{
if (null != streamWriter)
{
streamWriter.Close();
}
sExportFile = Path.Combine(Path.GetDirectoryName(sExportFile), Path.GetFileNameWithoutExtension(sExportFile) + "3.sub");
}
filestream = new FileStream(sExportFile, FileMode.Create, FileAccess.Write, FileShare.Read);
streamWriter = new StreamWriter(filestream, System.Text.Encoding.Unicode);
int n = 0;
int iSubtitle = 1;
for (n = 0; n < cc3List.Count; n++)
{
if (0 != (cc3List[n].CommandParameter.ToString()).Length)
{
StringBuilder sOut = new StringBuilder();
sOut.AppendFormat("{0:d4}", iSubtitle);
sOut.Append(" ");
sOut.Append(TimecodeToSUBString(cc3List[n].Position, false));
sOut.Append(" ");
if (cc3List.Count - 1 != n)
{
sOut.Append(TimecodeToSUBString(cc3List[n + 1].Position, false));
}
else
{
sOut.Append(TimecodeToSUBString(cc3List[n].Position, true));
}
sOut.Append(" ");
string sText = cc3List[n].CommandParameter;
sText = sText.Replace("[br]", " ");
sOut.Append(sText);
streamWriter.WriteLine(sOut.ToString());
streamWriter.WriteLine();
iSubtitle++;
}
}
}
}
finally
{
if (null != streamWriter)
{
streamWriter.Close();
}
}
}
}
String TimecodeToSUBString(Timecode timecode, bool bLast)
{
Int64 time = Convert.ToInt64(timecode.ToMilliseconds());
Int64 hours = time / 3600000;
Int64 mins = (time - hours * 3600000) / 60000;
Int64 secs = (time - hours * 3600000 - mins * 60000) / 1000;
Int64 ssecs = time - hours * 3600000 - mins * 60000 - secs * 1000;
if (bLast)
{
secs += 2;
if (secs > 59)
{
mins += 1;
secs -= 60;
}
if (mins > 59)
{
hours += 1;
mins -= 60;
}
}
return String.Format("{0:d2}:{1:d2}:{2:d2}:{3:d2}", hours, mins, secs, ssecs/10);
}
String ShowSaveFileDialog(String sFilter, String sTitle, String sDefaultFileName)
{
SaveFileDialog fileDialog = new SaveFileDialog();
if(null == sFilter)
{
sFilter = "All Files (*.*)|*.*";
}
fileDialog.Filter = sFilter;
if(null != sTitle)
{
fileDialog.Title = sTitle;
}
fileDialog.CheckPathExists = true;
fileDialog.AddExtension = true;
if(null != sDefaultFileName)
{
String sDir = Path.GetDirectoryName(sDefaultFileName);
if( Directory.Exists(sDir) )
{
fileDialog.InitialDirectory = sDir;
}
fileDialog.DefaultExt = Path.GetExtension(sDefaultFileName);
fileDialog.FileName = Path.GetFileName(sDefaultFileName);
}
if ( System.Windows.Forms.DialogResult.OK == fileDialog.ShowDialog() )
{
return Path.GetFullPath(fileDialog.FileName);
}
else
{
return null;
}
}
}