HTML5Media.pm 9.74 KB
Newer Older
1 2
package C4::HTML5Media;

3
# Copyright 2012/2015 Mirko Tietgen
4 5 6
#
# This file is part of Koha.
#
7 8 9 10
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
11
#
12 13 14 15
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
16
#
17 18
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19 20 21 22 23 24

use strict;
use warnings;

use C4::Context;
use MARC::Field;
25
use Koha::UploadedFiles;
26 27 28 29 30 31 32 33 34 35 36 37 38

=head1 HTML5Media

C4::HTML5Media

=head1 Description

This module gets the relevant data from field 856 (MARC21/UNIMARC) to create a HTML5 audio or video element containing the file(s) catalogued in 856.

=cut

=head2 gethtml5media

39
Get all relevant data from field 856. Takes a $record in the subroutine call, sets appropriate params.
40 41 42 43 44 45 46 47 48 49 50

=cut

sub gethtml5media {
    my $self = shift;
    my $record = shift;
    my @HTML5Media_sets = ();
    my @HTML5Media_fields = $record->field(856);
    my $HTML5MediaParent;
    my $HTML5MediaWidth;
    my @HTML5MediaExtensions = split( /\|/, C4::Context->preference("HTML5MediaExtensions") );
51
    my $HTML5MediaYouTube    = C4::Context->preference("HTML5MediaYouTube");
52
    my $marcflavour          = C4::Context->preference("marcflavour");
53
    my $isyoutube            = 0;
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 91 92 93 94 95 96 97 98 99 100
    foreach my $HTML5Media_field (@HTML5Media_fields) {
        my %HTML5Media;
        # protocol
        if ( $HTML5Media_field->indicator(1) eq '1' ) {
            $HTML5Media{protocol} = 'ftp';
        }
        elsif ( $HTML5Media_field->indicator(1) eq '4' ) {
            $HTML5Media{protocol} = 'http';
        }
        elsif ( $HTML5Media_field->indicator(1) eq '7' ) {
            if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
                $HTML5Media{protocol} = $HTML5Media_field->subfield('2');
            }
            elsif ($marcflavour eq 'UNIMARC') {
                $HTML5Media{protocol} = $HTML5Media_field->subfield('y');
            }
        }
        else {
            $HTML5Media{protocol} = 'http';
        }
        # user
        if ( $HTML5Media_field->subfield('l') ) {
            $HTML5Media{username} = $HTML5Media_field->subfield('l'); # yes, that is arbitrary if h and l are not the same. originally i flipped a coin in that case.
        }
        elsif ( $HTML5Media_field->subfield('h') ) {
            $HTML5Media{username} = $HTML5Media_field->subfield('h');
        }
        # user/pass
        if ( $HTML5Media{username} && $HTML5Media_field->subfield('k') ) {
            $HTML5Media{loginblock} = $HTML5Media{username} . ':' . $HTML5Media_field->subfield('k') . '@';
        }
        elsif ( $HTML5Media{username} ) {
            $HTML5Media{loginblock} = $HTML5Media{username} . '@';
        }
        else {
            $HTML5Media{loginblock} = '';
        }
        # port
        if ( $HTML5Media_field->subfield('p') ) {
            $HTML5Media{portblock} = ':' . $HTML5Media_field->subfield('k');
        }
        else {
            $HTML5Media{portblock} = '';
        }
        # src
        if ( $HTML5Media_field->subfield('u') ) {
            $HTML5Media{srcblock} = $HTML5Media_field->subfield('u');
101
            if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) {
102
                if ($HTML5MediaYouTube == 1) {
103 104
                    require WWW::YouTube::Download;
                    import  WWW::YouTube::Download qw(playback_url);
105
                    my $youtube           = WWW::YouTube::Download->new;
106 107 108 109 110 111 112 113 114
                    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;}
115 116 117 118 119
                }
               else {
                   next; # do not embed youtube videos
               }
            }
120 121 122 123 124
        }
        elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
            $HTML5Media{host}        = $HTML5Media_field->subfield('a');
            $HTML5Media{host}        =~ s/(^\/|\/$)//g;
            $HTML5Media{path}        = $HTML5Media_field->subfield('d');
125
            $HTML5Media{path}        =~ s/(^\/|\/$)//g; # TODO we could check for youtube here too, but nobody uses these fields anyway…
126 127 128 129 130 131 132
            $HTML5Media{file}        = $HTML5Media_field->subfield('f');
            $HTML5Media{srcblock}    = $HTML5Media{protocol} . '://' . $HTML5Media{loginblock} . $HTML5Media{host} . $HTML5Media{portblock} . '/' . $HTML5Media{path} . '/' . $HTML5Media{file};
        }
        else {
            next; # no file to play
        }
        # extension
133 134
        # check uploaded files
        if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
