1 package C4
::HTML5Media
;
3 # Copyright 2012/2015 Mirko Tietgen
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use Koha
::UploadedFiles
;
33 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.
39 Get all relevant data from field 856. Takes a $record in the subroutine call, sets appropriate params.
46 my @HTML5Media_sets = ();
47 my @HTML5Media_fields = $record->field(856);
50 my @HTML5MediaExtensions = split( /\|/, C4
::Context
->preference("HTML5MediaExtensions") );
51 my $HTML5MediaYouTube = C4
::Context
->preference("HTML5MediaYouTube");
52 my $marcflavour = C4
::Context
->preference("marcflavour");
53 foreach my $HTML5Media_field (@HTML5Media_fields) {
57 if ( $HTML5Media_field->indicator(1) eq '1' ) {
58 $HTML5Media{protocol
} = 'ftp';
60 elsif ( $HTML5Media_field->indicator(1) eq '4' ) {
61 $HTML5Media{protocol
} = 'http';
63 elsif ( $HTML5Media_field->indicator(1) eq '7' ) {
64 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
65 $HTML5Media{protocol
} = $HTML5Media_field->subfield('2');
67 elsif ($marcflavour eq 'UNIMARC') {
68 $HTML5Media{protocol
} = $HTML5Media_field->subfield('y');
72 $HTML5Media{protocol
} = 'http';
75 if ( $HTML5Media_field->subfield('l') ) {
76 $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.
78 elsif ( $HTML5Media_field->subfield('h') ) {
79 $HTML5Media{username
} = $HTML5Media_field->subfield('h');
82 if ( $HTML5Media{username
} && $HTML5Media_field->subfield('k') ) {
83 $HTML5Media{loginblock
} = $HTML5Media{username
} . ':' . $HTML5Media_field->subfield('k') . '@';
85 elsif ( $HTML5Media{username
} ) {
86 $HTML5Media{loginblock
} = $HTML5Media{username
} . '@';
89 $HTML5Media{loginblock
} = '';
92 if ( $HTML5Media_field->subfield('p') ) {
93 $HTML5Media{portblock
} = ':' . $HTML5Media_field->subfield('k');
96 $HTML5Media{portblock
} = '';
99 if ( $HTML5Media_field->subfield('u') ) {
100 $HTML5Media{srcblock
} = $HTML5Media_field->subfield('u');
101 if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) {
102 if ($HTML5MediaYouTube == 1) {
103 require WWW
::YouTube
::Download
;
104 import WWW
::YouTube
::Download
qw(playback_url);
105 my $youtube = WWW
::YouTube
::Download
->new;
107 $HTML5Media{srcblock
} = $youtube->playback_url(
108 $HTML5Media_field->subfield('u'), {
109 'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
114 else { $isyoutube = 1;}
117 next; # do not embed youtube videos
121 elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
122 $HTML5Media{host
} = $HTML5Media_field->subfield('a');
123 $HTML5Media{host
} =~ s/(^\/|\/$)//g;
124 $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 $HTML5Media{file
} = $HTML5Media_field->subfield('f');
127 $HTML5Media{srcblock
} = $HTML5Media{protocol
} . '://' . $HTML5Media{loginblock
} . $HTML5Media{host
} . $HTML5Media{portblock
} . '/' . $HTML5Media{path
} . '/' . $HTML5Media{file
};
130 next; # no file to play
133 # check uploaded files
134 if ( $HTML5Media{srcblock
} =~ /\Qopac-retrieve-file.pl\E/ ) {
135 my ( undef, $id ) = split /id=/, $HTML5Media{srcblock
};
137 my %public = ( ( caller )[1] =~ /opac/ ) ?
( public
=> 1 ): ();
138 my $upload = Koha
::UploadedFiles
->search({
139 hashvalue
=> $id, %public,
141 next if !$upload || $upload->filename !~ /\./;
142 $HTML5Media{extension
} = ( $upload->filename =~ m/([^.]+)$/ )[0];
146 $HTML5Media{extension
} = ($HTML5Media{srcblock
} =~ m/([^.]+)$/)[0];
148 if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
149 next; # not a specified media file
152 if ($isyoutube == 1) {
153 $HTML5Media{mime
} = 'video/webm';
156 if ( $HTML5Media_field->subfield('c') ) {
157 $HTML5Media{codecs
} = $HTML5Media_field->subfield('c');
160 if ( $HTML5Media_field->subfield('q') ) {
161 $HTML5Media{mime
} = $HTML5Media_field->subfield('q');
163 ### …or from file extension and codecs…
164 elsif ( $HTML5Media{codecs
} ) {
165 if ( $HTML5Media{codecs
} =~ /theora.*vorbis/ ) {
166 $HTML5Media{mime
} = 'video/ogg';
168 elsif ( $HTML5Media{codecs
} =~ /vp8.*vorbis/ ) {
169 $HTML5Media{mime
} = 'video/webm';
171 elsif ( ($HTML5Media{codecs
} =~ /^vorbis$/) && ($HTML5Media{extension
} eq 'ogg') ) {
172 $HTML5Media{mime
} = 'audio/ogg';
174 elsif ( ($HTML5Media{codecs
} =~ /^vorbis$/) && ($HTML5Media{extension
} eq 'webm') ) {
175 $HTML5Media{mime
} = 'audio/webm';
178 ### …or just from file extension
180 if ( $HTML5Media{extension
} eq 'ogv' ) {
181 $HTML5Media{mime
} = 'video/ogg';
182 $HTML5Media{codecs
} = 'theora,vorbis';
184 if ( $HTML5Media{extension
} eq 'oga' ) {
185 $HTML5Media{mime
} = 'audio/ogg';
186 $HTML5Media{codecs
} = 'vorbis';
188 elsif ( $HTML5Media{extension
} eq 'spx' ) {
189 $HTML5Media{mime
} = 'audio/ogg';
190 $HTML5Media{codecs
} = 'speex';
192 elsif ( $HTML5Media{extension
} eq 'opus' ) {
193 $HTML5Media{mime
} = 'audio/ogg';
194 $HTML5Media{codecs
} = 'opus';
196 elsif ( $HTML5Media{extension
} eq 'vtt' ) {
197 $HTML5Media{mime
} = 'text/vtt';
201 if ( $HTML5Media{codecs
} ) {
202 $HTML5Media{codecblock
} = '; codecs="' . $HTML5Media{codecs
} . '"';
205 $HTML5Media{codecblock
} = '';
208 if ( $HTML5Media{mime
} ) {
209 $HTML5Media{typeblock
} = ' type=\'' . $HTML5Media{mime
} . $HTML5Media{codecblock
} . '\'';
212 $HTML5Media{typeblock
} = '';
215 if ( $HTML5Media{mime
} =~ /audio/ ) {
216 $HTML5Media{type
} = 'audio';
218 elsif ( $HTML5Media{mime
} =~ /video/ ) {
219 $HTML5Media{type
} = 'video';
221 elsif ( $HTML5Media{mime
} =~ /text/ ) {
222 $HTML5Media{type
} = 'track';
225 if ( $HTML5Media{srcblock
} && $HTML5Media{type
} ) {
226 push (@HTML5Media_sets, \
%HTML5Media);
230 for my $media ( @HTML5Media_sets ) {
231 if ( ($media->{mime
}) && ($media->{mime
} =~ /audio/) ) {
232 if ( $HTML5MediaParent ne 'video' ) {
233 $HTML5MediaParent = 'audio';
234 $HTML5MediaWidth = '';
237 elsif ( ($media->{mime
}) && ($media->{mime
} =~ /video/) ) {
238 $HTML5MediaParent = 'video';
239 $HTML5MediaWidth = ' width="480"';
243 for my $media ( @HTML5Media_sets ) {
244 if ( ($media->{type
}) && ( ($media->{type
} eq 'video') || ($media->{type
} eq 'audio') ) ) {
245 if ( $media->{type
} eq $HTML5MediaParent ) {
246 $media->{child
} = 'source';
250 $media->{child
} = $media->{type
};
255 HTML5MediaEnabled
=> ( (scalar(@HTML5Media_sets) > 0) && ($HTML5MediaParent) ),
256 HTML5MediaSets
=> \
@HTML5Media_sets,
257 HTML5MediaParent
=> $HTML5MediaParent,
258 HTML5MediaWidth
=> $HTML5MediaWidth,