How to Download YouTube Video Using PHP

If you want to add youtube video downloader feature in your website then in this tutorial I am going to share simple PHP class which help you to download youtube video in available formats. With the help of this class you not only download youtube video but also convert youtube video as mp3 audio files. This PHP class takes a YouTube URL (or YouTube Video-ID) and downloads the video to your computer. Optionally, you can convert any YouTube video to an MP3 Audio file (requires ffmpeg to be installed!).


Download youtube-dl.class.php From HERE

Call the class on page and pass youtube video url in function to download youtube video with default settings.

<?php
    require('youtube-dl.class.php');
    try {
        
        new yt_downloader('http://www.youtube.com/watch?v=aahOEZKTCzU', TRUE);
 
        
        new yt_downloader('http://www.youtube.com/watch?v=aahOEZKTCzU', TRUE, 'audio');
    }
    catch (Exception $e) {
        die($e->getMessage());
    }
?>




Change youtube video audio format and download youtube video as audio file.

<?php
require('youtube-dl.class.php');
try
{
	$mytube = new yt_downloader("http://www.youtube.com/watch?v=px17OLxdDMU");
	$mytube->set_audio_format("wav");        # Change default audio output filetype.
	$mytube->set_ffmpegLogs_active(FALSE);   # Disable Ffmpeg process logging.
	$mytube->download_audio();
}
catch (Exception $e) {
	die($e->getMessage());
}
?>




Customise youtube video downloading settings.

<?php
if(!isset($_GET["vid"])) { exit("Nope."); }
else {
    require('youtube-dl.class.php');
    try
    {
        $mytube = new yt_downloader();
        $mytube->set_youtube($_GET["vid"]);     # YouTube URL (or ID) of the video to download.
        $mytube->set_video_quality(1);          # Change default output video file quality.
        $mytube->set_thumb_size('s');           # Change default video preview image size.
        $download = $mytube->download_video();
        if($download == 0 || $download == 1)
        {
            $video = $mytube->get_video();
            if($download == 0) {
                print "<h2><code>$video</code><br>succesfully downloaded into your Downloads Folder.</h2>";
            }
            else if($download == 1) {
                print "<h2><code>$video</code><br>already exists in your your Downloads Folder.</h2>";
            }
            $filestats = $mytube->video_stats();
            if($filestats !== FALSE) {
                print "<h3>File statistics for <code>$video</code></h3>";
                print "Filesize: " . $filestats["size"] . "<br>";
                print "Created: " . $filestats["created"] . "<br>";
                print "Last modified: " . $filestats["modified"] . "<br>";
            }
            $path = $mytube->get_downloads_dir();
            print "<br><a href='". $path . $video ."' target='_blank'>Click, to open downloaded video file.</a>";
            $thumb = $mytube->get_thumb();
            clearstatcache();
            if($thumb !== FALSE && file_exists($path . $thumb)) {
                print "<hr><img src=\"". $path . $thumb ."\"><hr>";
            }
        }
    }
    catch (Exception $e) {
        die($e->getMessage());
    }
}

See live demo and download source code.

DEMO | DOWNLOAD

Visit official github repository for more information and follow for future updates. Don’t forget to read license for using this plugin in your projects.


Posted in PHP