Skip to content

Commit

Permalink
[FIXED] Character encoding conversion using Encode
Browse files Browse the repository at this point in the history
[REMOVED] Usage of system calls and piconv
  • Loading branch information
PhobosK committed Jul 31, 2015
1 parent 8412239 commit 1545a09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Have a look to the file COPYING included in the archive.

Changelog:
----------
01 Aug 2015: PhobosK
* sub2srt-0.5.6
- Fixed character encoding conversion using Encode instead of piconv

10 Dec 2014: Toni Ahola
* sub2srt-0.5.5
- Added support to encode output with piconv
Expand Down
36 changes: 31 additions & 5 deletions sub2srt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

use strict;
use warnings;
my $version = "0.5.5";
my $version = "0.5.6";

use Getopt::Long;
Getopt::Long::Configure("pass_through","no_ignore_case");
use Encode("from_to","find_encoding");
use File::Copy("mv");
use File::Temp;
my $help = 0;
my $fps = 25;
my $showvers = 0;
Expand Down Expand Up @@ -132,10 +135,33 @@ close OUTFILE;


if($convert) {
my $tmpfile = tmpnam();
system("mv $outfile $tmpfile");
system("piconv -f $fenc -t $tenc < $tmpfile > $outfile");
system("rm $tmpfile");
# Check if $fenc and $tenc are valid
if (!find_encoding($fenc)) {
print "--> $fenc <-- is not a valid From encoding. Encoding conversion skipped.\n";
exit 0;
}
if (!find_encoding($tenc)) {
print "--> $tenc <-- is not a valid To encoding. Encoding conversion skipped.\n";
exit 0;
}

my $tmpfile = tmpnam();

open(INPUT, "< :raw", $outfile)
or die "Unable to open < $outfile for reading: $!\n. Encoding conversion skipped.\n";
open(OUTPUT, "> :raw", $tmpfile)
or die "Unable to open > $tmpfile for writing: $!\n. Encoding conversion skipped.\n";
while (<INPUT>) {
from_to($_, $fenc, $tenc, Encode::FB_CROAK);
print OUTPUT;
}

close INPUT or die "Unable to close $outfile: $!\n";
close OUTPUT or die "Unable to close $tmpfile: $!\n";

mv($tmpfile, $outfile);

print "Encoding convertion from \"$fenc\" to \"$tenc\" done.\n" if (!$quiet);
}


Expand Down

0 comments on commit 1545a09

Please sign in to comment.