-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrt.pl
executable file
·49 lines (37 loc) · 938 Bytes
/
srt.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
#!/usr/bin/env perl
use strict;
use warnings;
sub trim {
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};
sub readfile {
my ($enc, $fname) = @_;
if ($enc ne "utf-8") {
return qx|iconv -f $enc -t UTF-8//TRANSLIT "$fname"|;
}
open(my $fh_in, "<", $fname);
my @subs = <$fh_in>;
close $fh_in;
return @subs;
}
if ($#ARGV < 0) {
print "usage: srt.pl <file>\n";
exit 1;
}
my $file = &trim(qx|file -bi "$ARGV[0]"|);
my @encoding = split("charset=", $file);
exit(1) if @encoding == 1;
print "Original encoding: @encoding\n";
my @res = &readfile($encoding[1], $ARGV[0]);
exit(1) if $? != 0;
print "Translating and converting... ";
open(my $fh_out, ">", $ARGV[0]);
my $srt = join '', @res;
$srt =~ s/<.*?i>|<.*?b>|<.*?u>//gi;
print $fh_out $srt;
close($fh_out);
my $info = "Encoding: @encoding\nFile: $ARGV[0] [OK]";
system("zenity --info --text=\"$info\"");
print "$info\n";