135 136
            my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
            next if !$id;
137 138 139 140 141 142
            my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
            my $upload = Koha::UploadedFiles->search({
                hashvalue => $id, %public,
            })->next;
            next if !$upload || $upload->filename !~ /\./;
            $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
143 144 145 146 147
        }
        # check remote files
        else {
            $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
        }
148
        if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
149 150
            next; # not a specified media file
        }
151 152 153 154
        # youtube
        if ($isyoutube == 1) {
                $HTML5Media{mime} = 'video/webm';
        }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        # mime
        if ( $HTML5Media_field->subfield('c') ) {
            $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
        }
        ### from subfield q…
        if ( $HTML5Media_field->subfield('q') ) {
            $HTML5Media{mime} = $HTML5Media_field->subfield('q');
        }
        ### …or from file extension and codecs…
        elsif ( $HTML5Media{codecs} ) {
            if ( $HTML5Media{codecs} =~ /theora.*vorbis/ ) {
                $HTML5Media{mime} = 'video/ogg';
            }
            elsif ( $HTML5Media{codecs} =~ /vp8.*vorbis/ ) {
                $HTML5Media{mime} = 'video/webm';
            }
            elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'ogg') ) {
                $HTML5Media{mime} = 'audio/ogg';
            }
            elsif ( ($HTML5Media{codecs} =~ /^vorbis$/) && ($HTML5Media{extension} eq 'webm') ) {
                $HTML5Media{mime} = 'audio/webm';
            }
        }
        ### …or just from file extension
        else {
            if ( $HTML5Media{extension} eq 'ogv' ) {
                $HTML5Media{mime} = 'video/ogg';
                $HTML5Media{codecs} = 'theora,vorbis';
            }
            if ( $HTML5Media{extension} eq 'oga' ) {
                $HTML5Media{mime} = 'audio/ogg';
186
                $HTML5Media{codecs} = 'vorbis';
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
            }
            elsif ( $HTML5Media{extension} eq 'spx' ) {
                $HTML5Media{mime} = 'audio/ogg';
                $HTML5Media{codecs} = 'speex';
            }
            elsif ( $HTML5Media{extension} eq 'opus' ) {
                $HTML5Media{mime} = 'audio/ogg';
                $HTML5Media{codecs} = 'opus';
            }
            elsif ( $HTML5Media{extension} eq 'vtt' ) {
                $HTML5Media{mime} = 'text/vtt';
            }
        }
        # codecs
        if ( $HTML5Media{codecs} ) {
            $HTML5Media{codecblock} = '; codecs="' . $HTML5Media{codecs} . '"';
        }
        else {
            $HTML5Media{codecblock} = '';
        }
        # type
        if ( $HTML5Media{mime} ) {
            $HTML5Media{typeblock} = ' type=\'' . $HTML5Media{mime} . $HTML5Media{codecblock} . '\'';
        }
        else {
          $HTML5Media{typeblock} = '';
        }
        # element
        if ( $HTML5Media{mime} =~ /audio/ ) {
            $HTML5Media{type} = 'audio';
        }
        elsif ( $HTML5Media{mime} =~ /video/ ) {
            $HTML5Media{type} = 'video';
        }
        elsif ( $HTML5Media{mime} =~ /text/ ) {
            $HTML5Media{type} = 'track';
        }
        # push
        if ( $HTML5Media{srcblock} && $HTML5Media{type} ) {
            push (@HTML5Media_sets, \%HTML5Media);
        }
    }
    # parent element
230 231
    for my $media ( @HTML5Media_sets ) {
        if ( ($media->{mime}) && ($media->{mime} =~ /audio/) ) {
232 233 234 235 236
            if ( $HTML5MediaParent ne 'video' ) {
                $HTML5MediaParent = 'audio';
                $HTML5MediaWidth = '';
            }
        }
237
        elsif ( ($media->{mime}) && ($media->{mime} =~ /video/) ) {
238 239 240 241 242
            $HTML5MediaParent = 'video';
            $HTML5MediaWidth = ' width="480"';
        }
    }
    # child element
243 244 245 246
    for my $media ( @HTML5Media_sets ) {
        if ( ($media->{type}) && ( ($media->{type} eq 'video') || ($media->{type} eq 'audio') ) ) {
            if ( $media->{type} eq $HTML5MediaParent ) {
                $media->{child} = 'source';
247 248 249
            }
        }
        else {
250
            $media->{child} = $media->{type};
251 252
        }
    }
253 254 255 256 257 258 259

    return (
        HTML5MediaEnabled  => ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
        HTML5MediaSets     => \@HTML5Media_sets,
        HTML5MediaParent   => $HTML5MediaParent,
        HTML5MediaWidth    => $HTML5MediaWidth,
    );
260 261 262
}

1;