• Technical
  • How to download YouTube videos using wget and cURL

How to download YouTube videos using wget and cURL

Hi all,

Few days back I got a request in PHPClasses.org to develop a functionality in PHP for downloading YouTube videos. So, here it is. I have already published it on PHPClasses.org. Here is the link: http://www.phpclasses.org/package/9092-PHP-Download-YouTube-Videos.html. In case, you don’t see this page right now, it is still under processing. 😉

The idea is pretty simple. First, get the information page of the video using http://youtube.com/get_video_info?video_id=GvQJbF2CXLQ” . Here, GvQJbF2CXLQ is the video id for the video –
“SPECTRE TEASER TRAILER – Coming Soon”. (God! When Monica Bellucci is going to age! I wish, never.). Coming back to the point, you need to get the information about the video first and then from those information you need to construct the download link of the video. The key “url_encoded_fmt_stream_map” contains all the information that you need. You might want to take a look into that if you are looking for various features like resolution, time, default image, etc.

So, let’s see the code now. This code make use of wget and cURL. Make sure that shell_exec and cURL extension are enabled.

 * Download videos from YouTube using WGET or cURL
 * @package YouTubeVideoDownloader
 * @author Nitesh Apte
 * @copyright 2015
 * @version 1
 * @license GPL v3
class YouTubeVideoDownloader {
    private $videoID;
    private $supportedVideoFormat = array("5", "18", "34");
    private $downloadUrl;
    private $videoFormat;
    private $DESTINATION = "/path/of/destination/folder/";
    private $title;
    public function __construct($videoID, $videoFormat) {
        $this->videoID = $videoID;
        if(!in_array($videoFormat, $this->supportedVideoFormat)) {
            $videoFormat = "5";
        $this->videoFormat = $videoFormat;
    public function createDownloadLink() {
        $infoPage = file_get_contents("http://youtube.com/get_video_info?video_id=".$this->videoID);
        parse_str($infoPage, $arr);
        $this->title = $arr['title'];
        $urlData = $arr['url_encoded_fmt_stream_map'];
        $dataSet = explode(',', $urlData);
        parse_str(urldecode($dataSet[0]), $data);
        $url = $data['url'];
        $sig = $data['signature'];
        $this->downloadUrl = str_replace('%2C', ',' ,$url.'&'.http_build_query($data).'&signature='.$sig.'&fmt='.$this->videoFormat);
    public function wgetDownload() {
        $code = 'wget --output-document='.$this->DESTINATION.str_replace(" ", "-", $this->title).'.mp4 '."'$this->downloadUrl'";
    public function curlDownload() {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->downloadUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSLVERSION,3);
        $data = curl_exec ($ch);
        $error = curl_error($ch);
        curl_close ($ch);
        $destination = $this->DESTINATION.$this->title.'.mp4';
        $file = fopen($destination, "w+");
        fputs($file, $data);

How to use it? Simple:

$obj = new YouTubeVideoDownloader("GvQJbF2CXLQ", "34");
$obj-&gt;wgetDownload(); // download using wget
$obj-&gt;curlDownload(); // download using cURL

Right now, I have implemented support for 3 formats only. In case you want to add extra formats, just add the format in $supportedVideoFormat array.

You can find it also on Github: https://github.com/niteshapte/youtube-video-downloader


That’s it guys for today.

Suggestions/feedback are very much welcome.

Have a great day ahead!

9,303 total views, 4 views today

This entry was posted in Technical

2 Responses

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.

Follow Me