Skip to content

Commit

Permalink
- Added the "--srt-languages=s" command-line option.
Browse files Browse the repository at this point in the history
It takes a list of comma-separated preferred language codes. It's particularily useful in combination with "--auto-captions" option to load auto-translated subtitles in a specific language, without editing the configuration file.

Example:

	# Romanian auto-translated subtitles
	youtube-viewer --srt-languages=ro --auto-captions https://www.youtube.com/watch?v=iZnLZFRylbs
  • Loading branch information
trizen committed Mar 2, 2021
1 parent e92b0f2 commit 2116147
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
11 changes: 10 additions & 1 deletion bin/youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ usage: $execname [options] ([url] | [keywords])
* Closed-captions
--get-captions! : download closed-captions for videos
--auto-captions! : include or exclude auto-generated captions
--srt-languages=s : comma separated list of preferred languages
* Config
--config=s : configuration file
Expand Down Expand Up @@ -1782,6 +1783,7 @@ sub parse_arguments {
'thousand-separator=s' => \$opt{thousand_separator},
'get-captions|get_captions!' => \$opt{get_captions},
'auto-captions|auto_captions!' => \$opt{auto_captions},
'srt-languages=s' => \$opt{srt_languages},
'copy-caption|copy_caption!' => \$opt{copy_caption},
'skip-if-exists|skip_if_exists!' => \$opt{skip_if_exists},
'downloads-dir|download-dir=s' => \$opt{downloads_dir},
Expand Down Expand Up @@ -3676,11 +3678,18 @@ sub get_streaming_url {
my $srt_file;
if (ref($captions) eq 'ARRAY' and @$captions and $opt{get_captions} and not $opt{novideo}) {
require WWW::YoutubeViewer::GetCaption;

my $languages = $opt{srt_languages};

if (ref($languages) ne 'ARRAY') {
$languages = [grep { /[a-z]/i } split(/\s*,\s*/, $languages)];
}

my $yv_cap = WWW::YoutubeViewer::GetCaption->new(
auto_captions => $opt{auto_captions},
captions_dir => $opt{cache_dir},
captions => $captions,
languages => $CONFIG{srt_languages},
languages => $languages,
yv_obj => $yv_obj,
);
$srt_file = $yv_cap->save_caption($video_id);
Expand Down
1 change: 0 additions & 1 deletion lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,6 @@ sub get_streaming_urls {
push @caption_urls, @{$caption_data->{captions}{playerCaptionsTracklistRenderer}{captionTracks}};

my $translationLanguages = $caption_data->{captions}{playerCaptionsTracklistRenderer}{translationLanguages};

if (ref($translationLanguages) eq 'ARRAY') {
foreach my $caption (@caption_urls) {
$caption->{translationLanguages} = $translationLanguages;
Expand Down
19 changes: 6 additions & 13 deletions lib/WWW/YoutubeViewer/GetCaption.pm
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ sub find_caption_data {

foreach my $lang (@{$self->{languages}}) {
foreach my $trans_lang (@{$caption->{translationLanguages}}) {

ref($trans_lang) eq 'HASH' or next;

if ($trans_lang->{languageCode} =~ /^\Q$lang\E(?:\z|[_-])/i) {
my %caption_copy = %{$caption};
$caption_copy{languageCode} = $trans_lang->{languageCode};
Expand Down Expand Up @@ -215,17 +218,6 @@ sub xml2srt {
return join("\n\n", @text);
}

=head2 get_xml_data($caption_data)
Get the XML content for a given caption data.
=cut

sub get_xml_data {
my ($self, $url) = @_;
$self->{yv_obj}->lwp_get($url, simple => 1);
}

=head2 save_caption($video_ID)
Save the caption in a .srt file and return its file path.
Expand All @@ -246,8 +238,9 @@ sub save_caption {
return $srt_file if (-e $srt_file);

# Get XML data, then transform it to SubRip data
my $xml = $self->get_xml_data($info->{baseUrl} // return) // return;
my $srt = $self->xml2srt($xml) // return;
my $url = $info->{baseUrl} // return;
my $xml = $self->{yv_obj}->lwp_get($url, simple => 1) // return;
my $srt = $self->xml2srt($xml) // return;

# Write the SubRib data to the $srt_file
open(my $fh, '>:utf8', $srt_file) or return;
Expand Down

0 comments on commit 2116147

Please sign in to comment.