-
Notifications
You must be signed in to change notification settings - Fork 34
/
srt_assembler.pl
executable file
·214 lines (169 loc) · 5.01 KB
/
srt_assembler.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 14 December 2014
# Edit: 14 December 2016
# License: GPLv3
# https://github.com/trizen
# Extract the text and the skeleton from a SRT file.
# The text can be translated into another language, then
# joined back with the SRT skeleton into a new SRT file.
use utf8;
use 5.010;
use strict;
use autodie;
use warnings;
use experimental qw(signatures);
use Getopt::Std qw(getopts);
use File::BOM qw(get_encoding_from_filehandle);
sub usage {
my ($code) = @_;
require File::Basename;
my $main = File::Basename::basename($0);
print <<"EOF";
usage: $main [options] [input file]
options:
-j : join text with template
-t : name of the template file
example:
$main -t file.t file.srt > file.text
$main -t file.t file.text > new_file.srt
EOF
exit($code // 0);
}
sub prepare_words ($words, $width, $callback, $depth = 0) {
my @root;
my $len = 0;
my $i = -1;
my $limit = $#{$words};
while (++$i <= $limit) {
$len += (my $word_len = length($words->[$i]));
if ($len > $width) {
if ($word_len > $width) {
$len -= $word_len;
splice(@$words, $i, 1, unpack("(A$width)*", $words->[$i]));
$limit = $#{$words};
--$i;
next;
}
last;
}
#<<<
push @root, [
join(' ', @{$words}[0 .. $i]),
prepare_words([@{$words}[$i + 1 .. $limit]], $width, $callback, $depth + 1),
];
#>>>
if ($depth == 0) {
$callback->($root[0]);
@root = ();
}
last if (++$len > $width);
}
\@root;
}
sub combine ($path, $callback, $root = []) {
my $key = shift(@$path);
foreach my $value (@$path) {
push @$root, $key;
if (@$value) {
foreach my $item (@$value) {
combine($item, $callback, $root);
}
}
else {
$callback->($root);
}
pop @$root;
}
}
sub smart_wrap ($text, $width) {
my @words = (
ref($text) eq 'ARRAY'
? @{$text}
: split(' ', $text)
);
my %best = (
score => 'inf',
value => [],
);
prepare_words(
\@words,
$width,
sub ($path) {
combine(
$path,
sub ($combination) {
my $score = 0;
foreach my $line (@$combination) {
$score += ($width - length($line))**2;
return if $score >= $best{score};
}
$best{score} = $score;
$best{value} = [@$combination];
}
);
}
);
join("\n", @{$best{value}});
}
sub disassemble ($srt_file, $template_file) {
open(my $srt_fh, '<:crlf', $srt_file);
open(my $tmpl_fh, '>', $template_file);
my $enc = get_encoding_from_filehandle($srt_fh);
if (defined($enc) and $enc ne '') {
binmode($srt_fh, ":encoding($enc)");
binmode(STDOUT, ":encoding($enc)");
}
local $/ = ""; # paragraph mode
while (defined(my $para = <$srt_fh>)) {
if (
$para =~ /^
(?<i>[0-9]+)\h*\R
(?<from>[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})
\h*-->\h*
(?<to>[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})\h*\R
(?<text>.+)/sx
) {
print {$tmpl_fh} "$+{i}\n$+{from} --> $+{to}\n%s\n\n";
my $text = $+{text};
$text =~ s/<.*?>//gs; # remove HTML tags
# (consider this a bug)
print join(' ', split(' ', $text)), "\n\n";
}
else {
die "[ERROR] Invalid paragraph:
{{->>BEGIN<<-}}
$para
{{->>END<<-}}\n";
}
}
close $srt_fh;
close $tmpl_fh;
}
sub assemble ($text_file, $template_file) {
open my $txt_fh, '<:crlf', $text_file;
open my $tmpl_fh, '<:crlf', $template_file;
my $enc = get_encoding_from_filehandle($txt_fh)
|| get_encoding_from_filehandle($tmpl_fh);
if (defined($enc) and $enc ne '') {
binmode($txt_fh, ":encoding($enc)");
binmode($tmpl_fh, ":encoding($enc)");
binmode(STDOUT, ":encoding($enc)");
}
local $/ = "";
while (defined(my $text = <$txt_fh>)) {
my $format = <$tmpl_fh> // die "Unexpected error: template file is shorter than text!";
$text =~ s/[?!.)\]"']\K\h+([-‒―—]+)(?=\h)/\n$1/g;
$text = join("\n", map { length($_) <= 45 ? $_ : smart_wrap($_, 45) } split(/\R/, $text));
printf($format, $text);
}
close $txt_fh;
close $tmpl_fh;
}
my %opt;
getopts('jt:h', \%opt);
my $input_file = shift(@ARGV) // usage(1);
my $template_file = $opt{t} // ($input_file =~ s/\.\w{1,5}\z//r . '.template');
$opt{j} || ($input_file !~ /\.srt\z/)
? assemble($input_file, $template_file)
: disassemble($input_file, $template_file);