Downloading Youtube videos with PHP [closed] Downloading Youtube videos with PHP [closed] php php

Downloading Youtube videos with PHP [closed]


The first thing you should do is get a tool like Fiddler and visit a YouTube video page. In Fiddler, you will see all of the files that make up that page, including the FLV itself. Now, you know that the video isn't one of the CSS files, nor is it the image files. You can ignore those. Look for a big file. If you look at the URL, it begins with /videoplayback.

Now, once you've found it, figure out how the browser knew to get that file. Do a search through the sessions (Ctrl+F) and look for "videoplayback". You will see a hit on the first page you went to, like http://www.youtube.com/watch?v=123asdf. If you dig through that file, you'll see a DIV tag with the ID of "watch-player". Within that there is a script tag to setup the flash player, and within that are all of the flash parameters. Within those is the URL to the video.

So now you know how to use your tools to figure out how the browser got to it. How do you duplicate this behavior in PHP?

Do a file_get_contents() on the page that references the video. Ignore everything not in that watch-player div. Parse through the code until you find that variable that contains the URL. From there you will probably have to unescape that URL. Once you have it, you can do a file_get_contents() (or some other download method, depending on what you are trying to do) to get the URL. it is that simple. Your HTML parsing code will be the most complex.

Finally, keep in mind what you are about to do may be illegal. Check the EULA.


Nobody writes manuals/howtos that become outdated every four weeks. The closest you can get is inspecting the actual extraction methods in a contemporary implementation. Quite readable:

http://bitbucket.org/rg3/youtube-dl/raw/2010.08.04/youtube-dl

If you don't want to read through/reimplement it, it's obviously not simple, you could just run it as-is from PHP:

  system("youtube-dl '$url'");


last time i was working on fixing one of the brocken chrome extension to download youtube video. I fixed it by altering the script part. (Javascript)

var links = new String();var downlink = new String();var has22 = new Boolean();has22 = false;var Marked=false;var FMT_DATA = fmt_url_map;//This is html text that you have to grab. In case of extension it was readily available through:document.getElementsByTagName('script');var StrSplitter1='%2C', StrSplitter2='%26', StrSplitter3='%3D'; if (FMT_DATA.indexOf(',')>-1) { //Found ,    StrSplitter1=',';     StrSplitter2=(FMT_DATA.indexOf('&')>-1)?'&':'\\u0026';     StrSplitter3='='; }var videoURL=new Array(); var FMT_DATA_PACKET=new Array();var FMT_DATA_PACKET=FMT_DATA.split(StrSplitter1); for (var i=0;i<FMT_DATA_PACKET.length;i++){    var FMT_DATA_FRAME=FMT_DATA_PACKET[i].split(StrSplitter2);     var FMT_DATA_DUEO=new Array();     for (var j=0;j<FMT_DATA_FRAME.length;j++){        var pair=FMT_DATA_FRAME[j].split(StrSplitter3);         if (pair.length==2) {             FMT_DATA_DUEO[pair[0]]=pair[1];         }     }     var url=(FMT_DATA_DUEO['url'])?FMT_DATA_DUEO['url']:null;     if (url==null) continue;         url=unescape(unescape(url)).replace(/\\\//g,'/').replace(/\\u0026/g,'&');         var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;         var itag=(FMT_DATA_DUEO['itag'])?FMT_DATA_DUEO['itag']:null;     if (itag==null) continue;         var signature=(FMT_DATA_DUEO['sig'])?FMT_DATA_DUEO['sig']:null;         if (signature!=null) {         url=url+"&signature="+signature;     }     if (url.toLowerCase().indexOf('http')==0) { // validate URL         if (itag == '5') {            links += '<a href="' + url + '&title=' + username + title + quality240 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v240p">FLV (240p)</span></a>';        }        if (itag == '18') {            links += '<a href="' + url + '&title=' + username + title + quality360 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v360p">MP4 (360p)</span></a>';        }        if (itag == '35') {            links += '<a href="' + url + '&title=' + username + title + quality480 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v480p">FLV (480p)</span></a>';        }        if (itag == '22') {            links += '<a href="' + url + '&title=' + username + title + quality720 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v720p">MP4 HD (720p)</span></a>';        }        if (itag == '37') {            links += ' <a href="' + url + '&title=' + username + title + quality1080 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v1080p">MP4 HD (1080p)</span></a>';        }        if (itag == '38') {            links += '<a href="' + url + '&title=' + username + title + quality4k + '"style="text-decoration:none"><span class="yt-uix-button-menu-item"  id="v4k">MP4 HD (4K)</span></a>';        }        FavVideo();        videoURL[itag]=url;         console.log(itag);    } } 

You can get separate video link from videoURL[itag] array.Above logic can be converted to PHP easily

The extension can be downloaded from location http://www.figmentsol.com/chrome/ytdw/

I hope this would help someone. This is working solution (date:06-04-2013)