Difference between revisions of "Extension:VideoSectionLink"

From Organic Design wiki
(Needs RawHTML enabled also)
(Change title data)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<?php
 
<?php
 
/**{{Category:Extensions|VideoSectionLink}}{{php}}{{Category:Extensions created with Template:Extension}}
 
/**{{Category:Extensions|VideoSectionLink}}{{php}}{{Category:Extensions created with Template:Extension}}
  * VideoSectionLink extension - An extension to enable users to link to defined sections of video. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]
+
  * VideoSectionLink extension - An extension to enable users to link to or embed user-defined sections of video. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]
 
  *
 
  *
 
  * See http://www.mediawiki.org/wiki/Extension:VideoSectionLink for installation and usage details
 
  * See http://www.mediawiki.org/wiki/Extension:VideoSectionLink for installation and usage details
Line 7: Line 7:
 
  * @package MediaWiki
 
  * @package MediaWiki
 
  * @subpackage Extensions
 
  * @subpackage Extensions
  * @author [http://www.mediawiki.org/wiki/User:Jack User:Jack]
+
  * @author [http://www.mediawiki.org/wiki/User:Jacknz User:Jacknz]
  * @copyright © 2008 [http://www.mediawiki.org/wiki/User:Jack User:Jack]
+
  * @copyright © 2008 [http://www.flashkiwi.com/ Jack Henderson]
 
  * @licence GNU General Public Licence 2.0 or later
 
  * @licence GNU General Public Licence 2.0 or later
 
  */
 
  */
Line 22: Line 22:
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'VideoSectionLink',
 
'name'        => 'VideoSectionLink',
'author'      => '[http://www.mediawiki.org/wiki/User:Jack User:Jack]',
+
'author'      => '[http://www.mediawiki.org/wiki/User:Jacknz User:Jacknz]',
'description' => 'An extension to enable users to link to defined sections of video. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]',
+
'description' => 'An extension to enable users to link to or embed user-defined sections of video. Made with [http://www.organicdesign.co.nz/Template:Extension Template:Extension]',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:VideoSectionLink',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:VideoSectionLink',
 
'version'    => VIDEOSECTIONLINK_VERSION
 
'version'    => VIDEOSECTIONLINK_VERSION
Line 46: Line 46:
 
* Expand the videosection-magic
 
* Expand the videosection-magic
 
*/
 
*/
function magicVideosectionlink (&$parser, $url = '', $start = '', $duration = '', $ext = '') {
+
function magicVideosectionlink (&$parser, $url = '', $start = '', $duration = '', $ext = '', $width = '', $height = '', $wtext = 'Click to download video') {
global $egVideoSectionLinkMagic, $wgScript, $wgUploadDirectory;
+
global $egVideoSectionLinkMagic, $wgScript, $wgUploadPath, $wgUploadDirectory, $wgServer;
if ($ext != "embed") {
+
if ($ext == '' || $ext != "embed") {
$text = "[{{SERVER}}$wgScript?action=videosection&url=" . urlencode($url) . "&start=$start&duration=$duration&ext=$ext&width=$width&height=$height Click to download video]";
+
$text = "<a href='$wgServer$wgScript?action=videosection&url=" . urlencode($url) . "&start=$start&duration=$duration&ext=$ext'>$wtext</a>";
  
 
# Return result with available parser flags
 
# Return result with available parser flags
Line 57: Line 57:
 
'found'  => true,
 
'found'  => true,
 
'nowiki'  => false,
 
'nowiki'  => false,
'noparse' => false,
+
'noparse' => true,
 
'noargs'  => false,
 
'noargs'  => false,
'isHTML'  => false
+
'isHTML'  => true
 
);
 
);
 
}
 
}
Line 65: Line 65:
 
# Get components of url, hash the first part
 
# Get components of url, hash the first part
 
$dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url);
 
$dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url);
$movie = md5($dest);
+
$ext = "flv";
+
$movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;
# Identify the extension for naming the decoded file
 
if ($ext == "embed") $embed = true;
 
if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
 
$movieOut = md5($start.$duration.$dest) . "." . $ext;
 
}
 
else if ($ext == "embed" || $ext == "mov") {
 
$ext = "flv";
 
$movieOut = md5($start.$duration.$dest) . "." . $ext;
 
}
 
else {
 
$ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url);
 
if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
 
$movieOut = md5($start.$duration.$dest) . "." . $ext;
 
}
 
else {
 
$ext = "flv";
 
$movieOut = md5($start.$duration.$dest) . "." . $ext;
 
}
 
}
 
 
 
 
# Execute the ffmpeg command
 
# Execute the ffmpeg command
Line 94: Line 75:
 
 
 
# Create the embed string to return
 
# Create the embed string to return
if (!$width) $width = "425";
+
if ($width == '') $width = "320";
if (!$height) $height = "350";
+
if ($height == '') $height = "240";
 
$embedText =  
 
