From 1545a09e53727fa2d47a659cea99c72a002eebe1 Mon Sep 17 00:00:00 2001 From: PhobosK Date: Sat, 1 Aug 2015 02:14:22 +0300 Subject: [PATCH] [FIXED] Character encoding conversion using Encode [REMOVED] Usage of system calls and piconv --- README | 4 ++++ sub2srt | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README b/README index 4be3297..9fb9c42 100644 --- a/README +++ b/README @@ -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 diff --git a/sub2srt b/sub2srt index 442cfe9..89bd2a6 100755 --- a/sub2srt +++ b/sub2srt @@ -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; @@ -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 () { + 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); }