-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathass2json.pl
201 lines (165 loc) · 5.19 KB
/
ass2json.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
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use English qw( -no_match_vars );
use Getopt::Long;
use utf8;
use feature 'unicode_strings';
use Mojolicious;
use Mojo::JSON qw(j);
use Mojo::UserAgent;
use Readonly;
our $VERSION = 0.1;
# constants
Readonly my $GOOGLE_APIS_ENDPOINT =>
'https://www.googleapis.com/language/translate/v2';
my $infile = qw{ };
my $source;
my $target;
my $translate;
my $count;
my $verbose;
# setting STDOUT to UTF-8
binmode STDOUT, ':encoding(UTF-8)';
GetOptions(
'file=s' => \$infile,
'in=s' => \$source,
'of=s' => \$target,
'translate' => \$translate,
'count' => \$count,
'verbose+' => \$verbose,
);
# check file permissions
if ($verbose) {
print "-> Checking file permissions ($infile)\n"
or die "Can't write verbose file permissions: $ERRNO\n";
}
if (!defined $infile || !-r $infile) {
croak 'Error in command line arguments, check README for usage.';
}
# slurp file content
my $content;
{
local $INPUT_RECORD_SEPARATOR = undef;
open my $fh, '<:encoding(UTF-8)', $infile
or die "Can't open $infile: $ERRNO\n";
$content = <$fh>;
close $fh or die "Can't close $infile: $ERRNO\n";
}
# removing CR LF
$content =~ s/\r\n/\n/smxg;
# parsing ASS file
my $array_parsed = parse_ass($content);
# verbose progress
my $progress = 1;
my $total = scalar @{$array_parsed};
# translating the content (if -t was specified in the program call)
my @array_translated;
foreach my $subtitle (@{$array_parsed}) {
my %translated;
# verify if -t was specified in the program call
if ($translate) {
if ($verbose) {
print "-> Translating $progress of $total\n"
or die "Can't write verbose progress: $ERRNO\n";
}
if (defined $subtitle->{formated}) {
# for formatted text
$translated{subtitle}
= get_translation($source, $target, $subtitle->{text});
$translated{subtitle}
= $subtitle->{prefix}
. $translated{subtitle}
. $subtitle->{suffix};
}
else {
# for unformatted text
$translated{subtitle}
= get_translation($source, $target, $subtitle->{subtitle});
}
}
else {
# does not translate
$translated{subtitle} = $subtitle->{subtitle};
# if -c was specified in the program call
if ($count) {
$count += length $subtitle->{subtitle};
}
}
# the other fields remain the same
$translated{original} = $subtitle->{subtitle};
$translated{start_time} = $subtitle->{start_time};
$translated{end_time} = $subtitle->{end_time};
push @array_translated, \%translated;
$progress++;
}
# writing results in file.json (if -c was not specified in the program call)
if (!$count) {
open my $fh, '>:encoding(UTF-8)', $infile . '.json'
or die "Can't open result file $infile.json: $ERRNO\n";
print {$fh} j(\@array_translated)
or die "Can't write result file $infile.json: $ERRNO\n";
close $fh or die "Can't close result file $infile.json: $ERRNO\n";
if ($verbose) {
print "-> Write results file ($infile.json)\n\n"
or die "Can't write verbose file output: $ERRNO\n";
}
}
# print results (if -v was specified in the program call)
if (defined $verbose && $verbose >= 2) {
print j(\@array_translated) or die "Can't show results: $ERRNO\n";
}
# print count results
if ($count) {
print "\nCharacters: $count\n"
or die "Can't print count results: $ERRNO\n";
}
sub parse_ass {
my $file_content = shift;
my @result;
# break file for better parsing
my @subtitles = split /\n\n/smx, $file_content;
foreach my $subtitle (@subtitles) {
if (
$subtitle =~ m{\d+\s*\n
(?<start_time>\d+:\d+:\d+,\d+)\s+\-\-\>\s+(?<end_time>\d+:\d+:\d+,\d+)\s*\n
(?<subtitle>.+)}smx
)
{
my %tmp_sub;
$tmp_sub{start_time} = $LAST_PAREN_MATCH{start_time};
$tmp_sub{end_time} = $LAST_PAREN_MATCH{end_time};
$tmp_sub{subtitle} = $LAST_PAREN_MATCH{subtitle};
if ($tmp_sub{subtitle}
=~ /(?<prefix><[^>]+>)(?<text>.*)(?<suffix><[^>]+>)/smx)
{
$tmp_sub{text} = $LAST_PAREN_MATCH{text};
$tmp_sub{prefix} = $LAST_PAREN_MATCH{prefix};
$tmp_sub{suffix} = $LAST_PAREN_MATCH{suffix};
$tmp_sub{formated} = 'y';
}
push @result, \%tmp_sub;
}
}
return \@result;
}
#
# get_translation function takes a source language, a target language and
# string and translate it using the Google Translate API and returns a string
# with the translated text.
#
sub get_translation {
my $src = shift;
my $trg = shift;
my $query = shift;
my $ua = Mojo::UserAgent->new;
return $ua->get(
$GOOGLE_APIS_ENDPOINT => form => {
key => $ENV{GOOGLE_TRANSLATE_API_KEY},
source => $src,
target => $trg,
q => $query,
}
)->res->json('/data/translations/0/translatedText');
}