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