#!/usr/bin/perl # CSV2srt.pl # # Copyright(C) Since 2009 Akira KAKINOHANA All Rights Reserved # Author : Akira KAKINOHANA # Distributed at : http://softwares.kirameister.net/ # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # For GNU General Public License, see . my $version = "0.05 - Sep 12 2009"; use strict; use warnings; use Text::CSV_XS; use utf8; binmode(STDOUT, ":utf8"); my $rfh; my $tc = Text::CSV_XS->new({binary=>1, allow_loose_quotes=>1}); # to handle non_ascii characters; my $csv_file = shift or help(); my $out_file = shift; open ($rfh, "<:utf8", $csv_file) or die "Cannot open the file $csv_file : $!\n"; chomp($out_file) if (defined($out_file)); if (defined($out_file)){ open (OUT, ">:utf8", $out_file) or die "Cannot open the file $out_file : $!\n"; } my $count = 0; while(<$rfh>){ ## this is for availding an unnecessary error on a line with double-quote and new line (which leaves only one double-quote..) $_ =~ s/^(.*)\".*?$/$1/ if (!($tc->parse($_))); next unless $tc->parse($_); my ($no, $s_time, $e_time, $en, $ja, $temp) = $tc->fields; chomp($no) if (defined($no) ); chomp($s_time) if (defined($s_time) ); chomp($e_time) if (defined($e_time) ); chomp($ja) if (defined($ja) ); next if ($no !~ m/\d+/); next if (!(defined($s_time))); next if ($s_time !~ m/^\d:\d\d:\d\d$/ && $s_time !~ m/^\d\d\:\d\d\:\d\d\.\d\d\d$/ && $s_time !~ m/^\d:\d\d:\d\d\.\d\d\d$/ ); $s_time = "0" . $s_time . ",000" if ($s_time =~ m/^\d\:\d\d\:\d\d$/); if ($s_time =~ m/^\d:\d\d:\d\d.\d\d\d$/ ){ $s_time =~ m/^(\d:\d\d:\d\d).(\d\d\d)$/; $s_time = "0" . $1 . "," . $2; } next if (!(defined($e_time))); next if ($e_time !~ m/^\d:\d\d:\d\d$/ && $e_time !~ m/^\d\d\:\d\d\:\d\d\.\d\d\d$/ && $e_time !~ m/^\d:\d\d:\d\d\.\d\d\d$/ ); $e_time = "0" . $e_time . ",000" if ($e_time =~ m/^\d\:\d\d\:\d\d$/); if ($e_time =~ m/^\d:\d\d:\d\d.\d\d\d$/ ){ $e_time =~ m/^(\d:\d\d:\d\d).(\d\d\d)$/; $e_time = "0" . $1 . "," . $2; } #1,0:00:00.000,0:00: $ja = "" if (!(defined($ja))); $ja =~ s/''/\"/g if (defined($ja)); $count++; if ($count != $no){ print STDERR "Segment No. " . $count . " is missing..\n"; $count = $no; } if ($ja eq ""){ print STDERR "Segment No. " . $count . " has a technical problem. Skipping..\n"; next; } if ($out_file){ print OUT "$no\n"; print OUT "$s_time --> $e_time\n"; print OUT "$ja\n"; print OUT "\n"; }else{ print "$no\n"; print "$s_time --> $e_time\n"; print "$ja\n"; print "\n"; } } close $rfh; close (OUT); sub help { die "Usage : perl $0 csv_file [out_file]\n"; } __END__