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.


    require('youtube-dl.class.php');
    try {
        // Instantly download a YouTube video (using the default settings).
        new yt_downloader('http://www.youtube.com/watch?v=aahOEZKTCzU', TRUE);
 
        // Instantly download a YouTube video as MP3 (using the default settings).
        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.


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.


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 "

$video
succesfully downloaded into your Downloads Folder.

"
; } else if($download == 1) { print "

$video
already exists in your your Downloads Folder.

"
; } $filestats = $mytube->video_stats(); if($filestats !== FALSE) { print "

File statistics for $video

"
; print "Filesize: " . $filestats["size"] . "
"
; print "Created: " . $filestats["created"] . "
"
; print "Last modified: " . $filestats["modified"] . "
"
; } $path = $mytube->get_downloads_dir(); print "
.
$path . $video ."" target="_blank">Click, to open downloaded video file."; $thumb = $mytube->get_thumb(); clearstatcache(); if($thumb !== FALSE && file_exists($path . $thumb)) { print "
\"
"
. $path . $thumb ."\">
"
; } } } catch (Exception $e) { die($e->getMessage()); } }

$video
succesfully downloaded into your Downloads Folder.

“;
}
else if($download == 1) {
print “

$video
already exists in your your Downloads Folder.

“;
}
$filestats = $mytube->video_stats();
if($filestats !== FALSE) {
print “

File statistics for $video

“;
print “Filesize: ” . $filestats[“size”] . “
“;
print “Created: ” . $filestats[“created”] . “
“;
print “Last modified: ” . $filestats[“modified”] . “
“;
}
$path = $mytube->get_downloads_dir();
print “
Click, to open downloaded video file.“;
$thumb = $mytube->get_thumb();
clearstatcache();
if($thumb !== FALSE && file_exists($path . $thumb)) {
print “



“;
}
}
}
catch (Exception $e) {
die($e->getMessage());
}
}

See live demo and download source code.

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.

Leave a Reply

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

Top