Difference between revisions of "Extension:VideoSectionLink"
(remove unnecessary concatenation) |
(awaits the embed code, that be next task) |
||
Line 47: | Line 47: | ||
* Expand the videosection-magic | * Expand the videosection-magic | ||
*/ | */ | ||
− | function magicVideosectionlink (&$parser, $url = '', $start = '',$duration = '') { | + | function magicVideosectionlink (&$parser, $url = '', $start = '', $duration = '', $ext = '') { |
global $egVideoSectionLinkMagic,$wgScript; | global $egVideoSectionLinkMagic,$wgScript; | ||
− | $text = "[{{SERVER}}$wgScript?action=videosection&url=" . urlencode($url) . "&start=$start&duration=$duration Click to download video]"; | + | $text = "[{{SERVER}}$wgScript?action=videosection&url=" . urlencode($url) . "&start=$start&duration=$duration&ext=$ext&width=$width&height=$height Click to download video]"; |
# Return result with available parser flags | # Return result with available parser flags | ||
Line 71: | Line 71: | ||
# Get the parameters from the title | # 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. | ||
+ | #$width is the desired width of the embed | ||
+ | #$height is the desired height of the embed | ||
+ | |||
+ | $url = $wgRequest->getText('url'); | ||
+ | $start = $wgRequest->getText('start'); | ||
+ | $duration = $wgRequest->getText('duration'); | ||
+ | $ext = $wgRequest->getText('ext'); | ||
+ | $width = $wgRequest->getText('width'); | ||
+ | $height = $wgRequest->getText('height'); | ||
+ | |||
+ | #Get components of url, hash the first part | ||
+ | $dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url); | ||
+ | $movie = md5($dest); | ||
+ | |||
+ | #Identify the extension for naming the decoded file | ||
+ | if ($ext == "mpg" || $ext == "avi" || $ext == "flv") { | ||
+ | $movieOut = md5($start.$duration.$dest) . "." . $ext; | ||
+ | } | ||
+ | else if ($ext == "embed") { | ||
+ | $ext = "flv"; | ||
+ | $movieOut = md5($start.$duration.$dest) . "." . $ext; | ||
+ | } | ||
+ | else { | ||
+ | $ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url); | ||
+ | $movieOut = md5($start.$duration.$dest) . "." . $ext; | ||
+ | } | ||
# Execute the ffmpeg command | # Execute the ffmpeg command | ||
− | $movieStr = "ffmpeg -i $ | + | if (!file_exists($movieOut)) { |
− | + | $movieStr = "ffmpeg -i $url -ss $start -t $duration -ar 22050 $wgUploadDirectory/$movieOut"; | |
+ | $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 { | ||
+ | |||
+ | # if w h specif set or set default | ||
− | + | # 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; | return true; | ||
} | } |
Revision as of 06:12, 22 November 2008
<?php /**
Template:PhpCategory:Extensions created with Template:Extension
* VideoSectionLink extension - An extension to enable users to link to 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:Jack * @copyright © 2008 User:Jack * @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:Jack',
'description' => 'An extension to enable users to link to 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 = ) { global $egVideoSectionLinkMagic,$wgScript;
$text = "" . urlencode($url) . "&start=$start&duration=$duration&ext=$ext&width=$width&height=$height Click to download video";
# Return result with available parser flags
return array( $text, 'found' => true, 'nowiki' => false, 'noparse' => false, 'noargs' => false, 'isHTML' => false ); }
/** * 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. #$width is the desired width of the embed #$height is the desired height of the embed
$url = $wgRequest->getText('url'); $start = $wgRequest->getText('start');
$duration = $wgRequest->getText('duration'); $ext = $wgRequest->getText('ext'); $width = $wgRequest->getText('width'); $height = $wgRequest->getText('height'); #Get components of url, hash the first part $dest = preg_replace('%(.+?\.)(\w+$)%', '$1', $url); $movie = md5($dest); #Identify the extension for naming the decoded file if ($ext == "mpg" || $ext == "avi" || $ext == "flv") { $movieOut = md5($start.$duration.$dest) . "." . $ext; }
else if ($ext == "embed") { $ext = "flv"; $movieOut = md5($start.$duration.$dest) . "." . $ext; }
else { $ext = preg_replace('%(.+?\.)(\w+$)%', '$2', $url); $movieOut = md5($start.$duration.$dest) . "." . $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); } if (!success && !file_exists($movieOut)) {
$wgOut->disable(); print_r("Input error. Hit your browser back button and try another URL"); }
else {
# if w h specif set or set default
# 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; }