Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 91 lines (80 sloc) 2.888 kb
14c2758 @robinsmidsrod Added create_srt_from_sbv.pl helper script
robinsmidsrod authored
1 #!/usr/bin/env perl
2 #
3 # Written by Robin Smidsrød <robin@smidsrod.no>
4 #
5 # If you need to quickly download the Youtube video so that you can preview
6 # the translations in VLC or another video player I would suggest using this
7 # GreaseMonkey script: http://userscripts.org/scripts/show/62634
8 #
9 # Create .srt from specified files
10 # ./create_srt_from_sbv.pl path/to/something.sbv path/to/another.sbv
11 #
9706740 @robinsmidsrod Minor doc fix
robinsmidsrod authored
12 # Create .srt files from all .sbv files found under specified directory
13 # ./create_srt_from_sbv.pl path/to/sbvfiles/
14c2758 @robinsmidsrod Added create_srt_from_sbv.pl helper script
robinsmidsrod authored
14 #
15 # Create .srt files from all .sbv files found under current directory
9706740 @robinsmidsrod Minor doc fix
robinsmidsrod authored
16 # ./create_srt_from_sbv.pl
14c2758 @robinsmidsrod Added create_srt_from_sbv.pl helper script
robinsmidsrod authored
17 #
18
19 use strict;
20 use warnings;
21
22 use autodie;
23 use File::Find qw(find);
24
25 my @sbv_files = scalar @ARGV
26 ? ( -f $ARGV[0] ? @ARGV : find_sbv_files($ARGV[0]) )
27 : find_sbv_files('.');
28
29 foreach my $sbv_file ( @sbv_files ) {
30 eval {
31 my $srt_file = create_srt_from_sbv($sbv_file);
32 print "CREATED: $srt_file\n";
33 };
34 if ($@) {
35 print STDERR "FAILED: $sbv_file: $@";
36 }
37 }
38
39 exit;
40
41 # A File::Find rule to find all the .sbv files in all subdirectories underneath
42 # the current directory
43 # return an array of relative pathnames (to the root directory).
44 sub find_sbv_files {
45 my ($root_dir) = @_;
46 die("Please run this program from the root directory of the checkout.\n") unless -f $0;
47 my @found_files;
48 find(sub {
49 return unless -f $_; # Only process files, not directories
50 return unless -r $_; # Skip it if it is not readable
51 return unless $_ =~ /\.sbv$/i; # Skip it if it is not a .sbv file
52 push @found_files, $File::Find::name;
53 }, $root_dir);
54 return @found_files;
55 }
56
57 # Create and write out an .srt file with the same path as the .sbv file
58 sub create_srt_from_sbv {
59 my ($sbv_file) = @_;
60 die("Not an sbv file: $sbv_file\n") unless $sbv_file =~ /\.sbv$/i;
61 open(my $sbv, "<", $sbv_file); # Open .sbv file for input
62 (my $srt_file = $sbv_file) =~ s/sbv$/srt/i; # Change extension
63 open(my $srt, ">", $srt_file); # Open .srt file for output (forced overwrite)
64 my $counter = 1;
65 while ( my $line = <$sbv> ) {
66 # Only convert lines with timecodes
67 # They look like this:
68 # 0:00:00.000,0:00:00:000
69 if ( $line =~ /^(\d:\d{2}:\d{2}\.\d{3}),(\d:\d{2}:\d{2}\.\d{3})$/ ) {
70
71 # Get timecodes
72 my $start = $1;
73 my $stop = $2;
74
75 # Change them to .srt style
76 $start =~ s/\./,/;
77 $stop =~ s/\./,/;
78
79 print $srt "$counter\n"; # Add frame number in front of timecode
80 print $srt "$start --> $stop\n"; # SRT timecode format
81 $counter++;
82 }
83 else {
84 print $srt $line; # Any other line is just printed
85 }
86 }
87 close($srt);
88 close($sbv);
89 return $srt_file; # Return the name of the .srt file created
90 }
Something went wrong with that request. Please try again.