The Code
<?php
/* usage
require('embed_code_extraction.php');
$embed_extraction = new EmbedCodeExtraction();
$embed_extraction->EmbedCode=$media['video'];
$embed_extraction->VideoHeight=250;
$embed_extraction->VideoWidth=350;
$thumbnail=$embed_extraction->get_embed_thumbnail();
$video_id=$embed_extraction->get_embed_id();
$video=$embed_extraction->get_embed_video()
*/
class EmbedCodeExtraction{
public $EmbedCode;
public $Id;
public $Thumbnail;
public $Video;
public $EmbedOutput;
public $EmbedType;
public $VideoHeight=250;
public $VideoWidth=350;
function get_embed_id(){
$type=$this->get_embed_type();
if($type=='vimeo'){
$id=$this->find_vimeo_id();
}
else if( $type=='youtube'){
$id=$this->find_youtube_id();
}
$this->Id=$id;
return $id;
}
function get_embed_thumbnail(){
$type=$this->get_embed_type();
if($type=='vimeo'){
$id=$this->find_vimeo_id();
$thumbnail=$this->get_vimeo_thumbnail();
}
else if( $type=='youtube'){
$id=$this->find_youtube_id();
$thumbnail=$this->get_youtube_thumbnail();
}
$this->Thumbnail=$thumbnail;
return $thumbnail;
}
function get_embed_video(){
$type=$this->get_embed_type();
if($type=='vimeo'){
$Id=$this->find_vimeo_id();
$video=$this->write_vimeo_video();
}
else if( $type=='youtube'){
$Id=$this->find_youtube_id();
$video=$this->write_youtube_video();
}
return $video;
}
//identify YouTube or Vimeo
function get_embed_type(){
$embed=$this->EmbedCode;
if(strpos($embed,'vimeo')>0 ){ $type='vimeo'; }
else if (strpos($embed,'youtube')>0 ){ $type='youtube'; }
else { $type='unknown'; }
$this->EmbedType=$type;
return $type;
}
/* VIMEO EXTRACTION */
function get_vimeo_thumbnail(){
$id=$this->find_vimeo_id($embed);
if($id){
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$id.php"));
$thumbnail= $hash[0]['thumbnail_small'];
$this->Thumbnail=$thumbnail;
return $thumbnail;
}
}
function write_vimeo_video(){
$id=$this->find_vimeo_id();
$h= $this-> VideoHeight;
$w= $this-> VideoWidth;
if($id){
$movie='<iframe src="http://player.vimeo.com/video/'.$id.'?title=0&byline=0&portrait=0&color=E68C35" width="'.$w.'" height="'.$h.'" frameborder="0"></iframe>';
return $movie;
}
}
function find_vimeo_id(){
$embed=$this->EmbedCode;
if(strpos($embed,'vimeo')>0 && strpos($embed,'clip_id=')>0 ){
$start=strpos($embed,'clip_id=')+8;
$end=strpos($embed,'&',$start);
$stop=$end-$start;
$id=substr( $embed, $start,$stop);
}
else if(strpos($embed,'vimeo')>0 && strpos($embed,'http://player.vimeo.com/video/')>0){
$start=strpos($embed,'/video/')+7;
$end=strpos($embed,'?',$start);
$stop=$end-$start;
$id=substr( $embed, $start,$stop);
}
$this->Id=$id;
return $id;
}
/* END VIMEO EXTRACTION */
/* YOUTUBE EXTRACTION */
function get_youtube_thumbnail(){
$id=$this->Id;
if($id){
$thumbnail= "http://i2.ytimg.com/vi/$id/default.jpg";
$this->Thumbnail=$thumbnail;
return $thumbnail;
}
}
function write_youtube_video(){
$id=$this->find_youtube_id();
$h= $this-> VideoHeight;
$w= $this-> VideoWidth;
$video='
<object width= "'.$w.'" height="'.$h.'">
<param name="movie" value="http://www.youtube.com/v/'.$id.'&autoplay=0&rel=0&fs=1&color1=0x4E3400&color2=0x322A22&border=0&loop=0"></param>
<param name="allowFullScreen" value="true"></param>
<embed src="http://www.youtube.com/v/'.$id.'&autoplay=0&rel=0&fs=1&color1=0x4E3400&color2=0x322A22&border=0&loop=0" type="application/x-shockwave-flash" allowfullscreen="true" width="'.$w.'" height="'.$h.'"></embed>
</object>
';
$this->Video=$video;
return $video;
}
/*
EXAMPLE YOUTUBE VARIATIONS
[YouTube - ‪Bolsa de Bota Atelier Favela‬‏](http://www.youtube.com/watch?v=FQDgVkasz7o)
<object width="425" height="344"><embed src="http://www.youtube.com/v/4Dq8dwyV35k?hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<object width=""425"" height=""344"" data=""http://www.youtube.com/v/wPsru6TlgfI%26hl=en%26fs=1%26rel=0%26ap=%2526fmt=18"" type=""application/x-shockwave-flash"">
<param name=""src"" value=""http://www.youtube.com/v/wPsru6TlgfI%26hl=en%26fs=1%26rel=0%26ap=%2526fmt=18"" />
</object>
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/VvC5HfUqa28" frameborder="0" allowfullscreen></iframe>
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/taFNdDs1SVU?rel=0" frameborder="0" allowfullscreen></iframe>
*/
function find_youtube_id(){
$embed=$this->EmbedCode;
if(strpos($embed,'watch?v=')>0 ){
$start=strpos($embed,'watch?v=')+8;
$id=substr( $embed, $start);
}
else if(strpos($embed,'youtube.com/v/')>0){
$start=strpos($embed,'/v/')+3;
$end=strpos($embed,'?',$start);
if(strpos($embed,'%')>0 && !$end){
$end=strpos($embed,'%',$start);
}
else if(strpos($embed,'"' )>0 && !$end){
$end=strpos($embed,'"',$start);
}
else if(strpos($embed,"'")>0 && !$end) {
$end=strpos($embed,'\\'',$start);
}
$stop=$end-$start;
$id=substr( $embed, $start,$stop);
}
else if(strpos($embed,'/embed/')>0){
$start=strpos($embed,'/embed/')+7;
$end=strpos($embed,'?',$start);
if(strpos($embed,'%')>0 && !$end){
$end=strpos($embed,'%',$start);
}
else if(strpos($embed,'"' )>0 && !$end){
$end=strpos($embed,'"',$start);
}
else if(strpos($embed,"'")>0 && !$end) {
$end=strpos($embed,'\\'',$start);
}
$stop=$end-$start;
$id=substr( $embed, $start,$stop);
}
$id=trim($id);
$this->Id=$id;
return $id;
}
/* END YOUTUBE EXTRACTION */
} //end class
?>