Page 1 of 4

Video Url Embed Script v1

PostPosted: Sun Oct 07, 2007 3:18 pm
by super3boy
I wanted to test out some OOP(Object Oriented Programming) in PHP so I created a video embed class.

Code: Select all
class video_embed{
   var $raw;
   var $code;
   var $host = "";
   var $error = false;
   
   function embed($raw)
   {      
      //clean the rawdata
      $raw = trim($raw);
      
      //get the code
      $hosts = array("http://youtube.com/watch?v=", "http://video.google.com/videoplay?docid=-",
      "http://one.revver.com/watch/", "http://www.metacafe.com/watch/");
      $code = str_replace($hosts, "", $raw);
      
   
      //find host
      $host1 = strpos($raw, "youtube.com", 1);
      $host2 = strpos($raw, "video.google.com", 1);
      $host3 = strpos($raw, "one.revver.com", 1);
      $host4 = strpos($raw, "www.metacafe.com", 1);
      
      if($host1 != null) {$host .= "youtube";}
      if($host2 != null) {$host .= "google";}
      if($host3 != null) {$host .= "revver";}
      if($host4 != null) {$host .= "metacafe";}
      
      //error
      if($host != "youtube" && $host != "google" && $host != "revver" && $host != "metacafe")
      {
         $error = true;
         echo "Error Embedding ";
      }
      
      if($host == "youtube")
      {
         echo "
         <object width='425' height='353'><param name='movie' value='http://www.youtube.com/v/$code'></param>
         <param name='wmode' value='transparent'></param>
         <embed src='http://www.youtube.com/v/$code' type='application/x-shockwave-flash' wmode='transparent' width='425' height='353'>
         </embed></object><br/>";
      }
      
      if($host == "google")
      {
         echo "
         <embed style='width:400px; height:326px;' id='VideoPlayback' type='application/x-shockwave-flash'
         src='http://video.google.com/googleplayer.swf?docId=-$code&hl=en' flashvars=''></embed><br/><br/>";
      }
      
      if($host == "revver")
      {
         //clean the code
         $code = str_replace("/flv", "", $code);
         
         echo "<script src='http://flash.revver.com/player/1.0/player.js?mediaId:$code;affiliateId:0;height:392;width:480;' type='text/javascript'>
         </script><br/>";
      }
      
      if($host == "metacafe")
      {
         //clean the code
         $code = strrev($code);
         $code = trim($code, "/");
         $code = strrev($code);
      
         echo "<embed src='http://www.metacafe.com/fplayer/$code.swf'
         width='400' height='345' wmode='transparent' pluginspage='http://www.macromedia.com/go/getflashplayer'
         type='application/x-shockwave-flash'> </embed><br>";
      }
      
   }
}


To embed a video from youtube, google video, revver, or metacafe simply use this template:

Code: Select all
$video=new video_embed();
$video->embed("video url or permalink");


Here is a temporary demo page: [url]http://1.nystic.com/t3h_bin/oop/video_embed.php/url]
Instead of adding embed code for each one I used a one line function with the url in it:
Code: Select all
$video=new video_embed();
$video->embed("http://youtube.com/watch?v=MgV0PjNw3Qw");
$video->embed("http://video.google.com/videoplay?docid=-6511716987227605917");
$video->embed("http://one.revver.com/watch/421951");
$video->embed("http://one.revver.com/watch/421951/flv");
$video->embed("http://www.metacafe.com/watch/854726/photoshop_match_color_simple_and_amazing_results/");

Re: Video Url Embed Script v1

PostPosted: Sun Oct 07, 2007 4:16 pm
by Roswell
You have a class for one function?

Re: Video Url Embed Script v1

PostPosted: Sun Oct 07, 2007 9:42 pm
by super3boy
Because it is version one of the script. I am going to add autoplay and size features.

Re: Video Url Embed Script v1

PostPosted: Sun Oct 07, 2007 9:57 pm
by Roswell
Even still, a class isn't really appropriate.

Re: Video Url Embed Script v1

PostPosted: Mon Oct 08, 2007 9:31 pm
by super3boy
Well then I started programming it I really didn't know what I was going to do till I got a little into it.

Re: Video Url Embed Script v1

PostPosted: Wed Oct 10, 2007 6:58 pm
by super3boy
New version!

Change log:
Liveleak.com added
Youtube and google video support for auto play

Code: Select all
<?php
class video_embed{

