How do I get a YouTube FLV URL?

xLite picture xLite · Sep 21, 2011 · Viewed 15.9k times · Source

Looking through the site, every question uses an outdated method. How do the YouTube FLV downloader websites/applications do it?

I am trying to do this in PHP but the theory or steps to do it will suffice, thanks.

Answer

rbrito picture rbrito · Oct 2, 2011

As mentioned in other posts, you may want to look at our code in youtube-dl (or in the code of the Firefox extension called FlashVideoReplacer).

In the particular case of youtube-dl, the "real work" is done in the subclasses of InformationExtractor and it hard to give a "stable" answer, as the layout of such sites changes constantly.

There are some pieces of the information that are not dynamic, such as, for instance, the uploader of the video, the title, the date of upload, and, most importantly, the identifier of the video (a 11-character string).

For the dynamic parts, what can be said about such tools is that, essentially, the URLs generated by such videos are dynamically generated and you need to perform some back-and-forth communication with the server.

It is important to have in mind that what such sites can (and do) take into consideration depend on a number of parameters, including: the cookies that you have already received---as the case for HTML5 videos, your geolocation---for regional control, your age--for "strong" material, your language/locale---for showing content tailored to you, etc.

youtube-dl uses a regular expression to extract the video ID from the URL that you give and, then, uses a "normalized", typical URL as used from the United States, from which to proceed.

Some of the dynamic data to be gathered includes:

  • some time-stamp (the expire, and fexp parts of the final URL)
  • the cookies sent to the browser
  • the format in which we want to download the video (the itag part of the final URL)
  • throttling information (the algorithm, burst, factor)
  • some hashes/tokens used internally by them (e.g., the signature part of the final URL)

Some of the information listed above were once not required, but now they are (in particular, the cookies that they send you). This means that the information listed above is very likely to become obsolete, as the controls become stricter.

You can see some of the work (with respect to the cookies) that I did in this regard in the implementation of an external backend to use an external downloader (a "download accelerator") with what youtube-dl extracts.

Discloser: I have committed some changes to the repository, and I maintain the youtube-dl package in Debian (and, as a side effect, in Ubuntu).