PHP Simple Script For YouTube Video Downloader


PHP simple youtube video downloader script free download. In this tutorial, you will learn how to create simple youtube videos and status videos downloader in PHP.

Note that, YouTube does not have the direct download of videos, an important feature for users or computers. So, this tutorial will guide you on how to create youtube video downloader in PHP.

And you will learn step by step from this tutorial on how to create a youtube video downloader in PHP. And also you can checkout live demo here https://www.tutsmake.com/yt1s/.

Simple Youtube Video Downloader PHP Script

Follow following the below steps create youtube video downloader in PHP:

  • Step 1 – Create index.php
  • Step 2 – Create class.youtube.php
  • Step 3 – Create downloader.php

Step 1 – Create index.php

First of all, create an index.php file and update the below HTML code into your index.php file.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form method="post" action="" class="formSmall">
<div class="row">
<div class="col-lg-12">
<h7 class="text-align"> Download YouTube Video</h7>
</div>
<div class="col-lg-12">
<div class="input-group">
<input type="text" class="form-control" name="video_link" placeholder="Paste link.. e.g. https://www.youtube.com/watch?v=OK_JCtrrv-c">
<span class="input-group-btn">
<button type="submit" name="submit" id="submit" class="btn btn-primary">Go!</button>
</span>
</div><!-- /input-group -->
</div>
</div><!-- .row -->
</form>
<?php
require_once "class.youtube.php";
$yt  = new YouTubeDownloader();
$downloadLinks ='';
$error='';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$videoLink = $_POST['video_link'];
if(!empty($videoLink)) {
$vid = $yt->getYouTubeCode($videoLink);
if($vid) {
$result = $yt->processVideo($vid);
if($result) {
//print_r($result);
$info = $result['videos']['info'];
$formats = $result['videos']['formats'];
$adapativeFormats = $result['videos']['adapativeFormats'];
$videoInfo = json_decode($info['player_response']);
$title = $videoInfo->videoDetails->title;
$thumbnail = $videoInfo->videoDetails->thumbnail->thumbnails{0}->url;
}
else {
$error = "Something went wrong";
}
}
}
else {
$error = "Please enter a YouTube video URL";
}
}
?>
<?php if($formats):?>
<div class="row formSmall">
<div class="col-lg-3">
<img src="<?php print $thumbnail?>">
</div>
<div class="col-lg-9">
<?php print $title?>
</div>
</div>
<div class="card formSmall">
<div class="card-header">
<strong>With Video & Sound</strong>
</div>
<div class="card-body">
<table class="table ">
<tr>
<td>Type</td>
<td>Quality</td>
<td>Download</td>
</tr>
<?php foreach ($formats as $video) :?>
<tr>
<td><?php print $video['type']?></td>
<td><?php print $video['quality']?></td>
<td><a href="downloader.php?link=<?php print urlencode($video['link'])?>&title=<?php print urlencode($title)?>&type=<?php print urlencode($video['type'])?>">Download</a> </td>
</tr>
<?php endforeach;?>
</table>
</div>
</div>
<div class="card formSmall">
<div class="card-header">
<strong>Videos video only/ Audios audio only</strong>
</div>
<div class="card-body">
<table class="table ">
<tr>
<td>Type</td>
<td>Quality</td>
<td>Download</td>
</tr>
<?php foreach ($adapativeFormats as $video) :?>
<tr>
<td><?php print $video['type']?></td>
<td><?php print $video['quality']?></td>
<td><a href="downloader.php?link=<?php print urlencode($video['link'])?>&title=<?php print urlencode($title)?>&type=<?php print urlencode($video['type'])?>">Download</a> </td>
</tr>
<?php endforeach;?>
</table>
</div>
</div>
<?php endif;?>
</div>
</body>
</html>

This HTML and PHP code will show you videos for download from youtube. When you enter a link to the YouTube video in the search bar and search.

Step 2 – Create class.youtube.php

In this step, create a new file name upload.php file and update the below code into your class.youtube.php file.

<?php
/**
* tutsmake
*
* This class narrates the functions to support download a video from YouTube
* @class YouTubeDownloader
* @author tutsmake
*
*/
Class YouTubeDownloader {
/**
* Get the YouTube code from a video URL
* @param $url
* @return mixed
*/
public function getYouTubeCode($url) {
parse_str( parse_url( $url, PHP_URL_QUERY ), $vars );
return $vars['v'];
}
/**
* Process the video url and return details of the video
* @param $vid
* @return array|void
*/
public function processVideo($vid) {
parse_str(file_get_contents("https://youtube.com/get_video_info?video_id=".$vid),$info);
$playabilityJson = json_decode($info['player_response']);
$formats = $playabilityJson->streamingData->formats;
$adaptiveFormats = $playabilityJson->streamingData->adaptiveFormats;
//Checking playable or not
$IsPlayable = $playabilityJson->playabilityStatus->status;
//writing to log file
if(strtolower($IsPlayable) != 'ok') {
$log = date("c")." ".$info['player_response']."\n";
file_put_contents('./video.log', $log, FILE_APPEND);
}
$result = array();
if(!empty($info) && $info['status'] == 'ok' && strtolower($IsPlayable) == 'ok') {
$i=0;
foreach($adaptiveFormats as $stream) {
$streamUrl = $stream->url;
$type = explode(";", $stream->mimeType);
$qualityLabel='';
if(!empty($stream->qualityLabel)) {
$qualityLabel = $stream->qualityLabel;
}
$videoOptions[$i]['link'] = $streamUrl;
$videoOptions[$i]['type'] = $type[0];
$videoOptions[$i]['quality'] = $qualityLabel;
$i++;
}
$j=0;
foreach($formats as $stream) {
$streamUrl = $stream->url;
$type = explode(";", $stream->mimeType);
$qualityLabel='';
if(!empty($stream->qualityLabel)) {
$qualityLabel = $stream->qualityLabel;
}
$videoOptionsOrg[$j]['link'] = $streamUrl;
$videoOptionsOrg[$j]['type'] = $type[0];
$videoOptionsOrg[$j]['quality'] = $qualityLabel;
$j++;
}
$result['videos'] = array(
'info'=>$info,
'adapativeFormats'=>$videoOptions,
'formats'=>$videoOptionsOrg
);
return $result;
}
else {
return;
}
}
}

Now, you must know about these functions, which is in class.youtube.php file. There are followings:

  • getYouTubeCode – Get the YouTube code from a video URL
  • processVideo – Process the video url and return details of the video

Step 3 – Create downloader.php

In this step, you need to create a file named downloader.php. And the following code into it:

Because this file code is responsible for the force download of the file to your computer or mobile.

<?php
$downloadURL = urldecode($_GET['link']);
//print  $downloadURL;exit;
$type = urldecode($_GET['type']);
$title = urldecode($_GET['title']);
//Finding file extension from the mime type
$typeArr = explode("/",$type);
$extension = $typeArr[1];
$fileName = $title.'.'.$extension;
if (!empty($downloadURL)) {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment;filename=\"$fileName\"");
header("Content-Transfer-Encoding: binary");
readfile($downloadURL);
}

Conclusion

In this tutorial, you have learned how to create simple youtube videos and status videos downloader in PHP.

Recommended PHP Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?