From baa25a7b540798a3be5a2b18c8d17ad074592409 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 May 2020 12:54:50 +0200 Subject: [PATCH] [19.11.x] Bug 20783: Use iframe to embed Youtube videos WWW::YouTube::Download is broken and not reliable. Other alternative was to use HTML::Video::Embed but not updated since years. The best alternative seems to follow youtube advise and use an iframe https://developers.google.com/youtube/iframe_api_reference Test plan: Put youtube video in 856$u (using different url formats, youtu.be, youtube.com/embed, etc.) Enable HTML5MediaEnabled and HTML5MediaYouTube and confirm that the youtube videos are correctly embeded. Signed-off-by: Kelly McElligott Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/HTML5Media.pm | 29 +++++++++---------- .../prog/en/modules/catalogue/detail.tt | 15 ++++++---- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/C4/HTML5Media.pm b/C4/HTML5Media.pm index c77e8a40bc..04fc263169 100644 --- a/C4/HTML5Media.pm +++ b/C4/HTML5Media.pm @@ -51,7 +51,7 @@ sub gethtml5media { my $HTML5MediaYouTube = C4::Context->preference("HTML5MediaYouTube"); my $marcflavour = C4::Context->preference("marcflavour"); foreach my $HTML5Media_field (@HTML5Media_fields) { - my $isyoutube = 0; + my $is_youtube = 0; my %HTML5Media; # protocol if ( $HTML5Media_field->indicator(1) eq '1' ) { @@ -100,18 +100,14 @@ sub gethtml5media { $HTML5Media{srcblock} = $HTML5Media_field->subfield('u'); if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) { if ($HTML5MediaYouTube == 1) { - require WWW::YouTube::Download; - import WWW::YouTube::Download qw(playback_url); - my $youtube = WWW::YouTube::Download->new; - eval { - $HTML5Media{srcblock} = $youtube->playback_url( - $HTML5Media_field->subfield('u'), { - 'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities - } - ); - }; - if ($@) { warn $@; } - else { $isyoutube = 1;} + my $url = $HTML5Media_field->subfield('u'); + # Credit for regex goes to https://stackoverflow.com/questions/3452546/how-do-i-get-the-youtube-video-id-from-a-url + next unless $url =~ m{^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*}; + my $video_id = $2; + next unless length($video_id) == 11; # Youtube video ids are 11 chars length + $HTML5Media{srcblock} = sprintf '%s://www.youtube.com/embed/%s', $HTML5Media{protocol}, $video_id; + $HTML5Media{is_youtube} = 1; + $is_youtube = 1; } else { next; # do not embed youtube videos @@ -145,12 +141,13 @@ sub gethtml5media { else { $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0]; } - if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) { + if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $is_youtube != 1) ) { next; # not a specified media file } # youtube - if ($isyoutube == 1) { - $HTML5Media{mime} = 'video/webm'; + if ($is_youtube == 1) { + $HTML5Media{mime} = 'video/webm'; + $HTML5Media{type} = 'video'; } # mime if ( $HTML5Media_field->subfield('c') ) { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt index 24319f7dcd..a81c7fd0a5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt @@ -193,7 +193,7 @@ [% END %] [% END %] -[% IF ( HTML5MediaEnabled ) %][% IF ( HTML5MediaSets ) %]
  • Play media
  • [% END %][% END %] +[% IF HTML5MediaEnabled && HTML5MediaSets.size %]
  • Play media
  • [% END %] [% IF ( Koha.Preference('NovelistSelectStaffEnabled') && Koha.Preference('NovelistSelectStaffProfile') && Koha.Preference('NovelistSelectStaffView') == 'tab' ) %] [% END %] @@ -724,10 +724,15 @@
    [% FOREACH HTML5MediaSet IN HTML5MediaSets %]

    - <[% HTML5MediaParent | html %] controls preload=none> - <[% HTML5MediaSet.child | html %] src="[% HTML5MediaSet.srcblock | html %]"[% HTML5MediaSet.typeblock | html %] /> - [[% HTML5MediaParent | html %] tag not supported by your browser.] - + [% IF HTML5MediaSet.is_youtube %] + + [% ELSE %] + <[% HTML5MediaParent | html %] controls preload=none> + <[% HTML5MediaSet.child | html %] src="[% HTML5MediaSet.srcblock | url %]"[% HTML5MediaSet.typeblock | html %] /> + [[% HTML5MediaParent | html %] tag not supported by your browser.] + + [% END %]

    [% END %]
    -- 2.28.0