4 Copyright © 2007-2013 the VideoLAN team
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 -- Helper function to get a parameter's value in a URL
22 function get_url_param( url, name )
23 local _, _, res = string.find( url, "[&?]"..name.."=([^&]*)" )
28 local iurl = get_url_param( vlc.path, "iurl" )
32 local video_id = get_url_param( vlc.path, "v" )
36 return "http://img.youtube.com/vi/"..video_id.."/default.jpg"
39 function get_prefres()
41 if vlc.var and vlc.var.inherit then
42 prefres = vlc.var.inherit(nil, "preferred-resolution")
43 if prefres == nil then
50 -- Pick the most suited format available
51 function get_fmt( fmt_list )
52 local prefres = get_prefres()
58 for itag,height in string.gmatch( fmt_list, "(%d+)/%d+x(%d+)/[^,]+" ) do
59 -- Apparently formats are listed in quality
60 -- order, so we take the first one that works,
61 -- or fallback to the lowest quality
63 if tonumber(height) <= prefres then
70 -- Descramble the URL signature using the javascript code that does that
72 function js_descramble( sig, js_url )
73 -- Fetch javascript code
74 local js = vlc.stream( js_url )
80 -- Look for the descrambler function's name
81 local descrambler = nil
82 while not descrambler do
83 local line = js:readline()
85 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
88 -- Buffer lines for later, so we don't have to make a second
90 table.insert( lines, line )
91 -- c&&(b.signature=ij(c));
92 -- descrambler = string.match( line, "%.signature=(.-)%(" )
93 -- descrambler = string.match( line, "%.sig%|%|(.-)%(" )
94 descrambler = string.match( line, "%.sig||([a-zA-Z0-9]+)%(" )
97 -- Fetch the code of the descrambler function. The function is
98 -- conveniently preceded by the definition of a helper object
99 -- that it uses. Example:
100 -- var Fo={TR:function(a){a.reverse()},TU:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b]=c},sH:function(a,b){a.splice(0,b)}};function Go(a){a=a.split("");Fo.sH(a,2);Fo.TU(a,28);Fo.TU(a,44);Fo.TU(a,26);Fo.TU(a,40);Fo.TU(a,64);Fo.TR(a,26);Fo.sH(a,1);return a.join("")};
101 local transformations = nil
103 while not transformations and not rules do
106 line = table.remove( lines )
110 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
114 transformations, rules = string.match( line, "var ..={(.-)};function "..descrambler.."%([^)]*%){(.-)}" )
117 -- Parse the helper object to map available transformations
119 for meth,code in string.gmatch( transformations, "(..):function%([^)]*%){([^}]*)}" ) do
121 if string.match( code, "%.reverse%(" ) then
122 trans[meth] = "reverse"
125 elseif string.match( code, "%.splice%(") then
126 trans[meth] = "slice"
128 -- var c=a[0];a[0]=a[b%a.length];a[b]=c
129 elseif string.match( code, "var c=" ) then
132 vlc.msg.warn("Couldn't parse unknown youtube video URL signature transformation")
136 -- Parse descrambling rules, map them to known transformations
137 -- and apply them on the signature
138 local missing = false
139 for meth,idx in string.gmatch( rules, "..%.(..)%([^,]+,(%d+)%)" ) do
140 idx = tonumber( idx )
142 if trans[meth] == "reverse" then
143 sig = string.reverse( sig )
145 elseif trans[meth] == "slice" then
146 sig = string.sub( sig, idx + 1 )
148 elseif trans[meth] == "swap" then
150 sig = string.gsub( sig, "^(.)("..string.rep( ".", idx - 1 )..")(.)(.*)$", "%3%2%1%4" )
152 sig = string.gsub( sig, "^(.)(.)", "%2%1" )
155 vlc.msg.dbg("Couldn't apply unknown youtube video URL signature transformation")
160 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
165 -- Parse and pick our video URL
166 function pick_url( url_map, fmt, js_url )
168 for stream in string.gmatch( url_map, "[^,]+" ) do
169 -- Apparently formats are listed in quality order,
170 -- so we can afford to simply take the first one
171 local itag = string.match( stream, "itag=(%d+)" )
172 if not fmt or not itag or tonumber( itag ) == tonumber( fmt ) then
173 local url = string.match( stream, "url=([^&,]+)" )
175 url = vlc.strings.decode_uri( url )
177 local sig = string.match( stream, "sig=([^&,]+)" )
179 -- Scrambled signature
180 sig = string.match( stream, "s=([^&,]+)" )
182 vlc.msg.dbg( "Found "..string.len( sig ).."-character scrambled signature for youtube video URL, attempting to descramble... " )
184 sig = js_descramble( sig, js_url )
186 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
192 signature = "&signature="..sig
195 path = url..signature
205 if vlc.access ~= "http" and vlc.access ~= "https" then
208 youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
209 if not youtube_site then
210 -- FIXME we should be using a builtin list of known youtube websites
211 -- like "fr.youtube.com", "uk.youtube.com" etc..
212 youtube_site = string.find( vlc.path, ".youtube.com" )
213 if youtube_site == nil then
217 return ( string.match( vlc.path, "/watch%?" ) -- the html page
218 or string.match( vlc.path, "/get_video_info%?" ) -- info API
219 or string.match( vlc.path, "/v/" ) -- video in swf player
220 or string.match( vlc.path, "/embed/" ) -- embedded player iframe
221 or string.match( vlc.path, "/player2.swf" ) ) -- another player url
226 if string.match( vlc.path, "/watch%?" )
227 then -- This is the HTML page's URL
228 -- fmt is the format of the video
229 -- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs)
230 fmt = get_url_param( vlc.path, "fmt" )
232 -- Try to find the video's title
233 line = vlc.readline()
234 if not line then break end
235 if string.match( line, "<meta name=\"title\"" ) then
236 _,_,name = string.find( line, "content=\"(.-)\"" )
237 name = vlc.strings.resolve_xml_special_chars( name )
238 name = vlc.strings.resolve_xml_special_chars( name )
240 if string.match( line, "<meta name=\"description\"" ) then
241 -- Don't ask me why they double encode ...
242 _,_,description = string.find( line, "content=\"(.-)\"" )
243 description = vlc.strings.resolve_xml_special_chars( description )
244 description = vlc.strings.resolve_xml_special_chars( description )
246 if string.match( line, "<meta property=\"og:image\"" ) then
247 _,_,arturl = string.find( line, "content=\"(.-)\"" )
249 -- This is not available in the video parameters (whereas it
250 -- is given by the get_video_info API as the "author" field)
252 artist = string.match( line, "yt%-uix%-sessionlink yt%-user%-name[^>]*>([^<]*)</" )
254 artist = vlc.strings.resolve_xml_special_chars( artist )
257 -- JSON parameters, also formerly known as "swfConfig",
258 -- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig" ...
259 if string.match( line, "ytplayer%.config" ) then
261 local js_url = string.match( line, "\"js\": *\"(.-)\"" )
263 js_url = string.gsub( js_url, "\\/", "/" )
264 js_url = string.gsub( js_url, "^//", vlc.access.."://" )
268 fmt_list = string.match( line, "\"fmt_list\": *\"(.-)\"" )
270 fmt_list = string.gsub( fmt_list, "\\/", "/" )
271 fmt = get_fmt( fmt_list )
275 url_map = string.match( line, "\"url_encoded_fmt_stream_map\": *\"(.-)\"" )
277 -- FIXME: do this properly
278 url_map = string.gsub( url_map, "\\u0026", "&" )
279 path = pick_url( url_map, fmt, js_url )
283 -- If this is a live stream, the URL map will be empty
284 -- and we get the URL from this field instead
285 local hlsvp = string.match( line, "\"hlsvp\": *\"(.-)\"" )
287 hlsvp = string.gsub( hlsvp, "\\/", "/" )
291 -- There is also another version of the parameters, encoded
292 -- differently, as an HTML attribute of an <object> or <embed>
293 -- tag; but we don't need it now
298 local video_id = get_url_param( vlc.path, "v" )
301 format = "&fmt=" .. fmt
305 -- Without "el=detailpage", /get_video_info fails for many
306 -- music videos with errors about copyrighted content being
307 -- "restricted from playback on certain sites"
308 path = "http://www.youtube.com/get_video_info?video_id="..video_id..format.."&el=detailpage"
309 vlc.msg.warn( "Couldn't extract video URL, falling back to alternate youtube API" )
314 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
319 arturl = get_arturl()
322 return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
324 elseif string.match( vlc.path, "/get_video_info%?" ) then -- video info API
325 local line = vlc.readline() -- data is on one line only
327 local fmt = get_url_param( vlc.path, "fmt" )
329 local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
331 fmt_list = vlc.strings.decode_uri( fmt_list )
332 fmt = get_fmt( fmt_list )
336 local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
338 url_map = vlc.strings.decode_uri( url_map )
339 path = pick_url( url_map, fmt )
343 -- If this is a live stream, the URL map will be empty
344 -- and we get the URL from this field instead
345 local hlsvp = string.match( line, "&hlsvp=([^&]*)" )
347 hlsvp = vlc.strings.decode_uri( hlsvp )
353 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
357 local title = string.match( line, "&title=([^&]*)" )
359 title = string.gsub( title, "+", " " )
360 title = vlc.strings.decode_uri( title )
362 local artist = string.match( line, "&author=([^&]*)" )
364 artist = string.gsub( artist, "+", " " )
365 artist = vlc.strings.decode_uri( artist )
367 local arturl = string.match( line, "&thumbnail_url=([^&]*)" )
369 arturl = vlc.strings.decode_uri( arturl )
372 return { { path = path, title = title, artist = artist, arturl = arturl } }
374 else -- This is the flash player's URL
375 video_id = get_url_param( vlc.path, "video_id" )
377 _,_,video_id = string.find( vlc.path, "/v/([^?]*)" )
380 video_id = string.match( vlc.path, "/embed/([^?]*)" )
383 vlc.msg.err( "Couldn't extract youtube video URL" )
386 fmt = get_url_param( vlc.path, "fmt" )
388 format = "&fmt=" .. fmt
392 return { { path = "http://www.youtube.com/watch?v="..video_id..format } }