$embedText =  
"<html><object width=\"$width\" height=\"$height\">"
+
"<html><object id=\"FlowPlayer\" width=\"$width\" height=\"$height\" data=\"$wgServer$wgUploadPath/FlowPlayer.swf\" type=\"application/x-shockwave-flash\">"
. "<param name=\"movie\" value=\"$wgUploadDirectory/$movieOut\"></param>"
+
. "<param name=\"allowScriptAccess\" value=\"sameDomain\"/>"
. "<param name=\"wmode\" value=\"transparent\"></param>"
+
. "<param name=\"movie\" value=\"$wgServer$wgUploadPath/FlowPlayer.swf\"/>"
. "<embed src=\"$wgUploadDirectory/$movieOut\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"$width\" height=\"$height\">"
+
. "<param name=\"quality\" value=\"high\"/>"
. "</embed></object></html>";
+
. "<param name=\"scale\" value=\"noScale\"/>"
 +
. "<param name=\"wmode\" value=\"transparent\"/>"
 +
. "<param name=\"flashvars\" value=\"videoFile=$wgUploadPath/$movieOut&baseURL=$wgServer\"/>"
 +
. "</object></html>";
 
 
 
return array(
 
return array(
 
$embedText,
 
$embedText,
'found'   => true,
+
'noparse' => true,
'nowiki'  => false,
 
'noparse' => false,
 
'noargs'  => false,
 
 
'isHTML'  => true
 
'isHTML'  => true
 
);
 
);
Line 125: Line 106:
 
  # $duration is the desired duration of clip expressed in HH:MM:SS
 
  # $duration is the desired duration of clip expressed in HH:MM:SS
 
  # $ext is the desired extension, can be mpg, avi, flv, or embed, which is an flv returned as embed code.
 
  # $ext is the desired extension, can be mpg, avi, flv, or embed, which is an flv returned as embed code.
# $width is the desired width of the embed
 
# $height is the desired height of the embed
 
 
 
 
 
 
$url = $wgRequest->getText('url');
 
$url = $wgRequest->getText('url');
Line 132: Line 111:
 
  $duration = $wgRequest->getText('duration');
 
  $duration = $wgRequest->getText('duration');
 
  $ext = $wgRequest->getText('ext');
 
  $ext = $wgRequest->getText('ext');
$width = $wgRequest->getText('width');
+
 
$height = $wgRequest->getText('height');
 
 
 
 
 
 
  # Get components of url, hash the first part
 
  # Get components of url, hash the first part
 
  $dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url);
 
  $dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url);
$movie = md5($dest);
 
 
 
 
 
 
  # Identify the extension for naming the decoded file
 
  # Identify the extension for naming the decoded file
if ($ext == "embed") $embed = true;
 
 
  if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
 
  if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
   $movieOut = md5($start.$duration.$dest) . "." . $ext;
+
   $movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;
 
  }
 
  }
else if ($ext == "embed" || $ext == "mov") {
 
$ext = "flv";
 
$movieOut = md5($start.$duration.$dest) . "." . $ext;
 
}
 
 
  else {
 
  else {
 
  $ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url);
 
  $ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url);
 
  if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
 
  if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
  $movieOut = md5($start.$duration.$dest) . "." . $ext;
+
  $movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;
 
}
 
}
 
else {
 
else {
$ext = "flv";
+
$ext = "mpg";
$movieOut = md5($start.$duration.$dest) . "." . $ext;
+
$movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;
 
}
 
}
 
}
 
}
 
 
  
 
  # Execute the ffmpeg command
 
  # Execute the ffmpeg command
 
  if (!file_exists($movieOut)) {
 
  if (!file_exists($movieOut)) {
$movieStr = "ffmpeg -i $url -ss $start -t $duration -ar 22050 $wgUploadDirectory/$movieOut";
+
if ($ext == "flv") {
 +
$movieStr = "ffmpeg -i $url -ss $start -t $duration -ar 22050 $wgUploadDirectory/$movieOut";
 +
}
 +
else {
 +
$movieStr = "ffmpeg -i $url -ss $start -t $duration $wgUploadDirectory/$movieOut";
 +
}
 
  $success = shell_exec($movieStr);
 
  $success = shell_exec($movieStr);
}
 
 
if (!success && !file_exists($movieOut)) {
 
$wgOut->disable();
 
print_r("Input error. Hit your browser back button and try another URL");
 
 
}
 
}
 
 
else {
+
# Set headers to video
+
$wgOut->disable();
# Set up embed code with height and width here, or:
+
header("Content-Type: video/mpeg");
 +
$wgHfile = "Content-Disposition: attachment; filename=\"$wgUploadDirectory/$movieOut\"";
 +
header($wgHfile);
  
# Set headers to video
+
# Return the file as binary back to the client
$wgOut->disable();
+
readfile("$wgUploadDirectory/$movieOut");
header("Content-Type: video/mpeg");
 
$wgHfile = "Content-Disposition: attachment; filename=\"$wgUploadDirectory/$movieOut\"";
 
header($wgHfile);
 
  
# Return the file as binary back to the client
 
readfile("$wgUploadDirectory/$movieOut");
 
}
 
 
return true;
 
