Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get a youtube video information from get_video_info.
request = require 'request'
youTubeMovieInfo =
youTubeMovieId: 'videoId'
url = "http://www.youtube.com/get_video_info?video_id=#{youTubeMovieInfo.youTubeMovieId}"
request.get url, (err, res, body) ->
return callback(false) if err
return callback(false) if res.statusCode isnt 200
formats = parseYoutubeInfoStringToFormats(body)
return callback(false) if formats is null
mp4StreamUrl = getMp4StreamUrlFromFormats(formats)
youTubeMovieInfo.mp4StreamUrl = mp4StreamUrl
# parse youtube api json response.
parseYoutubeInfoStringToFormats = (youtubeInfoString) ->
youtubeInfoArray = youtubeInfoString.split '&'
return null if youtubeInfoArray[0] is 'status=fail'
formatStreamArrayString = (element for element in youtubeInfoArray when element.split('=')[0] is 'url_encoded_fmt_stream_map')[0].split('=')[1]
formatStreamArray = decodeURIComponent(formatStreamArrayString).split(',')
formats = []
for formatStreamString in formatStreamArray
formatInfoArray = formatStreamString.split '&'
formatInfoMap = {}
for formatInfoElement in formatInfoArray
formatInfoElementPair = formatInfoElement.split '='
formatInfoMap[formatInfoElementPair[0]] = decodeURIComponent(formatInfoElementPair[1])
formats.push formatInfoMap
return formats
# find mp4 stream url from parsed data.
getMp4StreamUrlFromFormats = (formats) ->
return format.url for format in formats when format.itag is '18'
@JeanPaul3

This comment has been minimized.

Copy link

@JeanPaul3 JeanPaul3 commented Feb 5, 2020

Hi
Do you know a similar code in php?
thanks

@JeanPaul3

This comment has been minimized.

Copy link

@JeanPaul3 JeanPaul3 commented Feb 20, 2020

Thanks for the answer, but unfortunately getVideoInfo () not from anymore the results that you manage with your code, particularly the index "url_encoded_fmt_stream_map" it doesn't exist anymore, but it is necessary to intercept the index "player_response."
I attach you a code what works alone if is present the index 'url' , but unfortunately in the most greater part of the video Youtube the url is now drowned in the index "cipher", that I don't know whether to decipher.
for example the id: "QGV14x_8-eQ" directly returns an url downloadable, while the id "7cGWMlcAWq4" not!
Do You succeed there even?
This is the code
`<?PHP
//$id = "QGV14x_8-eQ";
$id = "7cGWMlcAWq4";
$data = youtube_info($id);
foreach($data[0] as $h => $v) echo ($h.":".$v."
");

function youtube_info($id)
	{
		$vformat = "video/mp4"; 				// The MIME type of the video. e.g. video/mp4, video/webm, etc.
		$url = "http://youtube.com/get_video_info?video_id=".$id;
		parse_str(file_get_contents($url),$info);
		if (!array_key_exists('player_response',$info)) 
			$div[0] = 'not found';
			else
			{	$videos = $info["player_response"];
				$i = 0;
				while ($i != 99)
					{ $da = strpos($videos,'{"itag"')+1;
					  $per = strpos($videos,'}',$da) - $da;
					  $data = substr($videos,$da,$per);
					   
					  if (strpos($data,$vformat) && strpos($data,'"quality"')) $itags [$i++] = $data; 
					  
					  $videos = substr($videos,$da + $per);
					  if ( !strpos($videos,'{"itag"') ) $i = 99;
					}
				$y = 0;
				$c = count($itags); $i = 0;
				while ($c--)
					{ $itags[$i] = str_replace ('":' , ':' , $itags[$i] );
					  $itags[$i] = str_replace ('"itag' , 'itag' , $itags[$i] );
					  $array = explode(',"',$itags[$i++]);
					  $base = array();
					  foreach($array as $h => $v) 
						{ $d = strpos($array[$h],":");
						  $ch = substr($array[$h],0,$d);
						  $dt = substr($array[$h],$d+1);
						  if (substr($dt,0,1) == '"') 
						  		$dt = substr($dt,1,strlen($dt)-2);
						  $base[$ch] = str_replace("\\u0026","&",$dt);
						}
					  if (!array_key_exists('url',$base)) 
					  	{ parse_str($base["cipher"],$dturl);
							foreach($dturl as $h => $v) echo ($h.":".$v."<br>");
							$url = $dturl['url'].'&sig='.$dturl['s'];
					  	}
					  $elem = array();
						if (array_key_exists('url',$base)) 
							{ $elem['url'] = $base['url']; $elem['dec'] = urldecode($base['url']); }
							else 
							{ $elem['cipher'] = $base['cipher']; $elem['dec'] = urldecode($base['cipher']); }
						$elem['itag'] = $base['itag'];
						$elem['formato'] = 'mp4';
						$elem['quality'] = $base['quality'];
						$elem['dim'] = $base['qualityLabel'];
					  $div [$y++] = $elem;
					}
			}
		return $div;
	}

?>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.