convert srt to our yaml format
[HTML5TV.git] / bin / srt2yaml.pl
1 #!/usr/bin/perl
2
3 # ./bin/srt2yaml.pl media/_editing/captions.srt > media/_editing/video.srt.yaml
4
5 use warnings;
6 use strict;
7
8 use YAML;
9 use Data::Dump qw(dump);
10
11 my @subtitles;
12
13 my $nr = 0;
14 our @s;
15
16 sub to_t {
17         my $txt = shift;
18         $txt =~ s/,/./; # fix decimal
19         my @t = split(/:/,$txt);
20         my $t = ( $t[0] * 60 * 60 ) + ( $t[1] * 60 ) + $t[2];
21         warn "# $txt -> $t\n";
22         return $t;
23 }
24
25 while(<>) {
26         s/^\xEF\xBB\xBF//; # strip utf-8 marker
27         s/[\n\r]+$//;
28         warn "# ",dump $_;
29         if ( length($_) == 0 ) {
30
31                 warn "s = ",dump(@s);
32
33                 my ( $f,$t ) = split(/\s*-->\s*/, $s[1], 2);
34
35                 $subtitles[ $s[0] ] = [ to_t($f), to_t($t), join(" ", @s[ 2 .. $#s ]) ];
36
37                 @s = ();
38                 next;
39         }
40         push @s, $_;
41
42 }
43
44 $subtitles[0] ||= [ 0, 0.001, "[1]" ]; # fake first slide
45
46 print Dump @subtitles;