Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added options and preferences for subtitle support.

Also added code to convert SAMI subtitles to SubRip format.
  • Loading branch information...
commit 35a9ec38fdddc56d87ccec0cee866b0fc5f93c42 1 parent c358d72
@zakflash zakflash authored
View
70 FlashVideo/Utils.pm
@@ -14,7 +14,8 @@ use constant MAX_REDIRECTS => 5;
our @EXPORT = qw(debug info error
extract_title extract_info title_to_filename get_video_filename url_exists
swfhash swfhash_data EXTENSIONS get_user_config_dir get_win_codepage
- is_program_on_path get_terminal_width json_unescape);
+ is_program_on_path get_terminal_width json_unescape
+ convert_sami_subtitles_to_srt);
my $HAS_READKEY = eval { require Term::ReadKey };
@@ -273,4 +274,71 @@ sub json_unescape {
return $s;
}
+sub convert_sami_subtitles_to_srt {
+ my ($sami_subtitles, $filename) = @_;
+
+ die "SAMI subtitles must be provided" unless $sami_subtitles;
+ die "Output SRT filename must be provided" unless $filename;
+
+ my $parser = HTML::TokeParser->new(\$sami_subtitles);
+
+ my @subtitles;
+ my $count = 0;
+
+ while (my $token = $parser->get_token()) {
+ my ($start_or_end, $tag, $attributes) = @$token;
+
+ if ($start_or_end eq 'S' and $tag eq 'sync') {
+ my $begin = $attributes->{start};
+
+ my $subtitle_text = $parser->get_trimmed_text('sync');
+ $subtitle_text =~ s/\xA0/ /g; # convert non-breaking space to normal
+
+ # Convert milliseconds into HH:MM:ss,mmm format
+ my $seconds = int($begin / 1000);
+ my $milliseconds = $begin - ($seconds * 1000);
+ $begin = sprintf("%02d:%02d:%02d,%03d", (gmtime($seconds))[2,1,0],
+ $milliseconds);
+
+ if ($count and !$subtitles[$count - 1]->{end}) {
+ $subtitles[$count - 1]->{end} = $begin;
+ }
+
+ # SAMI subtitles are a bit crap. Only a start time is specified for
+ # each subtitle. No end time is specified, so the subtitle is displayed
+ # until the next subtitle is ready to be shown. This means that if
+ # subtitles aren't meant to be shown for part of the video, a dummy
+ # subtitle (usually just a space) has to be inserted.
+ if (!$subtitle_text or $subtitle_text eq ' ') {
+ # Gap in subtitles.
+ next; # this is not a meaningful subtitle
+ }
+
+ push @subtitles, {
+ start => $begin,
+ text => $subtitle_text,
+ };
+
+ $count++;
+ }
+ }
+
+ $subtitles[$count - 1]->{end} = $subtitles[$count - 1]{start};
+
+ # Write subtitles
+ open my $subtitle_fh, '>', $filename
+ or die "Can't open subtitles file $filename: $!";
+
+ $count = 1;
+ foreach my $subtitle (@subtitles) {
+ print $subtitle_fh "$count\n$subtitle->{start} --> $subtitle->{end}\n" .
+ "$subtitle->{text}\n\n";
+ $count++;
+ }
+
+ close $subtitle_fh;
+
+ return 1;
+}
+
1;
View
6 FlashVideo/VideoPreferences.pm
@@ -9,6 +9,7 @@ sub new {
return bless {
quality => $opt{quality} || "high",
+ subtitles => $opt{subtitles} || 0,
}, $class;
}
@@ -18,4 +19,9 @@ sub quality {
return FlashVideo::VideoPreferences::Quality->new($self->{quality});
}
+sub subtitles {
+ my($self) = @_;
+
+ return $self->{subtitles};
+}
1;
View
3  get_flash_videos
@@ -64,6 +64,7 @@ BEGIN {
debug => 0,
quiet => 0,
quality => "high",
+ subtitles => 0,
);
}
@@ -87,6 +88,7 @@ Options:
-p --play Start playing the video once enough has been downloaded.
--player Player to use for the video (default: $opt{player}).
--proxy Proxy to use, use host:port for SOCKS, or URL for HTTP.
+ --subtitles Download subtitles where available.
-q --quiet Be quiet (only print errors).
-r --quality Quality to download at (high|medium|low, or site specific).
-u --update Update to latest version.
@@ -130,6 +132,7 @@ GetOptions(
"quiet|q" => \$opt{quiet},
"add-plugin=s" => \$opt{add_plugin},
"quality|r=s" => \$opt{quality},
+ "subtitles" => \$opt{subtitles},
) or die "Try $0 --help for more information.\n";
if($opt{version}) {
View
6 get_flash_videos.pod
@@ -138,6 +138,12 @@ C<rtmpdump> which supports SOCKS proxying.
If no proxy is specified on the command line or the config file the
C<$HTTP_PROXY> environment variable will be used.
+=item I<--subtitles>
+
+Download subtitles for the video, if available. Subtitles are converted to
+SubRip format and saved to a file of the same name as the video file, but
+with an extension of 'srt'.
+
=item I<-q>, I<--quiet>
Be quiet, only print errors.
Please sign in to comment.
Something went wrong with that request. Please try again.