Fabrice
9/13/2013 - 11:04 AM

Parse url video pour afficher thumb et emded url

Parse url video pour afficher thumb et emded url

class media
{
  var $CI;

	function __construct()
	{
		$this->CI =& get_instance();
		$this->CI->load->library('dmcloud');
	}

	function get($url)
	{
		$host = str_replace("www.", "",url_host($url));

		switch ($host) {
			case 'youtube.com':
				return $this->youtube($url);
				break;
			case 'youtu.be':
				return $this->youtube($url);
				break;				
			case 'dailymotion.com':
				return $this->dailymotion($url);
				break;
			case 'vimeo.com':
				return $this->vimeo($url);
				break;
			case 'api.dmcloud.net':
				return $this->dmcloud($url);
				break;	
			default:
				return $this->other($url);
				break;
		}

	}

	/**
	 * Regexp de l'url
	 */
	function youtube($url)
	{
        preg_match_all('/(youtu.be\/|\/watch\?v=|\/embed\/)([a-z0-9\-_]+)/i',$url,$matches);
        $id = $matches[2][0];

		return $this->youtubeVideo($id);
	}

	/**
	 * Regexp de l'url
	 */
	function dmcloud($url)
	{
        preg_match_all('/api.dmcloud.net\/(.*)\/4d2ada44f325e1218b00035e\/(.*)\?/i',$url,$matches);
        $id = $matches[2][0];  

		return $this->dmcloudVideo($id);
	}
	
	/**
	 * Regexp de l'url
	 */
	function dailymotion($url)
	{
		preg_match_all('/^.+dailymotion.com\/(?:video|swf\/video|embed\/video|hub|swf)\/([^&?]+)/',$url,$matches);

		$id = $matches[1][0];

		return $this->dailyVideo($id);
    }

	/**
	 * Regexp de l'url
	 */
	function vimeo($url)
	{
        preg_match_all('/vimeo.com\/(.*)/i',$url,$matches);
        $id = $matches[1][0];
        
        return $this->vimeoVideo($id);
	}

	/**
	* @package DailyMation Video parser 
	*/
	function dailyVideo($id)
	{
		$site = file_get_contents("http://www.dailymotion.com/services/oembed?format=json&url=http://www.dailymotion.com/video/$id");
		$convert = json_decode($site);

		if( ! is_object($convert)){
			$video->embed = NULL;
	    	$video->thumb = NULL;

	    	return $video;
		}

		$thumbs = $convert->thumbnail_url;

		$video->embed = 'http://www.dailymotion.com/embed/video/'.$id;
	    $video->thumb = $thumbs;

	    return $video;
	}

	/**
	* @package Youtube Video parser 
	*/
	function youtubeVideo($id)
	{
	  	$video->embed 	= 'http://www.youtube.com/embed/'.$id;
	    $video->thumb 	= 'http://i1.ytimg.com/vi/'.$id.'/default.jpg';
	   	$video->thumbhq 	= 'http://i1.ytimg.com/vi/'.$id.'/hqdefault.jpg';

	    return $video;
	}

	/**
	* @package vimeo Video parser 
	*/
	function vimeoVideo($id)
	{
		$site 	 	= file_get_contents("http://vimeo.com/api/v2/video/$id.json");
		$convert 	= json_decode($site);

		if( ! is_object($convert)){
			$video->embed = NULL;
	    	$video->thumb = NULL;

	    	return $video;
		}

		$thumbs 	= $convert[0]->thumbnail_large;

	    $video->embed 	= 'http://player.vimeo.com/video/'.$id;
	    $video->thumb 	= $thumbs;

	    return $video;
	}

	/**
	* @package Youtube Video parser 
	*/
	function dmcloudVideo($id)
	{
		$site = $this->CI->dmcloud->get_url_embed($id);

	    $video->embed 	= $site->embed;
	    $video->thumb 	= $site->thumb;

	    return $video; 
	}

	/**
	* @package Other Video parser 
	*/
	function other($url)
	{
	    $video->embed 	= $url;
	    $video->thumb 	= NULL;

	    return $video; 
	}	

}