Skip to content

Instantly share code, notes, and snippets.

@RenanMsV
Created April 7, 2019 15:50
Show Gist options
  • Select an option

  • Save RenanMsV/70f31fec777469a4d5a99d5db632dd04 to your computer and use it in GitHub Desktop.

Select an option

Save RenanMsV/70f31fec777469a4d5a99d5db632dd04 to your computer and use it in GitHub Desktop.
PHP - Youtube Mp4 Downloader
<?php
function YT_IN_DX($url){
$cookie_file_path = "cookies.txt";
$agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/46.0";
$ch = curl_init();
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function YT_V_INFO($v){
$url="https://www.youtube.com/watch?v=$v";
$html=YT_IN_DX($url);
$video_links=Explode_Content('ytplayer.config = ', ';ytplayer.load', $html);
$jsondownload1=json_decode($video_links, true);
$json_str=$jsondownload1["args"]["player_response"];
$jsn_str=str_replace("\u0026","&",$json_str);
$streamin_data_json=json_decode($jsn_str, true);
// Get and show full list of properties of this video
//printf('<pre>%s</pre>', var_export($video_links, true));
// Get and show video url and thumbnail
printf('<h3>Requesting video data:</h3>%s</br>%s</br><img src="%s"/></br>', $jsondownload1["args"]["title"], $jsondownload1["args"]["loaderUrl"], $jsondownload1["args"]["thumbnail_url"]);
printf('<h3>List of available formats:</h3><ul>');
foreach ($streamin_data_json["streamingData"]["formats"] as $key => $value)
{
printf('<li>Mirror[%d]: <a href="%s" target="_blank">%s</a> - Quality: %s %s</li>', $key, $value["url"], $value["mimeType"], $value["quality"], $value["qualityLabel"]);
}
print('</ul>');
// Get and show list of formats
printf('<h3>Extra data:</h3><pre>%s</pre>', var_export($streamin_data_json["streamingData"]["formats"], true));
// Returning index [0] direct url and quality
#return sprintf('%s - %s', $streamin_data_json["streamingData"]["formats"][0]["url"], $streamin_data_json["streamingData"]["formats"][0]["quality"]);
}
function Explode_Content($first, $last, $string){
$exp=explode($first,$string);
// Check if ID is invalid
if(count($exp) === 1)
die('Invalid youtube video ID');
$exp=explode($last,$exp[1]);
return $exp[0];
}
// Include these functions at the top.
// Get direct url from Youtube with two lines
YT_V_INFO(isset($_GET["v"]) ? $_GET["v"] : 'xcJtL7QggTI');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment