Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active December 17, 2015 03:38
Show Gist options
  • Select an option

  • Save dex4er/5544583 to your computer and use it in GitHub Desktop.

Select an option

Save dex4er/5544583 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use v5.10;
use Scalar::Util 'looks_like_number';
my $rate = looks_like_number $ARGV[0] ? shift @ARGV : 25.000;
for my $filein (@ARGV) {
open my $fhin, "<:crlf:encoding(cp-1250)", $filein or die "open: $filein: $!";
$fileout = $filein =~ s/(\.txt)?$/.srt/r;
open my $fhout, ">:crlf:utf8", $fileout or die "open: $fileout: $!";
my $n = 1;
while (<$fhin>) {
chomp;
/^{(\d+)}{(\d+)}(.*)$/ or /^\[(\d+)\]\[(\d+)\](.*)$/ or next;
my ($t1, $t2, $text) = ($1 / $rate, $2 / $rate, $3);
my ($h1, $m1, $s1, $u1) = ($t1 / 60 / 60, $t1 / 60 % 60, $t1 % 60, 1000*($t1 - int $t1));
my ($h2, $m2, $s2, $u2) = ($t2 / 60 / 60, $t2 / 60 % 60, $t2 % 60, 1000*($t2 - int $t2));
$text =~ s/{.*?}//g;
printf $fhout "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n",
$n++, $h1, $m1, $s1, $u1, $h2, $m2, $s2, $u2, $text;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment