-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2ass.pl
52 lines (40 loc) · 1.15 KB
/
json2ass.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
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use English qw( -no_match_vars );
use Getopt::Long;
use Mojolicious;
use JSON;
our $VERSION = 0.1;
my $infile = qw{ };
# setting STDOUT to UTF-8
binmode STDOUT, ':encoding(UTF-8)';
GetOptions('file=s' => \$infile,);
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";
}
# parsing content (in a perl structure)
my $array = JSON->new->utf8->decode($content);
# generate SRT
my $srt;
my $index = 1;
foreach my $subtitle (@{$array}) {
$srt
.= "$index\n$subtitle->{start_time} --> $subtitle->{end_time}\n$subtitle->{subtitle}\n\n";
$index++;
}
# writing results in file.srt
open my $fh, '>:encoding(UTF-8)', $infile . '.ass'
or die "Can't open $infile.ass: $ERRNO\n";
print {$fh} $srt or die "Can't write $infile.ass: $ERRNO\n";
close $fh or die "Can't close $infile.ass: $ERRNO\n";