Location: PHPKode > scripts > YouTube Video Fetcher > ytf.class.php
<?php

#####################################
#
# Name: Youtube Video Fetcher
# Filename: ytf.class.php
# Autor: Biagio Vincenzo Mamone Neto
# Contact: hide@address.com
# Autor Homepage: http://www.bmamone.com
# Version: 1.0
#
# License: FREE FOR NON COMERCIAL USE
#
#####################################

class ytf
{	
	public $info_url         = 'http://youtube.com/get_video_info?video_id='; // dont change it
	public $video_files_path = 'videos/'; // dont forget the slash in the end

	# getInfoFile
	# gets the information file needed to retrieve the video download url

	private function getInfoFile($id)
	{
		$info_file = @file_get_contents($this->info_url . $id);
		parse_str($info_file, $psd);
		if (!empty($psd['fmt_url_map']))
		{
			return $psd;
		
		} else {

			die('Error 001: Could not get video information file');

		}	
	}


	# getUrl
	# get the url for the video quality specified, if 999, gets the best one, if 0, the worst. If it could not find the quality specified, it returns a error.

	private function getUrl($id, $SPECquality)
	{

		$info_file = $this->getInfoFile($id);	
		$url_map = $info_file['fmt_url_map'];
		
		if ($SPECquality == 999) {

			# best quality

			$urls = explode('|', $url_map);
			if (count($urls) > 2)
			{
				$best_quality = substr($urls[1], 0, -3);				
			} else {
				$best_quality = $urls[1];
		
			}
			return $best_quality;

		} else if ($SPECquality == 0)
		{
			# worst quality

			$urls = explode('|', $url_map);
			$worst_quality = $urls[count($urls) - 1];
			return $worst_quality;
			
		} else
		{
			# quality is specified

			$urls_with_quality = explode(',', $url_map);

			$final_url_array = array();

			foreach ($urls_with_quality as $url_with_quality)
			{
				$parts = explode('|', $url_with_quality);
				$quality = $parts[0];
				$url = $parts[1];
				$final_url_array[$quality] = $url;
	
			}

			if (@!$final_url_array[$SPECquality])
			{

				die('Error 004: the specified (' . $SPECquality . ') quality does not exist');
			
			} else {
			
				return $final_url_array[$SPECquality];

			}

			
		}

	}

	# getVideo
	# downloads the video and returns it

	private function getVideo($id, $quality)
	{
		
		return file_get_contents($this->getUrl($id, $quality));
	
	}

	# getVideoToFile
	# puts the downloaded video into a file in the specified path

	private function getVideoToFile($id, $quality)
	{
		$file_path = $this->video_files_path . $id . '.mp4';
		$handle = fopen($file_path, 'w');
		if (fwrite($handle, $this->getVideo($id, $quality))) {
			
			fclose($handle);
			return $file_path;

		} else
		{
			
			fclose($handle);			
			die('Error 003: failed to write the video file');

		}
	}

	# downloadVideo
	# just executes the whole script, this function is the one wich has to be called

	public function downloadVideo($id, $quality='999')
	{
		if ($this->getVideoToFile($id, $quality))
		{
			return true;			
		} else
		{
			return false;
		}
	}

}


?>
Return current item: YouTube Video Fetcher