4 funções em PHP para tratar vídeos do Youtube

2 minutos de leitura

Atualizado em:

Separei algumas funções muito úteis que criei e outras adaptei para o Gospel 10 para tratamento de vídeos do Youtube, confiram:

1. Separa a Hash do vídeo do url

function formataVideo($url){
		
		if ($url=='')
			return false;
		
		$aUrl = parse_url($url);
            $aUrl['query_params'] = array();
            $aPairs = explode('&', $aUrl['query']);
            foreach($aPairs as $sPair) {
                if (trim($sPair) == '') { continue; }
                list($sKey, $sValue) = explode('=', $sPair);
                $aUrl['query_params'][$sKey] = urldecode($sValue);
            }
			$qUrl=$aUrl['query_params'];
			
			$v=$qUrl['v'];
			
			if ($v=="")
				return false;		
            return $v;	
}

Como usar:


2. Retorna o Tempo do Vídeo

function tempoVideoYoutube($url) {      
	
	// set video data feed URL
	$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $url;
	
	// read feed into SimpleXML object
	if($handle=@fopen($feedURL,r))
		$entry = simplexml_load_file($feedURL);
	else
		return "";
	// get nodes in media: namespace for media information
	$media = $entry->children('http://search.yahoo.com/mrss/');
			
	// get  node for video length
	$yt = $media->children('http://gdata.youtube.com/schemas/2007');
	$attrs = $yt->duration->attributes();
	$length = $attrs['seconds']; 
	
	$minuto=floor($length/60);
	$segundo= $length % 60;	
	
	if ($minuto<10)
		$minuto="0".$minuto;
	if ($segundo<10)
		$segundo="0".$segundo;

	$tempo=$minuto.":".$segundo;
	
	  
    return $tempo;      
} 

Como usar:


3. Retorna a miniatura do vídeo

function imagemVideo($url){	
	$imagem="http://img.youtube.com/vi/".$url."/default.jpg";
	return $imagem;
}

Como Usar:

">

4. Retorna várias informações do vídeo

function parseVideoEntry($url) {      
      $obj= new stdClass;
      
	// set video data feed URL
	$feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $url;
	
	// read feed into SimpleXML object
	if($handle=@fopen($feedURL,r))
		$entry = simplexml_load_file($feedURL);
	else
		return "";
	
      // get author name and feed URL
      $obj->author = $entry->author->name;
      $obj->authorURL = $entry->author->uri;
      
      // get nodes in media: namespace for media information
      $media = $entry->children('http://search.yahoo.com/mrss/');
      $obj->title = $media->group->title;
      $obj->description = $media->group->description;
      
      // get video player URL
      $attrs = $media->group->player->attributes();
      $obj->watchURL = $attrs['url']; 
      
      // get video thumbnail
      $attrs = $media->group->thumbnail[0]->attributes();
      $obj->thumbnailURL = $attrs['url']; 
            
      // get  node for video length
      $yt = $media->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->duration->attributes();
      $obj->length = $attrs['seconds']; 
      
      // get  node for viewer statistics
      $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->statistics->attributes();
      $obj->viewCount = $attrs['viewCount']; 
      $minuto=floor($length/60);
	$segundo= $length % 60;	
	
	if ($minuto<10)
		$minuto="0".$minuto;
	if ($segundo<10)
		$segundo="0".$segundo;

	$tempo=$minuto.":".$segundo;
	
      // get  node for video ratings
      $gd = $entry->children('http://schemas.google.com/g/2005'); 
      if ($gd->rating) { 
        $attrs = $gd->rating->attributes();
        $obj->rating = $attrs['average']; 
      } else {
        $obj->rating = 0;         
      }
        
      // get  node for video comments
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->comments->feedLink) { 
        $attrs = $gd->comments->feedLink->attributes();
        $obj->commentsURL = $attrs['href']; 
        $obj->commentsCount = $attrs['countHint']; 
      }
      
      // get feed URL for video responses
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/
      schemas/2007#video.responses']"); 
      if (count($nodeset) > 0) {
        $obj->responsesURL = $nodeset[0]['href'];      
      }
         
      // get feed URL for related videos
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/
      schemas/2007#video.related']"); 
      if (count($nodeset) > 0) {
        $obj->relatedURL = $nodeset[0]['href'];      
      }
    
      // return object to caller  
      return $obj;      
    }   

Deixe um comentário