Skip to content

Instantly share code, notes, and snippets.

@caraya
Created March 26, 2012 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caraya/2206834 to your computer and use it in GitHub Desktop.
Save caraya/2206834 to your computer and use it in GitHub Desktop.
This code is released under the following license
Copyright (c) 2012 Carlos Araya
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Syntax
Basic syntax: [video src="File URL/path"]
Advanced syntax: [video src="File URL/path" width="pixel size" height="pixel size" options="string" id="string" format="special string"]
Example:
[video src="http://myblog.com/videos/vidclip"
poster="http://myblog.com/wp-content/uploads/2009/09/clip-teaser.jpg"
width="320"
height="240"
options="autoplay" id="vid-1"]
src is required. It must be an absolute or relative path to video with the file name ("vidclip", in this example). The files need to be located somewhere in your content folder (usually 'wp-content') unless you specify the files using the format option. File extension can be omitted. Upload "vidclip.ogg" (in Ogg Theora format) and "vidclip.m4v" (in mp4 format) to the specified location.
poster is optional. It is a URL to the image you want to display before the video loads/starts playback. (If a jpg with the same file name can be detected, it will be used when 'poster' is not specified.)
width and height are optional. The default size is 480x320 (which is the resolution of iPhone; larger videos might not play back on iPhone/iPod Touch).
options is optional. It is a space-separated list of attributes defining the player behavior: autoplay to start playback automatically; autobuffer to start preparing the playback, controls to display the built-in playback controls (otherwise you can build and hook up your own), and loop to start from the beginning when the end is reached. The default value is "controls autobuffer".
id is optional. If you do not include one, the video tag will have an automatically generated ID of html5video-number.
class is optional. It is applied to a 'wrapper' div and to the video tag. If you do not include one, the class will be 'html5video'.
format is optional. It is a space-separated list of available file formats. (Recognized values are auto, flv, m4v, ogg and ogv.) The default value is auto which autodetects the formats. You can specify a list of available formats manually instead (e.g. format="ogg m4v").
QUESTIONS TO ANSWER
Can you use file exists when querying a remote file?
Is JPG the only format for poster frames?
<?php
function video_init(){
add_shortcode('video', 'video_shortcode');
}
add_action('init', 'video_init', 99);
function video_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'src' => '',
'poster' => '',
'id' => '',
'class' => 'video',
'options' => 'controls',
'width' => '480',
'height' => '320',
'format' => 'auto',
), $atts ) );
static $vid_cnt = 0;
$fallbackext = 'mp4';
$format = substr($src, 0, strlen($src)-3);
$hasm4v=false;
$hasogg=false;
$mySrc = $src;
// If the fourth character counting backwards is a period
// then the source is everything but the last 4 characters
if (substr($src, strlen($src)-4, 1)=='.') {
$src = substr($src, 0, strlen($src)-4);
}
// Test if we're calling a remote file (the first 4 characters are http)
// If we are not then set the file name to the path of our video upload directory
// plus the $src variable
if (substr($src, 0, 4)!='http') $filename = WP_CONTENT_URL . '/video/' . $src ;
// Otherwise make it equal to the source of the video, we're assuming you're using
// a full URL
else $filename = $src;
// If the format is auto we need to determine which formats we have available.
// Note that both MP4 and OGG can use different extensions, we take that into account
if ($format == ' auto') {
if (file_exists($filename.'.m4v')) {$hasm4v=true; $mp4ext='.m4v';}
if (file_exists($filename.'.mp4')) {$hasm4v=true; $mp4ext='.mp4';}
if (file_exists($filename.'.ogg')) {$hasogg=true; $oggext='.ogg';}
if (file_exists($filename.'.ogv')) {$hasogg=true; $oggext='.ogv';}
} else {
if (strpos($format, 'm4v')) {$hasm4v=true; $mp4ext='.m4v';}
if (strpos($format, 'mp4')) {$hasm4v=true; $mp4ext='.mp4';}
if (strpos($format, 'ogg')) {$hasogg=true; $oggext='.ogg';}
if (strpos($format, 'ogv')) {$hasogg=true; $oggext='.ogv';}
}
if($poster == '') {if (file_exists($filename.'.jpg')) $poster = $src.'.jpg';}
if($id == '') $id = 'video-' . $vid_cnt++;
$plpath = WP_CONTENT_URL .'/player/flowplayer-3.2.5.swf';
$fallbackpl = '<object width="'.$width.'" height="'.$height.'" type="application/x-shockwave-flash" data="'.$plpath.'?file='.$src.$fallbackext.'" id="f-'.$id.'"><param name="movie" value="'.$plpath.'?file='.$src.$fallbackext.'" />';
$output = '<div class="video_wrap '.$class.'">';
if ($has4v) {
$output .= '<video width="'.$width.'" height="'.$height.'" '.$options;
if( $poster != '') $output .= ' poster="' . $poster . '"';
$output .= ' id="' . $id . '" class="' . $class . '">';
$output .= '<source src="'.$src.$mp4ext.'" type="video/mp4" />';
if ($hasogg) {
$output .= '<source src="'.$src.$oggext.'" type="video/ogg" />';
}
$output .= $fallbackpl ;
}
else {
$output .= $fallbackpl.'</div>';
}
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment