Skip to content

Parsing the url_encoded_fmt_stream_map

Kalle Lindström edited this page Feb 17, 2014 · 2 revisions

The url_encoded_fmt_stream_map (referred to as "the stream map" from now) is a list of format maps. A format map is a key-value mapping of video attributes. For example, one key in a format map is "url" which maps to the download url for the video, and another is "itag" maps to a numerical ID that identifies a specific video format. There is one format map for each format that a particular video supports. For example, there will be one format map for the "mp4" format, one for the "flv" format and one for the "3gp" format and so on.

The stream map is returned as a string where the individual format maps are separated by a comma (,). The easiest way to extract the format maps is to simply split on a comma:

format_maps = stream_map.split(",")

Now it's simple to parse the URL parameters in the format maps using the CGI library:

parsed = format_maps.map { |fmt| CGI.parse(fmt) }

The parsed entries now look something like this:

[{"sig"=>
   ["A3F77185C9BBB8F56192B3032E6890402DBF1D6F.2AC99B7D6D060F7CFA34DC9B40FC452230AE811D"],
  "itag"=>["22"],
  "quality"=>["hd720"],
  "type"=>["video/mp4; codecs=\"avc1.64001F, mp4a.40.2\""],
  "url"=>
   ["http://r1---sn-5go7ln7s.googlevideo.com/videoplayback?mt=1392659818&itag=22&id=a31cfc45e48f52de&ms=au&pcm2fr=yes&fexp=900225%2C927612%2C933212%2C929209%2C916608%2C936110%2C937417%2C913434%2C936910%2C936913%2C902907&sver=3&expire=1392686029&key=yt5&ip=176.10.249.16&mv=m&upn=fxzeAH1SPGc&sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2fr%2Cratebypass%2Csource%2Cupn%2Cexpire&source=youtube&ratebypass=yes&ipbits=0"],
  "fallback_host"=>["tc.v5.cache2.googlevideo.com"]},
 {"sig"=>
   ["9BDC07563E2A6C21C1FE742A3806354EDEFC6C1D.6D785B491951C17EE95A7E27F55F3696AC2FDAFE"],
  "itag"=>["43"],
  "quality"=>["medium"],
  "type"=>["video/webm; codecs=\"vp8.0, vorbis\""],
  "url"=>
   ["http://r1---sn-5go7ln7s.googlevideo.com/videoplayback?mt=1392659818&itag=43&id=a31cfc45e48f52de&ms=au&pcm2fr=yes&fexp=900225%2C927612%2C933212%2C929209%2C916608%2C936110%2C937417%2C913434%2C936910%2C936913%2C902907&sver=3&expire=1392686029&key=yt5&ip=176.10.249.16&mv=m&upn=fxzeAH1SPGc&sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2fr%2Cratebypass%2Csource%2Cupn%2Cexpire&source=youtube&ratebypass=yes&ipbits=0"],
  "fallback_host"=>["tc.v16.cache3.googlevideo.com"]},
...

Before we can use the download url we have to add a url paramater named signature that has the value of the sig key:

parsed.each do |entry|
  entry["url"].first << "&signature=#{entry["sig"].first}"
end

The urls can now be used to download the video.

Clone this wiki locally