Video Url Embed Script v1

Video Url Embed Script v1

Postby super3boy » Sun Oct 07, 2007 3:18 pm

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/");
Image
Image
Image
Image
User avatar
super3boy
Site Admin
Site Admin
 
Posts: 4944
Joined: Sun May 20, 2007 3:57 pm
Location: Atlanta, GA

Re: Video Url Embed Script v1

Postby Roswell » Sun Oct 07, 2007 4:16 pm

You have a class for one function?
Roswell
Moderator Team
Moderator Team
 
Posts: 2631
Joined: Thu Jul 05, 2007 5:06 pm

Re: Video Url Embed Script v1

Postby super3boy » Sun Oct 07, 2007 9:42 pm

Because it is version one of the script. I am going to add autoplay and size features.
Image
Image
Image
Image
User avatar
super3boy
Site Admin
Site Admin
 
Posts: 4944
Joined: Sun May 20, 2007 3:57 pm
Location: Atlanta, GA

Re: Video Url Embed Script v1

Postby Roswell » Sun Oct 07, 2007 9:57 pm

Even still, a class isn't really appropriate.
Roswell
Moderator Team
Moderator Team
 
Posts: 2631
Joined: Thu Jul 05, 2007 5:06 pm

Re: Video Url Embed Script v1

Postby super3boy » Mon Oct 08, 2007 9:31 pm

Well then I started programming it I really didn't know what I was going to do till I got a little into it.
Image
Image
Image
Image
User avatar
super3boy
Site Admin
Site Admin
 
Posts: 4944
Joined: Sun May 20, 2007 3:57 pm
Location: Atlanta, GA

Re: Video Url Embed Script v1

Postby super3boy » Wed Oct 10, 2007 6:58 pm

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");
Image
Image
Image
Image
User avatar
super3boy
Site Admin
Site Admin
 
Posts: 4944
Joined: Sun May 20, 2007 3:57 pm
Location: Atlanta, GA

Re: Video Url Embed Script v1

Postby art1919 » Mon Oct 22, 2007 6:50 pm

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!
art1919
Member
Member
 
Posts: 3
Joined: Mon Oct 22, 2007 6:14 pm

Re: Video Url Embed Script v1

Postby Roswell » Mon Oct 22, 2007 9:30 pm

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.
Roswell
Moderator Team
Moderator Team
 
Posts: 2631
Joined: Thu Jul 05, 2007 5:06 pm

Re: Video Url Embed Script v1

Postby art1919 » Mon Oct 22, 2007 11:14 pm

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.
art1919
Member
Member
 
Posts: 3
Joined: Mon Oct 22, 2007 6:14 pm

Re: Video Url Embed Script v1

Postby Roswell » Mon Oct 22, 2007 11:30 pm

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
?>
Roswell
Moderator Team
Moderator Team
 
Posts: 2631
Joined: Thu Jul 05, 2007 5:06 pm

Next

Return to PHP

Who is online

Users browsing this forum: No registered users and 1 guest