Index: trunk/extensions/OggHandler/OggHandler.php |
— | — | @@ -1,421 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -class OggHandler extends MediaHandler { |
5 | | - const OGG_METADATA_VERSION = 1; |
6 | | - |
7 | | - var $videoTypes = array( 'Theora' ); |
8 | | - var $audioTypes = array( 'Vorbis', 'Speex', 'FLAC' ); |
9 | | - |
10 | | - function isEnabled() { |
11 | | - return true; |
12 | | - } |
13 | | - |
14 | | - function getParamMap() { |
15 | | - // TODO: add thumbtime, noplayer |
16 | | - return array( 'img_width' => 'width' ); |
17 | | - } |
18 | | - |
19 | | - function validateParam( $name, $value ) { |
20 | | - // TODO |
21 | | - return true; |
22 | | - } |
23 | | - |
24 | | - function makeParamString( $params ) { |
25 | | - // No parameters just yet, the thumbnails are always full-size |
26 | | - return ''; |
27 | | - /* |
28 | | - $s = ''; |
29 | | - foreach ( $params as $name => $value ) { |
30 | | - if ( $s !== '' ) { |
31 | | - $s .= '-'; |
32 | | - } |
33 | | - $s .= "$name=$value"; |
34 | | - }*/ |
35 | | - } |
36 | | - |
37 | | - function parseParamString( $str ) { |
38 | | - // TODO |
39 | | - return array(); |
40 | | - } |
41 | | - |
42 | | - function normaliseParams( $image, &$params ) { |
43 | | - // TODO |
44 | | - return true; |
45 | | - } |
46 | | - |
47 | | - function getImageSize( $file, $path, $metadata = false ) { |
48 | | - // Just return the size of the first video stream |
49 | | - if ( $metadata === false ) { |
50 | | - $metadata = $file->getMetadata(); |
51 | | - } |
52 | | - $metadata = $this->unpackMetadata( $metadata ); |
53 | | - if ( isset( $metadata['error'] ) ) { |
54 | | - return false; |
55 | | - } |
56 | | - foreach ( $metadata['streams'] as $stream ) { |
57 | | - if ( in_array( $stream['type'], $this->videoTypes ) ) { |
58 | | - return array( |
59 | | - $stream['header']['PICW'], |
60 | | - $stream['header']['PICH'] |
61 | | - ); |
62 | | - } |
63 | | - } |
64 | | - return array( false, false ); |
65 | | - } |
66 | | - |
67 | | - function getMetadata( $image, $path ) { |
68 | | - $metadata = array( 'version' => self::OGG_METADATA_VERSION ); |
69 | | - |
70 | | - if ( !class_exists( 'File_Ogg' ) ) { |
71 | | - return require( 'File/Ogg.php' ); |
72 | | - } |
73 | | - |
74 | | - try { |
75 | | - $f = new File_Ogg( $path ); |
76 | | - $streams = array(); |
77 | | - foreach ( $f->listStreams() as $streamType => $streamIDs ) { |
78 | | - foreach ( $streamIDs as $streamID ) { |
79 | | - $stream = $f->getStream( $streamID ); |
80 | | - $streams[$streamID] = array( |
81 | | - 'serial' => $stream->getSerial(), |
82 | | - 'group' => $stream->getGroup(), |
83 | | - 'type' => $stream->getType(), |
84 | | - 'vendor' => $stream->getVendor(), |
85 | | - 'length' => $stream->getLength(), |
86 | | - 'size' => $stream->getSize(), |
87 | | - 'header' => $stream->getHeader(), |
88 | | - 'comments' => $stream->getComments() |
89 | | - ); |
90 | | - } |
91 | | - } |
92 | | - $metadata['streams'] = $streams; |
93 | | - $metadata['length'] = $f->getLength(); |
94 | | - } catch ( PEAR_Exception $e ) { |
95 | | - // File not found, invalid stream, etc. |
96 | | - $metadata['error'] = array( |
97 | | - 'message' => $e->getMessage(), |
98 | | - 'code' => $e->getCode() |
99 | | - ); |
100 | | - } |
101 | | - return serialize( $metadata ); |
102 | | - } |
103 | | - |
104 | | - function unpackMetadata( $metadata ) { |
105 | | - $unser = @unserialize( $metadata ); |
106 | | - if ( isset( $unser['version'] ) && $unser['version'] == self::OGG_METADATA_VERSION ) { |
107 | | - return $unser; |
108 | | - } else { |
109 | | - return false; |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - function getMetadataType( $image ) { |
114 | | - return 'ogg'; |
115 | | - } |
116 | | - |
117 | | - function isMetadataValid( $image, $metadata ) { |
118 | | - return $this->unpackMetadata( $metadata ) !== false; |
119 | | - } |
120 | | - |
121 | | - function getThumbType( $ext, $mime ) { |
122 | | - return array( 'jpg', 'image/jpeg' ); |
123 | | - } |
124 | | - |
125 | | - function doTransform( $file, $dstPath, $dstUrl, $params, $flags = 0 ) { |
126 | | - global $wgFFmpegLocation; |
127 | | - $width = $params['width']; |
128 | | - $srcWidth = $file->getWidth(); |
129 | | - $srcHeight = $file->getHeight(); |
130 | | - $height = $srcWidth == 0 ? $srcHeight : $width * $srcHeight / $srcWidth; |
131 | | - $length = $this->getLength( $file ); |
132 | | - |
133 | | - if ( $srcHeight == 0 || $srcWidth == 0 ) { |
134 | | - // Make audio player |
135 | | - if ( empty( $params['width'] ) ) { |
136 | | - $width = 200; |
137 | | - } else { |
138 | | - $width = $params['width']; |
139 | | - } |
140 | | - $height = 20; |
141 | | - return new OggAudioDisplay( $file->getURL(), $width, $height, $length ); |
142 | | - } |
143 | | - |
144 | | - if ( $flags & self::TRANSFORM_LATER ) { |
145 | | - return new OggVideoDisplay( $file->getURL(), $dstUrl, $width, $height, $length ); |
146 | | - } |
147 | | - |
148 | | - wfMkdirParents( dirname( $dstPath ) ); |
149 | | - |
150 | | - wfDebug( "Creating video thumbnail at $dstPath\n" ); |
151 | | - |
152 | | - $cmd = wfEscapeShellArg( $wgFFmpegLocation ) . |
153 | | - ' -i ' . wfEscapeShellArg( $file->getPath() ) . |
154 | | - # MJPEG, that's the same as JPEG except it's supported by the windows build of ffmpeg |
155 | | - # No audio, one frame |
156 | | - ' -f mjpeg -an -vframes 1' . |
157 | | - # Seek to midpoint, it tends to be more interesting than the fade in at the start |
158 | | - ' -ss ' . intval( $length / 2 ) . ' ' . |
159 | | - wfEscapeShellArg( $dstPath ) . ' 2>&1'; |
160 | | - |
161 | | - $retval = 0; |
162 | | - $returnText = wfShellExec( $cmd, $retval ); |
163 | | - |
164 | | - if ( $retval ) { |
165 | | - // Filter nonsense |
166 | | - $lines = explode( "\n", str_replace( "\r\n", "\n", $returnText ) ); |
167 | | - if ( substr( $lines[0], 0, 6 ) == 'FFmpeg' ) { |
168 | | - for ( $i = 1; $i < count( $lines ); $i++ ) { |
169 | | - if ( substr( $lines[$i], 0, 2 ) != ' ' ) { |
170 | | - break; |
171 | | - } |
172 | | - } |
173 | | - $lines = array_slice( $lines, $i ); |
174 | | - } |
175 | | - // Return error box |
176 | | - return new MediaTransformError( 'thumbnail_error', $width, $height, implode( "\n", $lines ) ); |
177 | | - } |
178 | | - return new OggVideoDisplay( $file->getURL(), $dstUrl, $width, $height, $length ); |
179 | | - } |
180 | | - |
181 | | - function canRender() { return true; } |
182 | | - function mustRender( $file ) { return true; } |
183 | | - |
184 | | - /* |
185 | | - function formatMetadata( $image, $metadata ) { |
186 | | - if ( !$this->isMetadataValid( $image, $metadata ) ) { |
187 | | - return false; |
188 | | - } |
189 | | - $metadata = unserialize( $metadata ); |
190 | | - $formatted = array(); |
191 | | - if ( isset( $metadata['error'] ) ) { |
192 | | - self::addMeta( $formatted, 'visible', 'ogg', 'error', $metadata['error']['message'] ); |
193 | | - return $formatted; |
194 | | - } |
195 | | - $formatted = array(); |
196 | | - $n = 0; |
197 | | - foreach ( $metadata['streams'] as $stream ) { |
198 | | - $prefix = "Stream $n "; |
199 | | - $type = strtolower( $stream['type'] ); |
200 | | - self::addMeta( $formatted, 'visible', 'ogg', 'type', $stream['type'], $n ); |
201 | | - self::addMeta( $formatted, 'visible', 'ogg', 'vendor', $stream['vendor'], $n ); |
202 | | - self::addMeta( $formatted, 'visible', 'ogg', 'length', $stream['length'], $n ); |
203 | | - self::addMeta( $formatted, 'visible', 'ogg', 'size', $stream['size'], $n ); |
204 | | - |
205 | | - foreach ( $stream['header'] as $name => $value ) { |
206 | | - self::addMeta( $formatted, 'visible', $type, $name, $value, $n ); |
207 | | - $visible[$prefix . $name] = wfEscapeWikiText( $value ); |
208 | | - } |
209 | | - foreach ( $stream['comments'] as $name => $value ) { |
210 | | - self::addMeta( $formatted, 'visible', $type, $name, $value, $n ); |
211 | | - } |
212 | | - } |
213 | | - return $formatted; |
214 | | - }*/ |
215 | | - |
216 | | - function getLength( $file ) { |
217 | | - $metadata = $this->unpackMetadata( $file->getMetadata() ); |
218 | | - if ( !$metadata || isset( $metadata['error'] ) ) { |
219 | | - return 0; |
220 | | - } else { |
221 | | - return $metadata['length']; |
222 | | - } |
223 | | - } |
224 | | - |
225 | | - function getStreamTypes( $file ) { |
226 | | - $streamTypes = ''; |
227 | | - $metadata = $this->unpackMetadata( $file->getMetadata() ); |
228 | | - if ( !$metadata || isset( $metadata['error'] ) ) { |
229 | | - return false; |
230 | | - } |
231 | | - foreach ( $metadata['streams'] as $stream ) { |
232 | | - $streamTypes[$stream['type']] = true; |
233 | | - } |
234 | | - return array_keys( $streamTypes ); |
235 | | - } |
236 | | - |
237 | | - function getShortDesc( $file ) { |
238 | | - global $wgLang; |
239 | | - wfLoadExtensionMessages( 'OggHandler' ); |
240 | | - $streamTypes = $this->getStreamTypes( $file ); |
241 | | - if ( !$streamTypes ) { |
242 | | - return parent::getShortDesc( $file ); |
243 | | - } |
244 | | - if ( array_intersect( $streamTypes, $this->videoTypes ) ) { |
245 | | - // Count multiplexed audio/video as video for short descriptions |
246 | | - $msg = 'ogg-short-video'; |
247 | | - } elseif ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
248 | | - $msg = 'ogg-short-audio'; |
249 | | - } else { |
250 | | - $msg = 'ogg-short-general'; |
251 | | - } |
252 | | - return wfMsg( $msg, implode( '/', $streamTypes ), |
253 | | - $wgLang->formatTimePeriod( $this->getLength( $file ) ) ); |
254 | | - } |
255 | | - |
256 | | - function getLongDesc( $file ) { |
257 | | - global $wgLang; |
258 | | - wfLoadExtensionMessages( 'OggHandler' ); |
259 | | - $streamTypes = $this->getStreamTypes( $file ); |
260 | | - if ( !$streamTypes ) { |
261 | | - $unpacked = $this->unpackMetadata( $file->getMetadata() ); |
262 | | - return wfMsg( 'ogg-long-error', $unpacked['error']['message'] ); |
263 | | - } |
264 | | - if ( array_intersect( $streamTypes, $this->videoTypes ) ) { |
265 | | - if ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
266 | | - $msg = 'ogg-long-multiplexed'; |
267 | | - } else { |
268 | | - $msg = 'ogg-long-video'; |
269 | | - } |
270 | | - } elseif ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
271 | | - $msg = 'ogg-long-audio'; |
272 | | - } else { |
273 | | - $msg = 'ogg-long-general'; |
274 | | - } |
275 | | - $size = 0; |
276 | | - $unpacked = $this->unpackMetadata( $file->getMetadata() ); |
277 | | - if ( !$unpacked || isset( $metadata['error'] ) ) { |
278 | | - $length = 0; |
279 | | - } else { |
280 | | - $length = $this->getLength( $file ); |
281 | | - foreach ( $unpacked['streams'] as $stream ) { |
282 | | - $size += $stream['size']; |
283 | | - } |
284 | | - } |
285 | | - $bitrate = $length == 0 ? 0 : $size / $length * 8; |
286 | | - return wfMsg( $msg, implode( '/', $streamTypes ), |
287 | | - $wgLang->formatTimePeriod( $length ), |
288 | | - $wgLang->formatBitrate( $bitrate ), |
289 | | - $wgLang->formatNum( $file->getWidth() ), |
290 | | - $wgLang->formatNum( $file->getHeight() ) |
291 | | - ); |
292 | | - } |
293 | | - |
294 | | - function getDimensionsString( $file ) { |
295 | | - global $wgLang; |
296 | | - wfLoadExtensionMessages( 'OggHandler' ); |
297 | | - if ( $file->getWidth() ) { |
298 | | - return wfMsg( 'video-dims', $wgLang->formatTimePeriod( $this->getLength( $file ) ), |
299 | | - $wgLang->formatNum( $file->getWidth() ), |
300 | | - $wgLang->formatNum( $file->getHeight() ) ); |
301 | | - } else { |
302 | | - return $wgLang->formatTimePeriod( $this->getLength( $file ) ); |
303 | | - } |
304 | | - } |
305 | | - |
306 | | - function setHeaders( $out ) { |
307 | | - global $wgScriptPath; |
308 | | - if ( $out->hasHeadItem( 'OggHandler' ) ) { |
309 | | - return; |
310 | | - } |
311 | | - |
312 | | - wfLoadExtensionMessages( 'OggHandler' ); |
313 | | - |
314 | | - $msgNames = array( 'ogg-play', 'ogg-pause', 'ogg-stop', 'ogg-no-player', |
315 | | - 'ogg-player-videoElement', 'ogg-player-oggPlugin', 'ogg-player-cortado', 'ogg-player-vlcPlugin', |
316 | | - 'ogg-player-vlcActiveX', 'ogg-using-player' ); |
317 | | - $msgValues = array_map( 'wfMsg', $msgNames ); |
318 | | - $jsMsgs = Xml::encodeJsVar( (object)array_combine( $msgNames, $msgValues ) ); |
319 | | - $encCortadoUrl = Xml::encodeJsVar( "$wgScriptPath/extensions/OggHandler/cortado-ovt-stripped-0.2.2.jar" ); |
320 | | - |
321 | | - $out->addHeadItem( 'OggHandler', <<<EOT |
322 | | -<script type="text/javascript" src="$wgScriptPath/extensions/OggHandler/OggPlayer.js"></script> |
323 | | -<script type="text/javascript"> |
324 | | -wgOggPlayer.msg = $jsMsgs; |
325 | | -wgOggPlayer.cortadoUrl = $encCortadoUrl; |
326 | | -//wgOggPlayer.forcePlayer = 'cortado'; |
327 | | -</script> |
328 | | -EOT |
329 | | - ); |
330 | | - |
331 | | - } |
332 | | - |
333 | | - function parserTransformHook( $parser, $file ) { |
334 | | - if ( isset( $parser->mOutput->hasOggTransform ) ) { |
335 | | - return; |
336 | | - } |
337 | | - $parser->mOutput->hasOggTransform = true; |
338 | | - $parser->mOutput->addOutputHook( 'OggHandler' ); |
339 | | - } |
340 | | - |
341 | | - static function outputHook( $outputPage, $parserOutput, $data ) { |
342 | | - $instance = MediaHandler::getHandler( 'application/ogg' ); |
343 | | - if ( $instance ) { |
344 | | - $instance->setHeaders( $outputPage ); |
345 | | - } |
346 | | - } |
347 | | -} |
348 | | - |
349 | | -class OggTransformOutput extends MediaTransformOutput { |
350 | | - static $serial = 0; |
351 | | - |
352 | | - function __construct( $videoUrl, $thumbUrl, $width, $height, $length, $isVideo ) { |
353 | | - $this->videoUrl = $videoUrl; |
354 | | - $this->thumbUrl = $thumbUrl; |
355 | | - $this->width = round( $width ); |
356 | | - $this->height = round( $height ); |
357 | | - $this->length = round( $length ); |
358 | | - $this->isVideo = $isVideo; |
359 | | - } |
360 | | - |
361 | | - function toHtml( $attribs = array() , $linkAttribs = false ) { |
362 | | - wfLoadExtensionMessages( 'OggHandler' ); |
363 | | - |
364 | | - OggTransformOutput::$serial++; |
365 | | - |
366 | | - $encThumbUrl = htmlspecialchars( $this->thumbUrl ); |
367 | | - |
368 | | - if ( substr( $this->videoUrl, 0, 4 ) != 'http' ) { |
369 | | - global $wgServer; |
370 | | - $encUrl = Xml::encodeJsVar( $wgServer . $this->videoUrl ); |
371 | | - } else { |
372 | | - $encUrl = Xml::encodeJsVar( $this->videoUrl ); |
373 | | - } |
374 | | - #$encUrl = htmlspecialchars( $encUrl ); |
375 | | - $length = intval( $this->length ); |
376 | | - $width = intval( $this->width ); |
377 | | - $height = intval( $this->height ); |
378 | | - if ( $this->isVideo ) { |
379 | | - $msgStartPlayer = wfMsg( 'ogg-play-video' ); |
380 | | - $thumb = |
381 | | - Xml::tags( 'a', $linkAttribs, |
382 | | - Xml::element( 'img', |
383 | | - array( |
384 | | - 'src' => $this->thumbUrl, |
385 | | - 'width' => $width, |
386 | | - 'height' => $height, |
387 | | - ) + $attribs, |
388 | | - null ) |
389 | | - ) . |
390 | | - "<br/>\n"; |
391 | | - } else { |
392 | | - $msgStartPlayer = wfMsg( 'ogg-play-sound' ); |
393 | | - $thumb = ''; |
394 | | - } |
395 | | - $id = "ogg_player_" . OggTransformOutput::$serial; |
396 | | - |
397 | | - $s = Xml::tags( 'div', array( 'id' => $id ), |
398 | | - $thumb . |
399 | | - Xml::element( 'button', |
400 | | - array( |
401 | | - 'onclick' => "wgOggPlayer.init(false, '$id', $encUrl, $width, $height, $length);", |
402 | | - ), |
403 | | - $msgStartPlayer |
404 | | - ) |
405 | | - ); |
406 | | - return $s; |
407 | | - } |
408 | | -} |
409 | | - |
410 | | -class OggVideoDisplay extends OggTransformOutput { |
411 | | - function __construct( $videoUrl, $thumbUrl, $width, $height, $length ) { |
412 | | - parent::__construct( $videoUrl, $thumbUrl, $width, $height, $length, true ); |
413 | | - } |
414 | | -} |
415 | | - |
416 | | -class OggAudioDisplay extends OggTransformOutput { |
417 | | - function __construct( $videoUrl, $width, $height, $length ) { |
418 | | - parent::__construct( $videoUrl, false, $width, $height, $length, false ); |
419 | | - } |
420 | | -} |
421 | | - |
422 | | -?> |
Index: trunk/extensions/OggHandler/ext.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | $oggDir = dirname(__FILE__); |
5 | | -$wgAutoloadClasses['OggHandler'] = "$oggDir/Handler.php"; |
| 5 | +$wgAutoloadClasses['OggHandler'] = "$oggDir/OggHandler_body.php"; |
6 | 6 | $wgMediaHandlers['application/ogg'] = 'OggHandler'; |
7 | 7 | if ( !in_array( 'ogg', $wgFileExtensions ) ) { |
8 | 8 | $wgFileExtensions[] = 'ogg'; |
Index: trunk/extensions/OggHandler/OggHandler_body.php |
— | — | @@ -0,0 +1,421 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class OggHandler extends MediaHandler { |
| 5 | + const OGG_METADATA_VERSION = 1; |
| 6 | + |
| 7 | + var $videoTypes = array( 'Theora' ); |
| 8 | + var $audioTypes = array( 'Vorbis', 'Speex', 'FLAC' ); |
| 9 | + |
| 10 | + function isEnabled() { |
| 11 | + return true; |
| 12 | + } |
| 13 | + |
| 14 | + function getParamMap() { |
| 15 | + // TODO: add thumbtime, noplayer |
| 16 | + return array( 'img_width' => 'width' ); |
| 17 | + } |
| 18 | + |
| 19 | + function validateParam( $name, $value ) { |
| 20 | + // TODO |
| 21 | + return true; |
| 22 | + } |
| 23 | + |
| 24 | + function makeParamString( $params ) { |
| 25 | + // No parameters just yet, the thumbnails are always full-size |
| 26 | + return ''; |
| 27 | + /* |
| 28 | + $s = ''; |
| 29 | + foreach ( $params as $name => $value ) { |
| 30 | + if ( $s !== '' ) { |
| 31 | + $s .= '-'; |
| 32 | + } |
| 33 | + $s .= "$name=$value"; |
| 34 | + }*/ |
| 35 | + } |
| 36 | + |
| 37 | + function parseParamString( $str ) { |
| 38 | + // TODO |
| 39 | + return array(); |
| 40 | + } |
| 41 | + |
| 42 | + function normaliseParams( $image, &$params ) { |
| 43 | + // TODO |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + function getImageSize( $file, $path, $metadata = false ) { |
| 48 | + // Just return the size of the first video stream |
| 49 | + if ( $metadata === false ) { |
| 50 | + $metadata = $file->getMetadata(); |
| 51 | + } |
| 52 | + $metadata = $this->unpackMetadata( $metadata ); |
| 53 | + if ( isset( $metadata['error'] ) ) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + foreach ( $metadata['streams'] as $stream ) { |
| 57 | + if ( in_array( $stream['type'], $this->videoTypes ) ) { |
| 58 | + return array( |
| 59 | + $stream['header']['PICW'], |
| 60 | + $stream['header']['PICH'] |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + return array( false, false ); |
| 65 | + } |
| 66 | + |
| 67 | + function getMetadata( $image, $path ) { |
| 68 | + $metadata = array( 'version' => self::OGG_METADATA_VERSION ); |
| 69 | + |
| 70 | + if ( !class_exists( 'File_Ogg' ) ) { |
| 71 | + return require( 'File/Ogg.php' ); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + $f = new File_Ogg( $path ); |
| 76 | + $streams = array(); |
| 77 | + foreach ( $f->listStreams() as $streamType => $streamIDs ) { |
| 78 | + foreach ( $streamIDs as $streamID ) { |
| 79 | + $stream = $f->getStream( $streamID ); |
| 80 | + $streams[$streamID] = array( |
| 81 | + 'serial' => $stream->getSerial(), |
| 82 | + 'group' => $stream->getGroup(), |
| 83 | + 'type' => $stream->getType(), |
| 84 | + 'vendor' => $stream->getVendor(), |
| 85 | + 'length' => $stream->getLength(), |
| 86 | + 'size' => $stream->getSize(), |
| 87 | + 'header' => $stream->getHeader(), |
| 88 | + 'comments' => $stream->getComments() |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + $metadata['streams'] = $streams; |
| 93 | + $metadata['length'] = $f->getLength(); |
| 94 | + } catch ( PEAR_Exception $e ) { |
| 95 | + // File not found, invalid stream, etc. |
| 96 | + $metadata['error'] = array( |
| 97 | + 'message' => $e->getMessage(), |
| 98 | + 'code' => $e->getCode() |
| 99 | + ); |
| 100 | + } |
| 101 | + return serialize( $metadata ); |
| 102 | + } |
| 103 | + |
| 104 | + function unpackMetadata( $metadata ) { |
| 105 | + $unser = @unserialize( $metadata ); |
| 106 | + if ( isset( $unser['version'] ) && $unser['version'] == self::OGG_METADATA_VERSION ) { |
| 107 | + return $unser; |
| 108 | + } else { |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + function getMetadataType( $image ) { |
| 114 | + return 'ogg'; |
| 115 | + } |
| 116 | + |
| 117 | + function isMetadataValid( $image, $metadata ) { |
| 118 | + return $this->unpackMetadata( $metadata ) !== false; |
| 119 | + } |
| 120 | + |
| 121 | + function getThumbType( $ext, $mime ) { |
| 122 | + return array( 'jpg', 'image/jpeg' ); |
| 123 | + } |
| 124 | + |
| 125 | + function doTransform( $file, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 126 | + global $wgFFmpegLocation; |
| 127 | + $width = $params['width']; |
| 128 | + $srcWidth = $file->getWidth(); |
| 129 | + $srcHeight = $file->getHeight(); |
| 130 | + $height = $srcWidth == 0 ? $srcHeight : $width * $srcHeight / $srcWidth; |
| 131 | + $length = $this->getLength( $file ); |
| 132 | + |
| 133 | + if ( $srcHeight == 0 || $srcWidth == 0 ) { |
| 134 | + // Make audio player |
| 135 | + if ( empty( $params['width'] ) ) { |
| 136 | + $width = 200; |
| 137 | + } else { |
| 138 | + $width = $params['width']; |
| 139 | + } |
| 140 | + $height = 20; |
| 141 | + return new OggAudioDisplay( $file->getURL(), $width, $height, $length ); |
| 142 | + } |
| 143 | + |
| 144 | + if ( $flags & self::TRANSFORM_LATER ) { |
| 145 | + return new OggVideoDisplay( $file->getURL(), $dstUrl, $width, $height, $length ); |
| 146 | + } |
| 147 | + |
| 148 | + wfMkdirParents( dirname( $dstPath ) ); |
| 149 | + |
| 150 | + wfDebug( "Creating video thumbnail at $dstPath\n" ); |
| 151 | + |
| 152 | + $cmd = wfEscapeShellArg( $wgFFmpegLocation ) . |
| 153 | + ' -i ' . wfEscapeShellArg( $file->getPath() ) . |
| 154 | + # MJPEG, that's the same as JPEG except it's supported by the windows build of ffmpeg |
| 155 | + # No audio, one frame |
| 156 | + ' -f mjpeg -an -vframes 1' . |
| 157 | + # Seek to midpoint, it tends to be more interesting than the fade in at the start |
| 158 | + ' -ss ' . intval( $length / 2 ) . ' ' . |
| 159 | + wfEscapeShellArg( $dstPath ) . ' 2>&1'; |
| 160 | + |
| 161 | + $retval = 0; |
| 162 | + $returnText = wfShellExec( $cmd, $retval ); |
| 163 | + |
| 164 | + if ( $retval ) { |
| 165 | + // Filter nonsense |
| 166 | + $lines = explode( "\n", str_replace( "\r\n", "\n", $returnText ) ); |
| 167 | + if ( substr( $lines[0], 0, 6 ) == 'FFmpeg' ) { |
| 168 | + for ( $i = 1; $i < count( $lines ); $i++ ) { |
| 169 | + if ( substr( $lines[$i], 0, 2 ) != ' ' ) { |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + $lines = array_slice( $lines, $i ); |
| 174 | + } |
| 175 | + // Return error box |
| 176 | + return new MediaTransformError( 'thumbnail_error', $width, $height, implode( "\n", $lines ) ); |
| 177 | + } |
| 178 | + return new OggVideoDisplay( $file->getURL(), $dstUrl, $width, $height, $length ); |
| 179 | + } |
| 180 | + |
| 181 | + function canRender() { return true; } |
| 182 | + function mustRender( $file ) { return true; } |
| 183 | + |
| 184 | + /* |
| 185 | + function formatMetadata( $image, $metadata ) { |
| 186 | + if ( !$this->isMetadataValid( $image, $metadata ) ) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + $metadata = unserialize( $metadata ); |
| 190 | + $formatted = array(); |
| 191 | + if ( isset( $metadata['error'] ) ) { |
| 192 | + self::addMeta( $formatted, 'visible', 'ogg', 'error', $metadata['error']['message'] ); |
| 193 | + return $formatted; |
| 194 | + } |
| 195 | + $formatted = array(); |
| 196 | + $n = 0; |
| 197 | + foreach ( $metadata['streams'] as $stream ) { |
| 198 | + $prefix = "Stream $n "; |
| 199 | + $type = strtolower( $stream['type'] ); |
| 200 | + self::addMeta( $formatted, 'visible', 'ogg', 'type', $stream['type'], $n ); |
| 201 | + self::addMeta( $formatted, 'visible', 'ogg', 'vendor', $stream['vendor'], $n ); |
| 202 | + self::addMeta( $formatted, 'visible', 'ogg', 'length', $stream['length'], $n ); |
| 203 | + self::addMeta( $formatted, 'visible', 'ogg', 'size', $stream['size'], $n ); |
| 204 | + |
| 205 | + foreach ( $stream['header'] as $name => $value ) { |
| 206 | + self::addMeta( $formatted, 'visible', $type, $name, $value, $n ); |
| 207 | + $visible[$prefix . $name] = wfEscapeWikiText( $value ); |
| 208 | + } |
| 209 | + foreach ( $stream['comments'] as $name => $value ) { |
| 210 | + self::addMeta( $formatted, 'visible', $type, $name, $value, $n ); |
| 211 | + } |
| 212 | + } |
| 213 | + return $formatted; |
| 214 | + }*/ |
| 215 | + |
| 216 | + function getLength( $file ) { |
| 217 | + $metadata = $this->unpackMetadata( $file->getMetadata() ); |
| 218 | + if ( !$metadata || isset( $metadata['error'] ) ) { |
| 219 | + return 0; |
| 220 | + } else { |
| 221 | + return $metadata['length']; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + function getStreamTypes( $file ) { |
| 226 | + $streamTypes = ''; |
| 227 | + $metadata = $this->unpackMetadata( $file->getMetadata() ); |
| 228 | + if ( !$metadata || isset( $metadata['error'] ) ) { |
| 229 | + return false; |
| 230 | + } |
| 231 | + foreach ( $metadata['streams'] as $stream ) { |
| 232 | + $streamTypes[$stream['type']] = true; |
| 233 | + } |
| 234 | + return array_keys( $streamTypes ); |
| 235 | + } |
| 236 | + |
| 237 | + function getShortDesc( $file ) { |
| 238 | + global $wgLang; |
| 239 | + wfLoadExtensionMessages( 'OggHandler' ); |
| 240 | + $streamTypes = $this->getStreamTypes( $file ); |
| 241 | + if ( !$streamTypes ) { |
| 242 | + return parent::getShortDesc( $file ); |
| 243 | + } |
| 244 | + if ( array_intersect( $streamTypes, $this->videoTypes ) ) { |
| 245 | + // Count multiplexed audio/video as video for short descriptions |
| 246 | + $msg = 'ogg-short-video'; |
| 247 | + } elseif ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
| 248 | + $msg = 'ogg-short-audio'; |
| 249 | + } else { |
| 250 | + $msg = 'ogg-short-general'; |
| 251 | + } |
| 252 | + return wfMsg( $msg, implode( '/', $streamTypes ), |
| 253 | + $wgLang->formatTimePeriod( $this->getLength( $file ) ) ); |
| 254 | + } |
| 255 | + |
| 256 | + function getLongDesc( $file ) { |
| 257 | + global $wgLang; |
| 258 | + wfLoadExtensionMessages( 'OggHandler' ); |
| 259 | + $streamTypes = $this->getStreamTypes( $file ); |
| 260 | + if ( !$streamTypes ) { |
| 261 | + $unpacked = $this->unpackMetadata( $file->getMetadata() ); |
| 262 | + return wfMsg( 'ogg-long-error', $unpacked['error']['message'] ); |
| 263 | + } |
| 264 | + if ( array_intersect( $streamTypes, $this->videoTypes ) ) { |
| 265 | + if ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
| 266 | + $msg = 'ogg-long-multiplexed'; |
| 267 | + } else { |
| 268 | + $msg = 'ogg-long-video'; |
| 269 | + } |
| 270 | + } elseif ( array_intersect( $streamTypes, $this->audioTypes ) ) { |
| 271 | + $msg = 'ogg-long-audio'; |
| 272 | + } else { |
| 273 | + $msg = 'ogg-long-general'; |
| 274 | + } |
| 275 | + $size = 0; |
| 276 | + $unpacked = $this->unpackMetadata( $file->getMetadata() ); |
| 277 | + if ( !$unpacked || isset( $metadata['error'] ) ) { |
| 278 | + $length = 0; |
| 279 | + } else { |
| 280 | + $length = $this->getLength( $file ); |
| 281 | + foreach ( $unpacked['streams'] as $stream ) { |
| 282 | + $size += $stream['size']; |
| 283 | + } |
| 284 | + } |
| 285 | + $bitrate = $length == 0 ? 0 : $size / $length * 8; |
| 286 | + return wfMsg( $msg, implode( '/', $streamTypes ), |
| 287 | + $wgLang->formatTimePeriod( $length ), |
| 288 | + $wgLang->formatBitrate( $bitrate ), |
| 289 | + $wgLang->formatNum( $file->getWidth() ), |
| 290 | + $wgLang->formatNum( $file->getHeight() ) |
| 291 | + ); |
| 292 | + } |
| 293 | + |
| 294 | + function getDimensionsString( $file ) { |
| 295 | + global $wgLang; |
| 296 | + wfLoadExtensionMessages( 'OggHandler' ); |
| 297 | + if ( $file->getWidth() ) { |
| 298 | + return wfMsg( 'video-dims', $wgLang->formatTimePeriod( $this->getLength( $file ) ), |
| 299 | + $wgLang->formatNum( $file->getWidth() ), |
| 300 | + $wgLang->formatNum( $file->getHeight() ) ); |
| 301 | + } else { |
| 302 | + return $wgLang->formatTimePeriod( $this->getLength( $file ) ); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + function setHeaders( $out ) { |
| 307 | + global $wgScriptPath; |
| 308 | + if ( $out->hasHeadItem( 'OggHandler' ) ) { |
| 309 | + return; |
| 310 | + } |
| 311 | + |
| 312 | + wfLoadExtensionMessages( 'OggHandler' ); |
| 313 | + |
| 314 | + $msgNames = array( 'ogg-play', 'ogg-pause', 'ogg-stop', 'ogg-no-player', |
| 315 | + 'ogg-player-videoElement', 'ogg-player-oggPlugin', 'ogg-player-cortado', 'ogg-player-vlcPlugin', |
| 316 | + 'ogg-player-vlcActiveX', 'ogg-using-player' ); |
| 317 | + $msgValues = array_map( 'wfMsg', $msgNames ); |
| 318 | + $jsMsgs = Xml::encodeJsVar( (object)array_combine( $msgNames, $msgValues ) ); |
| 319 | + $encCortadoUrl = Xml::encodeJsVar( "$wgScriptPath/extensions/OggHandler/cortado-ovt-stripped-0.2.2.jar" ); |
| 320 | + |
| 321 | + $out->addHeadItem( 'OggHandler', <<<EOT |
| 322 | +<script type="text/javascript" src="$wgScriptPath/extensions/OggHandler/OggPlayer.js"></script> |
| 323 | +<script type="text/javascript"> |
| 324 | +wgOggPlayer.msg = $jsMsgs; |
| 325 | +wgOggPlayer.cortadoUrl = $encCortadoUrl; |
| 326 | +//wgOggPlayer.forcePlayer = 'cortado'; |
| 327 | +</script> |
| 328 | +EOT |
| 329 | + ); |
| 330 | + |
| 331 | + } |
| 332 | + |
| 333 | + function parserTransformHook( $parser, $file ) { |
| 334 | + if ( isset( $parser->mOutput->hasOggTransform ) ) { |
| 335 | + return; |
| 336 | + } |
| 337 | + $parser->mOutput->hasOggTransform = true; |
| 338 | + $parser->mOutput->addOutputHook( 'OggHandler' ); |
| 339 | + } |
| 340 | + |
| 341 | + static function outputHook( $outputPage, $parserOutput, $data ) { |
| 342 | + $instance = MediaHandler::getHandler( 'application/ogg' ); |
| 343 | + if ( $instance ) { |
| 344 | + $instance->setHeaders( $outputPage ); |
| 345 | + } |
| 346 | + } |
| 347 | +} |
| 348 | + |
| 349 | +class OggTransformOutput extends MediaTransformOutput { |
| 350 | + static $serial = 0; |
| 351 | + |
| 352 | + function __construct( $videoUrl, $thumbUrl, $width, $height, $length, $isVideo ) { |
| 353 | + $this->videoUrl = $videoUrl; |
| 354 | + $this->thumbUrl = $thumbUrl; |
| 355 | + $this->width = round( $width ); |
| 356 | + $this->height = round( $height ); |
| 357 | + $this->length = round( $length ); |
| 358 | + $this->isVideo = $isVideo; |
| 359 | + } |
| 360 | + |
| 361 | + function toHtml( $attribs = array() , $linkAttribs = false ) { |
| 362 | + wfLoadExtensionMessages( 'OggHandler' ); |
| 363 | + |
| 364 | + OggTransformOutput::$serial++; |
| 365 | + |
| 366 | + $encThumbUrl = htmlspecialchars( $this->thumbUrl ); |
| 367 | + |
| 368 | + if ( substr( $this->videoUrl, 0, 4 ) != 'http' ) { |
| 369 | + global $wgServer; |
| 370 | + $encUrl = Xml::encodeJsVar( $wgServer . $this->videoUrl ); |
| 371 | + } else { |
| 372 | + $encUrl = Xml::encodeJsVar( $this->videoUrl ); |
| 373 | + } |
| 374 | + #$encUrl = htmlspecialchars( $encUrl ); |
| 375 | + $length = intval( $this->length ); |
| 376 | + $width = intval( $this->width ); |
| 377 | + $height = intval( $this->height ); |
| 378 | + if ( $this->isVideo ) { |
| 379 | + $msgStartPlayer = wfMsg( 'ogg-play-video' ); |
| 380 | + $thumb = |
| 381 | + Xml::tags( 'a', $linkAttribs, |
| 382 | + Xml::element( 'img', |
| 383 | + array( |
| 384 | + 'src' => $this->thumbUrl, |
| 385 | + 'width' => $width, |
| 386 | + 'height' => $height, |
| 387 | + ) + $attribs, |
| 388 | + null ) |
| 389 | + ) . |
| 390 | + "<br/>\n"; |
| 391 | + } else { |
| 392 | + $msgStartPlayer = wfMsg( 'ogg-play-sound' ); |
| 393 | + $thumb = ''; |
| 394 | + } |
| 395 | + $id = "ogg_player_" . OggTransformOutput::$serial; |
| 396 | + |
| 397 | + $s = Xml::tags( 'div', array( 'id' => $id ), |
| 398 | + $thumb . |
| 399 | + Xml::element( 'button', |
| 400 | + array( |
| 401 | + 'onclick' => "wgOggPlayer.init(false, '$id', $encUrl, $width, $height, $length);", |
| 402 | + ), |
| 403 | + $msgStartPlayer |
| 404 | + ) |
| 405 | + ); |
| 406 | + return $s; |
| 407 | + } |
| 408 | +} |
| 409 | + |
| 410 | +class OggVideoDisplay extends OggTransformOutput { |
| 411 | + function __construct( $videoUrl, $thumbUrl, $width, $height, $length ) { |
| 412 | + parent::__construct( $videoUrl, $thumbUrl, $width, $height, $length, true ); |
| 413 | + } |
| 414 | +} |
| 415 | + |
| 416 | +class OggAudioDisplay extends OggTransformOutput { |
| 417 | + function __construct( $videoUrl, $width, $height, $length ) { |
| 418 | + parent::__construct( $videoUrl, false, $width, $height, $length, false ); |
| 419 | + } |
| 420 | +} |
| 421 | + |
| 422 | +?> |
Property changes on: trunk/extensions/OggHandler/OggHandler_body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 423 | + native |