Created
November 9, 2013 19:57
-
-
Save sng2c/7389229 to your computer and use it in GitHub Desktop.
make multi-language smi subtitle file to mono-language
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/env perl | |
| use 5.018; | |
| #use Encode qw(from_to); | |
| my $smi_path = $ARGV[0]; | |
| my $want = lc $ARGV[1]; | |
| my $smi; | |
| { | |
| local $/; | |
| undef $/; | |
| open(my $smifh,'<', $smi_path); | |
| $smi = <$smifh>; | |
| close($smifh); | |
| } | |
| #from_to($smi, 'cp949','utf8'); | |
| my @langs; | |
| if( $smi =~ m@<sami>(.+)</sami>@is ) | |
| { | |
| my $sami = $1; | |
| if( $sami =~ m@<head>(.+)</head>@is ){ | |
| my $head = $1; | |
| while( $head =~ /\.(\w+)\s*{(.+?)}/g ){ | |
| my $cls = $1; | |
| my $prp = $2; | |
| if( $prp =~ /lang\s*:\s*([^;]+)\s*/i ){ | |
| push(@langs, $1); | |
| } | |
| } | |
| } | |
| } | |
| if( !@langs || !$want ){ | |
| # nothing to do | |
| } | |
| else{ | |
| my $kor; | |
| my $head; | |
| my $body; | |
| if( $smi =~ m@<sami>(.+)</sami>@is ) | |
| { | |
| rename($smi_path,$smi_path.'.orig') unless -e $smi_path.'.orig'; | |
| open my $out, '>', $smi_path; | |
| print $out "<SAMI>\n"; | |
| my $sami = $1; | |
| if( $sami =~ m@<head>(.+)</head>@is ){ | |
| print $out "<HEAD>\n"; | |
| $head = $1; | |
| while( $head =~ /\.(\w+)\s*{(.+?)}/g ){ | |
| my $cls = $1; | |
| my $prp = $2; | |
| if( $prp =~ /lang\s*:\s*\Q$want\E/i ){ | |
| $kor = $cls; | |
| last; | |
| } | |
| } | |
| print $out $head; | |
| print $out "</HEAD>\n"; | |
| } | |
| if( $sami =~ m@<body>(.+)</body>@is ){ | |
| $body = $1; | |
| print $out "<BODY>\n"; | |
| my @chunks = split(/(<.+?>)/,$body); | |
| my $print; | |
| for( my $i=0; $i<@chunks; $i++){ | |
| my $c = $chunks[$i]; | |
| if( $c =~ /^<sync/i){ | |
| print $out $c; | |
| } | |
| elsif( $c =~ /^<p\s+class\s*=\s*(.+?)\s*>/i ){ | |
| my $cls = $1; | |
| $print = $cls eq $kor; | |
| print $out $c if $print; | |
| } | |
| else{ | |
| print $out $c if $print; | |
| } | |
| } | |
| print $out "</BODY>\n"; | |
| } | |
| print $out "</SAMI>\n"; | |
| close($out); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment