eyecatchup / php-yt_downloader

PHP Class to download videos from YouTube.
http://eyecatchup.github.com/php-yt_downloader/
293 stars 166 forks source link

Also Exception due to variable "videos" #7

Open jpgreth opened 11 years ago

jpgreth commented 11 years ago

Hey, I also get an error: "Notice: Undefined variable: videos in var/www/youtube_graph/youtube-dl.class.php on line 534 Grabbing original file location(s) failed."

Can you tell whats wrong there?

fhfa commented 11 years ago

Make sure your get_url_map($data) function in youtube-dl.class.php looks like this (look bellow). Basically you only need to add $videos = false; to the beginning of the function, seems the developer forgot to initialize the variable. Another likely explanation is: the developer expected if(!isset($match[1])) { return FALSE; } to step in the event something went wrong with the process of finding the urls for each available stream (quality and format) for a given Youtube video, and return false as this function was supposed to. Either way, since $videos is now initialized to false, when this function is unable to find the streams urls it will return false, and when it does find the streams it will return them instead.

/**
     *  Get the URL map for a YouTube Video.
     *  @access  private
     *  @param   string   $data  Info-File contents for a YouTube Video.
     *  @return  mixed    Returns an array, containg the Video URL map,
     *                    or (boolean) FALSE if extracting failed.
     */
    private function get_url_map($data)
    {
        $videos = false;
        preg_match('/stream_map=(.[^&]*?)&/i',$data,$match);
        if(!isset($match[1])) {
            return FALSE;
        }
        else {
            $fmt_url = urldecode($match[1]);
            if(preg_match('/^(.*?)\\\\u0026/',$fmt_url,$match2)) {
                $fmt_url = $match2[1];
            }

            $urls = explode(',',$fmt_url);
            $tmp = array();

            foreach($urls as $url) {
                if(preg_match('/itag=([0-9]+)&url=(.*?)&.*?/si',$url,$um))
                {
                    $u = urldecode($um[2]);
                    $tmp[$um[1]] = $u;
                }
            }

            $formats = array(
                '13' => array('3gp', '240p', '10'),
                '17' => array('3gp', '240p', '9'),
                '36' => array('3gp', '320p', '8'),
                '5' => array('flv', '240p', '7'),
                '6' => array('flv', '240p', '6'),
                '34' => array('flv', '320p', '5'),
                '35' => array('flv', '480p', '4'),
                '18' => array('mp4', '480p', '3'),
                '22' => array('mp4', '720p', '2'),
                '37' => array('mp4', '1080p', '1')
            );

            foreach ($formats as $format => $meta) {
                if (isset($tmp[$format])) {
                    $videos[] = array('pref' => $meta[2], 'ext' => $meta[0], 'type' => $meta[1], 'url' => $tmp[$format]);
                }
            }
            return $videos;
        }
    }

Note: If this function is unable to extract the streams urls, php-yt-downloader will be unable to download any video. In the event this happens several times, this could mean the function's code responsible for extracting the urls needs to be updated. This functions receives the content of: http://www.youtube.com/get_video_info?video_id=aahOEZKTCzU&el=embedded&ps=default&eurl=&hl=en_US (where "aahOEZKTCzU" is the ID of the Youtube video) and searches for the urls there after the &url_encoded_fmt_stream_map= section up until before &fallback_host=. Now you know this, case this function needs update, you might be able to do it yourself.

Another Note: This same content is also passed to Youtube's Flash Player (using javascript to send flash vars to the player) on the regular video viewing page, for example: http://www.youtube.com/watch?v=aahOEZKTCzU, this was how I about 2 years ago did something like this php-yt-downloader class to download youtube videos for my own computer. Fortunately (and congratulations to eyecatchup) though the same, improved on it, and found a way to retrieve only the required pieces (stripping HTML, JavaScript and CSS, therefore maximizing performance) by accessing the required content from: http://www.youtube.com/get_video_info?video_id=aahOEZKTCzU&el=embedded&ps=default&eurl=&hl=en_US

Hope this helps you, and anybody else seeking to update this function, everytime Youtube changes it's own method of making streams available to it's own player. Whenever time allows it, I will too post answers to other issues. Be well.