Created
February 16, 2022 18:38
-
-
Save richardjharris/ab76bcd50dd076bb9d03886b45d76996 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use autodie ':io'; | |
| use utf8; | |
| use open ':std', ':encoding(UTF-8)'; | |
| use File::Temp; | |
| use File::Copy qw(move); | |
| my $JP_SPACE = ' '; | |
| my $JP_STYLE = 'Style:Japanese,Arial,24,&H00FFEEEE,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,1,2,10,10,10,1\n'; | |
| for my $episode (qw(01 02 03 04 05 06 07 08 09 10 11), '12 finale') { | |
| my $base = "Nihonjin no Shiranai Nihongo ep$episode (704x396 XviD)"; | |
| convert("$base.ass", "$base.jp.srt", "enjp-$base.ass"); | |
| } | |
| sub convert { | |
| my ($ass, $srt, $out) = @_; | |
| open my $assfh, '<:utf8', $ass; | |
| open my $srtfh, '<:utf8', $srt; | |
| my $outfh = File::Temp->new; | |
| binmode $outfh, ':utf8'; | |
| # Read .ass file and add Japanese style | |
| while (<$assfh>) { | |
| if (/^Style:\s*Default/) { | |
| # Change alignment from middle-bottom to middle-top and adjust border | |
| s/2,2,2/2,1,8/; | |
| } | |
| # Upgrade to v4+ for sanity reasons - alignment numbers different between v4 and v4+ | |
| s/v4\.00(?!\+)/v4.00+/i if /^ScriptType:/i; | |
| s/V4/V4+/i if /\[V4 Styles\]/i; | |
| print {$outfh} $_; | |
| if (/^Style:\s*Default/) { | |
| print {$outfh} $JP_STYLE; | |
| } | |
| } | |
| # Now add Japanese lines | |
| while (<$srtfh>) { | |
| my $num = $_; | |
| # A byte-order mark? Get out with yez | |
| $num =~ s/^\x{FEFF}//; | |
| $num =~ /^\d+\r?\n$/ or die "can't parse '$_'"; | |
| my $timings = <$srtfh>; | |
| $timings =~ /^(.*?) --> (.*?)\r?\n$/; | |
| my ($start, $end) = ($1, $2); | |
| my @lines; | |
| while (<$srtfh>) { | |
| s/\r?\n//g; | |
| last if !$_; | |
| push @lines, $_; | |
| } | |
| my $all = join '\\n', @lines; | |
| $all =~ s/$JP_SPACE+/$JP_SPACE/g; | |
| printf {$outfh} "Dialogue: 0,%s,%s,Japanese,,0000,0000,0000,,%s\n", | |
| _time2ass($start), | |
| _time2ass($end), | |
| $all; | |
| } | |
| close $outfh; | |
| move($outfh->filename, $out); | |
| } | |
| sub _time2ass { | |
| my $t = shift; | |
| $t =~ /^(\d+):(\d+):(\d+),(\d\d)\d*$/ or die "invalid time '$t'"; | |
| my ($h,$m,$s,$ms) = ($1, $2, $3, $4); | |
| $h = 0+$h; # no leading zeros | |
| return sprintf '%d:%02d:%02d.%02d', $h, $m, $s, $ms; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment