-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass2srt.pl
executable file
·379 lines (278 loc) · 11.9 KB
/
ass2srt.pl
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl -w
use strict;
use warnings;
use encoding 'utf-8';
use Getopt::Long;
use File::Spec;
Getopt::Long::Configure( "pass_through", "no_ignore_case" );
# Default encoding
my $defaultEnc = 'utf8';
my $fromEnc = '';
my $toEnc = '';
my $help = 0;
my $preserve = 0;
my $showVer = 0;
my $showLicense = 0;
my $dosFormat = 0;
my $macFormat = 0;
my $oldTime = "";
$fromEnc = $ENV{'ASS2SRT_FROM'};
$toEnc = $ENV{'ASS2SRT_TO'};
my $DEBUG = 0;
my $noCorrect = 0;
GetOptions(
"help|h", \$help, "from|f=s", \$fromEnc,
"to|t=s", \$toEnc, "default|d=s", \$defaultEnc,
"preserve|p", \$preserve,
"version|v", \$showVer, "dos|o", \$dosFormat,
"mac|m", \$macFormat, "license|l", \$showLicense,
"debug", \$DEBUG, "nocorrect", \$noCorrect
);
# Lines read from .ass file will be put here (each in a hash with the
# relevant info) to be written to the .srt file after some post-processing.
my @lines;
# Wrap format
my $crlf = "\n";
# If the specified output at the same time as Mac and DOS format,
# responding to an error message and terminated
( $dosFormat && $macFormat )
&& die( "\n Error: \n\tOption -m|--mac and -o|--dos can not exist together \n" );
if ($dosFormat) { $crlf = "\r" . $crlf; }
if ($macFormat) { $crlf = "\r"; }
# Program version
my $version = "0.3.0.1";
# Display help message
if ($help) { usage(); }
# Show license information
if ($showLicense) { license(); }
# Show version Information
if ($showVer) { showVersion(); }
# If the source file encoding is not specified, then set it equals to defaultEnc
if ( !$fromEnc ) { $fromEnc = $defaultEnc; }
# If the purpose file encoding is not specified, then set it equals to defaultEnc
if ( !$toEnc ) { $toEnc = $defaultEnc; }
# The .ass file file name
my $assFile = shift || '';
if ( !$assFile ) { usage(); }
# The .srt file file name, the default value is .ass's filename,
# but changed the name extension to .srt
my $srtFile = shift || '';
if ( !$srtFile ) {
$srtFile = $assFile;
$srtFile =~ s|.[^.]*$||;
$srtFile .= ".srt";
}
# Using the parameters listed
print "Using parameters: \n";
print "\t .ass File Encoding: $fromEnc\n";
print "\t .SRT File Encoding: $toEnc\n";
print "\n";
# Open file
open( ASSFILE, "<", $assFile )
or die 'Can not open ".ass" source file:' . $assFile . ", please check it!!\n";
# Specify the file encoding
binmode( ASSFILE, ':encoding(' . $fromEnc . ')' );
# Check the existence of the system environment variable OS (Windows OS should be set this)
my $OS_TYPE = $ENV{'OS'};
# Assigned the .srt file encodings
my $toEncString = ':encoding(' . $toEnc . ')';
# Depending on OS type (only for Windows), fix the .srt file encoding setting.
if ( $OS_TYPE && $OS_TYPE =~ m/windows/i ) {
print "Operation System is: " . $OS_TYPE
. ", Checking File Encoding... \n";
# Only use UTF16 or UCS format when otherwise specified (others need another test)
if ( ( $toEnc =~ m/utf16/i ) || ( $toEnc =~ m/ucs/i ) ) {
$toEncString = ':raw' . $toEncString;
}
}
# Unicode output file on Windows need to join the BOM identification mark.
if ( $toEncString =~ m/:raw/i ) {
print SRTFILE "\x{FEFF}";
print "Using unicode encoding, print BOM signature to file: " . $srtFile
. "\n\n";
}
# Number of records subtitles
my $lineNum = 0;
# Read .ass and deal with the source file
while (<ASSFILE>) {
chomp;
# If a line begin with 'Dialougue', extract this line's parameters
if (m/^Dialogue:/) {
$lineNum = extractLine( $lineNum, $_ );
}
elsif (m/^(Title:|Original)/) {
# If a line begins with 'Title' or 'Original', it is the source for the subtitles
print $_ . "\n";
}
}
# Close the file
close ASSFILE;
# post-process lines to remove errors.
if (!$noCorrect)
{
for (my $i = 0; $i < scalar @lines; $i++)
{
my $line = $lines[$i];
my $prevLine = undef;
$prevLine = $lines[$i-1] if $i > 0;
# if the begin of the previous line is the same as the begin of this line,
# merge the two lines.
if ($prevLine && $prevLine->{begin} eq $line->{begin})
{
# merge subtitles
$prevLine->{subtitle} .= "\n" . $line->{subtitle};
# remove line $i from the list
@lines = (@lines[0..$i-1], @lines[$i+1..$#lines]);
# resync things for next test
$i--;
$prevLine = undef;
$prevLine = $lines[$i-1] if $i > 0;
}
# if the end of the previous line is smaller than the begin of this line,
# change the end of the previous line to be the begin of this line.
if ($prevLine && $prevLine->{end} gt $line->{begin})
{
$prevLine->{end} = $line->{begin};
}
}
}
open( SRTFILE, ">", $srtFile )
or die 'Can not open the ".srt" purpose of file:' . $srtFile . ", please check!\n";
# Specified file used to write code
binmode( SRTFILE, $toEncString );
# Write SRT file.
my $lineNumber = 1;
foreach my $line (@lines)
{
# Write .srt file
# Because .ass unit of time (10ms) different with .srt (1ms), so the full complement of 0
my $currentTime = $line->{begin} . "0 --> " . $line->{end} . "0";
print SRTFILE $lineNumber . $crlf;
print SRTFILE $currentTime . $crlf;
print SRTFILE $line->{subtitle} . " " . $crlf . $crlf . $crlf;
$lineNumber++;
}
close SRTFILE;
# Extract data for one line in the .ass file
sub extractLine {
# Deal with the number of rows
# From. ass of the original content
my ($lineNumber, $content) = @_;
my $begin;
my $end;
my $subtitle;
my $currentTime;
# Solved starting time, ending time, subtitle format, subtitles content
if ( $content
=~ m/Dialogue: [^,]*,([^,]*),([^,]*),([^,]*),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)$/
)
{
$begin = $1;
$end = $2;
$subtitle = $4;
my $isComment = $3;
print "\nLine: $lineNumber\n Begin: [$begin] End: [$end] isComment: [$isComment]\n Subtitle: [$subtitle]\n"
if $DEBUG;
# the separator between seconds and ms is "," -- not ".", so we change it !
$begin =~ s/\./,/g;
$end =~ s/\./,/g;
# If the time format will not be part of the hour when the two chars, make up two chars.
if ( $begin =~ m/^\d{1}:/ ) {
$begin = "0" . $begin;
}
if ( $end =~ m/^\d{1}:/ ) {
$end = "0" . $end;
}
# First filter out the end of every title to the digital sign in order to follow-up to the output under a variety of formats on different platforms
$subtitle =~ s/\r$//g;
# If there is no such setting .ass control commands, then filter out the
if ( !$preserve ) {
$subtitle =~ s/{[^}]*}//g;
}
# Comment if the subtitle format, then in the before and after the add ()
if ( $isComment eq 'comment' ) {
$subtitle = '(' . $subtitle . ')';
}
print "\nAfter:\n Begin: [$begin] End: [$end] isComment: [$isComment]\n Subtitle: [$subtitle]\n"
if $DEBUG;
my %line = ( begin => $begin, end => $end, isComment => $isComment, subtitle => $subtitle );
push @lines, \%line;
return $lineNumber;
}
}
# Use
sub usage {
print <<__HELP__;
ass2srt [option] .ass source files [.srt purpose of file]
The Advanced SubStation Alpha ".ass" file format to SubRip ".srt" format.
Options:
-h --help help show this help message
-d, --default=encoding default = encoding set the default file encoding, which is the purpose of the source file and use the same code file.
The default value is UTF-8 encoding format.
-f, --from=encoding from = encoding specified source file. ass coding system used. The set will be covered by the aforementioned pre -
Coding based on the settings file, when not specified the default file encoding for the default value.
* Tip: You can also use the system environment variables specified ASS2SRT_FROM
-t, --to=encoding encoding file specified purposes. srt coding system used. The set will be covered by the aforementioned pre -
Coding based on the settings file, when not specified the default file encoding for the default value.
* Tip: You can also use the system environment variables specified ASS2SRT_TO
-p, --preserve preserve the source file to retain. ass subtitles in the control instructions, write together. srt file.
-o, --dos dos specified output file format for DOS (the default format for Unix)
* Warning: can not be set this option with -m/--mac together.
-m, --mac mac specify the output file format for the Mac (the default format for Unix)
* Warning: can not be this option with set -o/--dos together.
--debug print debugging output for each line of the .ass file read.
--nocorrect don't attempt to correct errors in subtitles
if not specified, corrections will be made for the following errors:
- subtitle i+1 begins before subtitle i ends - subtitle i's end will be set to subtitle i+1's begin
- subtitle i+1 begins at the same time as subtitle i begins - subtitles will be merged into one subtitle
.ass source file:
The Subtitles of the original file.
It can be Advanced SubStation Alpha (.ass) or SubStation Alpha (.ssa) format.
[.srt purpose file]:
The default SubRip format output filename, the default value is the same as
the .ass source filename but it's extension changed to .srt.
Operation Example:
Like iconv utility, use -f to specified from encoding set, the -t to specified target encoding set.
1. Convert a utf16-le.ass file encoded with Unicode/UTF-18 LE to big5.srt with Big5 encoding.
myhost \$ perl ass2srt.pl -f utf16-le -t big5 utf16-le.ass big5.srt
2. Convert a Big5 encoding .ass file to UTF-8 encoding .srt file
myhost \$ perl ass2srt.pl -f big5 -t utf8 big5.ass utf8.srt
3. Convert a UTF-8 encoding .ass file to UTF-8 encoding .srt file,
no -f or -t parameter because UTF-8 is the default encoding setting.
myhost \$ perl ass2srt.pl utf-8.ass utf-8.srt
4. Convert a UCS-2 LE encoding .ass file to UCS-2 LE encoding .srt file,
UCS-2 LE is the same as Unicode encoding on Windows Platform.
myhost \$ perl ass2srt.pl -f ucs-2le -t ucs-2le ucs-2le.ass ucs-2le.srt
5. Using the environment variables to specified encoding parameter
* Warning: Some operation system can not add " or ' quotation marks to the varialbe's value
myhost \$ export ASS2SRT_FROM = utf16-le
myhost \$ export ASS2SRT_TO = big5
myhost \$ perl ass2srt.pl utf16-le.ass big5.srt
__HELP__
exit 2;
}
# Show version of the message
sub showVersion {
print <<__VERSION__;
ass2srt $version - Convert subtitles from .ass to .srt format
Advanced SubStation Alpha subtitle file format conversion tool
(c) 2006 Ada Hsu, hungwei.hsu (at) gmail (dot) com
(c) 2009 Jean-Sebastien Guay, jean_seb (at) videotron (dot) ca
__VERSION__
exit 2;
}
# License
sub license {
print <<__LICENSE__;
ass2srt $version - Convert subtitles from .ass to .srt format
Advanced SubStation Alpha subtitle file format conversion tool
(c) 2006 Ada Hsu, hungwei.hsu (at) gmail (dot) com
(c) 2009 Jean-Sebastien Guay, jean_seb (at) videotron (dot) ca
This utility use of the software CC-GNU GPL (http://creativecommons.org/licenses/GPL/2.0/)
authorization, You can view the relevant provisions of
http://www.gnu.org/copyleft/gpl.html text.
You are free to use the software, but software maintainer will hold no liability for any damages,
Thank You.
__LICENSE__
exit 2;
}