-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmi2srt
executable file
·182 lines (153 loc) · 4.13 KB
/
smi2srt
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
no warnings 'utf8';
my $USAGE = <<"USAGE";
Usage: smi2srt input-smi-01 [input-smi-02 ...]
or: smi2srt < input-smi > output-srt
See <https://github.com/9beach/srt-tools> for updates and bug reports
USAGE
sub say { print STDERR shift."\n"; }
my $no_pipe = -t STDIN;
die $USAGE if (
($#ARGV >= 0 and ($ARGV[0] eq "-h" or $ARGV[0] eq "--help")) or
(not -t STDOUT and $#ARGV >= 0) or
($no_pipe and $#ARGV < 0) or
(not $no_pipe and $#ARGV >= 0)
);
# Reads from pipe
if ($#ARGV < 0) {
my $content; { local $/; $content = <STDIN> };
die "invalid content\n" if (0 != to_srt(conv($content), *STDOUT));
exit 0;
}
my $err = 0;
# Reads ARGV files
foreach my $smi (@ARGV) {
my $srt = $smi; $srt =~ s/\.[^\/\.]*$//; $srt .= ".srt";
unless (open SMI, '<', $smi) {
$err = -1;
say "failed to open: ${smi}";
next;
}
if (-e $srt) {
$err = -1;
say "already exists: ${srt}";
close SMI;
next;
}
unless (open SRT, '>', $srt) {
$err = -1;
say "failed to open: ${srt}";
close SMI;
next;
}
my $content; { local $/; $content = <SMI> };
if (0 != to_srt(conv($content), *SRT)) {
$err = -1;
say "invalid content: ${smi}";
close SMI;
close SRT;
unlink $srt;
} else {
say "created: ${srt}";
close SMI;
close SRT;
}
}
exit $err;
# Defines submodules below
# Decodes SAMI to UTF-8
sub conv {
my $buf = shift;
if (not utf8::decode $buf) {
require Encode;
require Encode::Guess;
my $decoder = Encode::Guess::guess_encoding($buf, qw/cp949 euc-kr/);
if (ref $decoder) {
$buf = $decoder->decode($buf);
} else {
# Fairly often it works
$buf = Encode::decode("CP949", $buf);
}
}
return $buf;
}
# Converts "79145" to "00:01:19,145"
sub to_hms {
my $millisec = shift;
my $seconds = $millisec / 1000;
my $ms = $millisec % 1000;
my $s = $seconds % 60;
my $m = ($seconds / 60) % 60;
my $h = ($seconds / 3600) % 60;
return sprintf "%02d:%02d:%02d,%03d", $h, $m, $s, $ms;
}
# Coverts SAMI to UTF-8
# Returns 0 on success
sub to_srt {
my $buf = shift;
my $out = shift;
# FROM:
# ...
# <SYNC Start=50487><P Class=KOKRCC>
# Lolita,<br>light of my life,
# <SYNC Start=53239><P Class=KOKRCC>
#
# <SYNC Start=53323><P Class=KOKRCC>
# <font color=red>fire of my loins.<br>My sin, my soul.
# <SYNC Start=55074><P Class=KOKRCC>
#
#
# TO:
# ...\t50487 Lolita,\nlight of my life,\t53239 \t53323 <font color=red> ...
for ($buf) {
tr/\r\n\t//d;
# Some malformed SAMIs have <SYNC> after </BODY>
s/<\/BODY>.*//gi;
# They are useless in HTML rendering (<!-->, <P>, </P>, </SYNC>)
s/<(!--|.?P\b|\/SYNC)[^>]*>//gi;
s/ / /g;
s/<BR\b[^>]*> */\n/gi;
s/^ *\n//gm;
s/<SYNC[^=]*=[^0-9]*([0-9][0-9]*)[^>]*>[\n ]*/\t$1 /gi;
}
my @subs = split /\t/, $buf;
# At least 3 elements [ "<SAMI><HEAD>...", "\t...", "\t...", ... ]
return -1 if scalar(@subs) < 3;
# Removes a string before first TAB
shift @subs;
my $prev_time = 0;
my $prev_sub = "";
my $n = 0;
# FROM:
# \t50487 Lolita,\nlight of my life,
# \t53239
# \t53323 <font color=red>fire of my loins.\nMy sin, my soul.
# \t55074
#
# TO:
# 1
# 00:00:50,487 --> 00:00:53,239
# <font color=red>"Lolita,
# light of my life,"
#
# 2
# 00:00:53,323 --> 00:00:55,074
# <font color=red>"fire of my loins.
# My sin, my soul."
foreach (@subs) {
my $sp = index $_, ' ';
my $curr_sub = substr $_, $sp+1;
my $curr_time = int(substr $_, 0, $sp);
if ($prev_sub ne "") {
print $out "${\++$n}\n".
"${\to_hms $prev_time} --> ${\to_hms $curr_time}\n".
"$prev_sub\n\n";
}
$prev_time = $curr_time;
$prev_sub = $curr_sub;
}
return 0;
}