return true;
 
}
 
}

Latest revision as of 04:34, 28 November 2008

<?php /**

Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:Extension

* VideoSectionLink extension - An extension to enable users to link to or embed user-defined sections of video. Made with Template:Extension
*
* See http://www.mediawiki.org/wiki/Extension:VideoSectionLink for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Jacknz
* @copyright © 2008 Jack Henderson
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('VIDEOSECTIONLINK_VERSION', '0.0.1, 2008-10-31');

$egVideoSectionLinkMagic = 'videosection'; $wgExtensionFunctions[] = 'efSetupVideoSectionLink'; $wgHooks['LanguageGetMagic'][] = 'efVideoSectionLinkLanguageGetMagic';


$wgExtensionCredits['parserhook'][] = array( 'name' => 'VideoSectionLink', 'author' => 'User:Jacknz', 'description' => 'An extension to enable users to link to or embed user-defined sections of video. Made with Template:Extension', 'url' => 'http://www.organicdesign.co.nz/Extension:VideoSectionLink', 'version' => VIDEOSECTIONLINK_VERSION );

class VideoSectionLink {

/** * Constructor */ function __construct() { global $wgHooks, $wgParser, $egVideoSectionLinkMagic;

# Add the parser-function $wgParser->setFunctionHook($egVideoSectionLinkMagic, array($this,'magicVideosectionlink'));

#Add the Unknownaction hook $wgHooks['UnknownAction'][] = $this; }

/** * Expand the videosection-magic */ function magicVideosectionlink (&$parser, $url = , $start = , $duration = , $ext = , $width = , $height = , $wtext = 'Click to download video') { global $egVideoSectionLinkMagic, $wgScript, $wgUploadPath, $wgUploadDirectory, $wgServer; if ($ext == || $ext != "embed") { $text = "<a href='$wgServer$wgScript?action=videosection&url=" . urlencode($url) . "&start=$start&duration=$duration&ext=$ext'>$wtext</a>";

# Return result with available parser flags

return array( $text, 'found' => true, 'nowiki' => false, 'noparse' => true, 'noargs' => false, 'isHTML' => true ); } else { # Get components of url, hash the first part $dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url); $ext = "flv"; $movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;

# Execute the ffmpeg command if (!file_exists($movieOut)) { $movieStr = "ffmpeg -i $url -ss $start -t $duration -ar 22050 $wgUploadDirectory/$movieOut"; $success = shell_exec($movieStr); }

# Create the embed string to return if ($width == ) $width = "320"; if ($height == ) $height = "240"; $embedText = "" . "" . "" . "" . "" . "" . "" . "";

return array( $embedText, 'noparse' => true, 'isHTML' => true ); } }

/** * Callback method executes a shell command returning the section of the movie requested */ function onUnknownAction ($action, $article) { global $wgOut, $action, $article, $wgRequest, $wgUploadDirectory;

# Get the parameters from the title # $url is the url of the originating file and can be local or Internet # $start is the start time expressed in HH:MM:SS

		# $duration is the desired duration of clip expressed in HH:MM:SS
		# $ext is the desired extension, can be mpg, avi, flv, or embed, which is an flv returned as embed code.
		

$url = $wgRequest->getText('url'); $start = $wgRequest->getText('start');

		$duration = $wgRequest->getText('duration');
		$ext = $wgRequest->getText('ext');


		# Get components of url, hash the first part 		
		$dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url);
		
		# Identify the extension for naming the decoded file
		if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
 			$movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;
		}
		else {
			$ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url);
			if ($ext == "mpg" || $ext == "avi" || $ext == "flv") {
				$movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext;

} else { $ext = "mpg"; $movieOut = md5($dest.$start.$duration.$ext.$width.$height) . "." . $ext; } }

		# Execute the ffmpeg command
		if (!file_exists($movieOut)) {
			if ($ext == "flv") {

$movieStr = "ffmpeg -i $url -ss $start -t $duration -ar 22050 $wgUploadDirectory/$movieOut"; } else { $movieStr = "ffmpeg -i $url -ss $start -t $duration $wgUploadDirectory/$movieOut"; }

			$success = shell_exec($movieStr);

}

# Set headers to video $wgOut->disable(); header("Content-Type: video/mpeg"); $wgHfile = "Content-Disposition: attachment; filename=\"$wgUploadDirectory/$movieOut\""; header($wgHfile);

# Return the file as binary back to the client readfile("$wgUploadDirectory/$movieOut");

return true; }

/** * Needed in some versions to prevent Special:Version from breaking */ function __toString() { return __CLASS__; }

}

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

function efSetupVideoSectionLink() { global $egVideoSectionLink; $egVideoSectionLink = new VideoSectionLink();

}

/**

* Needed in MediaWiki >1.8.0 for magic word hooks to work properly
*/

function efVideoSectionLinkLanguageGetMagic(&$magicWords, $langCode = 0) { global $egVideoSectionLinkMagic; $magicWords[$egVideoSectionLinkMagic] = array($langCode, $egVideoSectionLinkMagic); return true; }