   function embed($raw, $auto)
   {      
      //clean the raw data
      $raw = trim($raw);
      
      //check auto play
      
      //get the code
      $hosts = array("http://youtube.com/watch?v=", "http://video.google.com/videoplay?docid=-",
      "http://one.revver.com/watch/", "http://www.metacafe.com/watch/", "http://www.liveleak.com/view?i=");
      $code = str_replace($hosts, "", $raw);
      
   
      //find host
      $host1 = strpos($raw, "youtube.com", 1);
      $host2 = strpos($raw, "video.google.com", 1);
      $host3 = strpos($raw, "one.revver.com", 1);
      $host4 = strpos($raw, "www.metacafe.com", 1);
      $host5 = strpos($raw, "www.liveleak.com", 1);
      
      if($host1 != null) {$host .= "youtube";}
      if($host2 != null) {$host .= "google";}
      if($host3 != null) {$host .= "revver";}
      if($host4 != null) {$host .= "metacafe";}
      if($host5 != null) {$host .= "liveleak";}
      
      //error
      if($host != "youtube" && $host != "google" && $host != "revver" && $host != "metacafe" && $host != "liveleak")
      {
         $error = true;
         echo "Error Embedding<br/>";
      }
      
      if($host == "youtube")
      {
         //autoplay
         $you_auto = "";
         if($auto == "play") { $you_auto = "&autoplay=1"; }
      
         echo "
         <object width='425' height='353'><param name='movie' value='http://www.youtube.com/v/$code$you_auto'></param>
         <param name='wmode' value='transparent'></param>
         <embed src='http://www.youtube.com/v/$code$you_auto' type='application/x-shockwave-flash' wmode='transparent' width='425' height='353'>
         </embed></object><br/>";
      }
      
      if($host == "google")
      {
         //autoplay
         $google_auto = "";
         if($auto == "play") { $google_auto = "&autoPlay=true"; }
      
         echo "
         <embed style='width:400px; height:326px;' id='VideoPlayback' type='application/x-shockwave-flash'
         src='http://video.google.com/googleplayer.swf?docId=-$code$google_auto&hl=en' flashvars=''></embed><br/><br/>";
      }
      
      if($host == "revver")
      {
         //clean the code
         $code = str_replace("/flv", "", $code);
         
         //autoplay
         $rev_auto = "";
         if($auto == "play") { $rev_auto = "&autoStart=true"; }
         
         echo "<script src='http://flash.revver.com/player/1.0/player.js?mediaId:$code;affiliateId:0;height:392;width:480;' type='text/javascript'>
         </script><br/>";
      }
      
      if($host == "metacafe")
      {
         //clean the code
         $code = strrev($code);
         $code = trim($code, "/");
         $code = strrev($code);
      
         echo "<embed src='http://www.metacafe.com/fplayer/$code.swf'
         width='400' height='345' wmode='transparent' pluginspage='http://www.macromedia.com/go/getflashplayer'
         type='application/x-shockwave-flash'> </embed><br/><br/>";
      }
      
      if($host == "liveleak")
      {
         //clean the code
         $code = str_replace("&p=1", "", $code);
      
      
         echo "<object type='application/x-shockwave-flash' width='450' height='370'='transparent'
         data='http://www.liveleak.com/player.swf?autostart=$live_auto&token=$code'>
         <param name='movie' value='http://www.liveleak.com/player.swf?autostart=$live_auto&token=$code'>
         <param name='wmode' value='transparent'><param name='quality' value='high'></object><br/><br/>";
      }
      
      //clean up varibles
      $raw = null;
      $code = null;
      $host = null;      
   }
}
?>


Format:
Code: Select all
[code]$video=new video_embed();
$video->embed("[video url]" , "[the word 'play' to activate autoplay]");
[/code]

Example code
[code]$video=new video_embed();
$video->embed("http://youtube.com/watch?v=MgV0PjNw3Qw" , "play");
$video->embed("http://video.google.com/videoplay?docid=-6511716987227605917" , "play");
$video->embed("http://one.revver.com/watch/421951");
$video->embed("http://one.revver.com/watch/421951/flv");
$video->embed("http://www.metacafe.com/watch/854726/photoshop_match_color_simple_and_amazing_results/");
$video->embed("http://www.liveleak.com/view?i=f23792e462");
$video->embed("http://www.liveleak.com/view?i=f23792e462&p=1", "play");

Re: Video Url Embed Script v1

PostPosted: Mon Oct 22, 2007 6:50 pm
by art1919
This script looks great, I just want to make sure it works the way I think.

Would this embed script work if somebody submitted a video url, for example a youtube link (http://www.youtube.com/watch?v=videocode) and it would convert this link into the code needed to embed the video on yo your site. Is this correct?

Are there any instructions for how to use this script? I am trying to embed youtube videos on my site. I have an area where people can submit a link. The link would be called from this command in the database

$video_name_raw

I am just not sure how to apply my code to this script.

Any help would be greatly appreciated.

Thanks!

Re: Video Url Embed Script v1

PostPosted: Mon Oct 22, 2007 9:30 pm
by Roswell
In terms of efficiency, this script is a nightmare. In all honesty, I'd just write something yourself. Seeing as you just need to use Youtube by the sounds of it, that won't be hard at all.

Re: Video Url Embed Script v1

PostPosted: Mon Oct 22, 2007 11:14 pm
by art1919
Thanks for the advice.

The only thing I am having trouble with in terms of writing something myself is that the url that is being submitted is in this format http://www.youtube.com/watch?v=3iVP0tzwhVc

however the url I need in order to embed is in this format
http://www.youtube.com/v/3iVP0tzwhVc

Is there any kind of php code that would convert the first url into the 2nd format which is used for the embedded video?

Thanks for any help anybody can provide.

Re: Video Url Embed Script v1

PostPosted: Mon Oct 22, 2007 11:30 pm
by Roswell
Code: Select all
<?php
$url 
$_GET["url"]; //Assuming it's in that format
$string explode("?v="$url);
$string $string[1];
$new_string "http://www.youtube.com/v/" $string;

echo 
$new_string//Output the way you want it
?>