PHP Code For Making A Simple YouTube Downloader

By Faruque Ahamed Mollick

Hello guys… I am here again with a tutorial. In this tutorial I am going to tell you how you can build a simple YouTube downloader with the help of PHP programming language. You can now build your own YouTube downloader after you get the code that I am going to show you.

Before showing the code I am going to tell you where you can get the YouTube video information for a specific video. Well, there is a specific URL which looks like that –

http://youtube.com/get_video_id?video_id=WwVZBfMlNPA

You can see that the URL above contain the video id WwVZBfMlNPA. That means all the information about the video WwVZBfMlNPA will be inside the URL. The YouTube player will issue a HTTP request to that URL. Now if you download the URL from that file then you will see some information are there about that video which contain video thumbnail image, video URL and more. So you need to get the video URL from there.

Recommended article: YouTube Video Downloader PHP Script – Download Source Code

PHP code to get the YouTube video download link

Now I am going to show you the code where you can see that I have get the YouTube video thumbnail and video download link from using the PHP code. Here is the code to get the video download link for a particular YouTube video:

 <?php
   parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id=WwVZBfMlNPA'), $video_data);
   $streams = $video_data['url_encoded_fmt_stream_map'];
   $thumburl = $video_data['iurlmq'];
   $streams = explode(',',$streams);
   $counter = 1;
 ?>
  <img src="<?php echo $thumburl; ?>" />
  <br/>
  
  <?php

    foreach ($streams as $streamdata) {
  printf("Stream %d:<br/>----------------<br/><br/>", $counter);
  
  parse_str($streamdata,$streamdata);
  
  foreach ($streamdata as $key => $value) {
    if ($key == "url") {
      $value = urldecode($value);
      ?>
      <strong><?php echo $key;?>:</strong> <a href='<?php echo $value; ?>' download='downloadfilename'>Download Video</a><br/>
  <?php echo "<br/><br/>";echo "<br/><br/>";
      } else {
      printf("<strong>%s:</strong> %s<br/>", $key, $value);
    }
  }
  $counter = $counter+1;
  printf("<br/><br/>");
}

You can see that I have got the thumbnail image and download links for each types of video quality.

Final PHP code for completing the YouTube Downloader

Now I am going to show you the complete code for building a real YouTube downloader where user can put the YOuTube video URL like https://www.youtube.com/watch?v=WwVZBfMlNPA and YouTube video download link with thumbnail image will be available. Here is the code:

<form action="index.php" method="get">
URL: <input type="text" name="yturl">
<input type="submit">
</form>
<?php
if (isset($_GET["yturl"])) {
$url = $_GET["yturl"];

$regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/";
$match;

if(preg_match($regex_pattern, $url, $match)){
    
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );  
$idfromurl = $my_array_of_vars['v'];
echo "<br/>";

// Download Videos from Youtube in PHP
// By: Sheharyar Naseer
$id = $idfromurl; // just in case

parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id), $video_data);

//parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id=Fw4jHDPaagg&el=vevo&el=embedded'), $video_data);

$streams = $video_data['url_encoded_fmt_stream_map'];
$thumburl = $video_data['iurlmq'];
$streams = explode(',',$streams);
$counter = 1;
?>
  
  
  <img src="<?php echo $thumburl; ?>" />
  <br/>
  
  <?php

    foreach ($streams as $streamdata) {
  printf("Stream %d:<br/>----------------<br/><br/>", $counter);
  
  parse_str($streamdata,$streamdata);
  
  foreach ($streamdata as $key => $value) {
    if ($key == "url") {
      $value = urldecode($value);
      ?>
      <strong><?php echo $key;?>:</strong> <a href='<?php echo $value; ?>' download='downloadfilename'>Download Video</a><br/>
      
      <?php echo "<br/><br/>";
    } else {
      printf("<strong>%s:</strong> %s<br/>", $key, $value);
    }
  }
  $counter = $counter+1;
  printf("<br/><br/>");
}

}else{
    echo "Sorry, not a youtube Video URL";
}
}
?>

In the above code you can see that i have created a form where user input data will be checked if it is a YouTube video link or not. From the URL YouTube video id will be taken and it will be put in the video info URL that looks like http://youtube.com/get_video_id?video_id=WwVZBfMlNPA .

now test it by entering the YouTube video URL. Remember that those videos which are restricted will not work with that code. Some of the videos from particulsr channels like Vevo can not be downloaded with this process. Youcan try by entering this video URL – http://youtube.com/get_video_id?video_id=WwVZBfMlNPA or by any videos which are not restricted.

Leave a Reply

Your email address will not be published. Required fields are marked *