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=(.-)%(" )
95 -- Fetch the code of the descrambler function. Example:
96 -- function ij(a){a=a.split("");a=a.reverse();a=jj(a,12);a=jj(a,32);a=a.reverse();a=jj(a,34);a=a.slice(3);a=jj(a,35);a=jj(a,42);a=a.slice(2);return a.join("")}
101 line = table.remove( lines )
105 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
109 rules = string.match( line, "function "..descrambler.."%([^)]*%){(.-)}" )
112 -- Parse descrambling rules one by one and apply them on the
113 -- signature as we go
114 for rule in string.gmatch( rules, "[^;]+" ) do
116 if string.match( rule, "%.reverse%(" ) then
117 sig = string.reverse( sig )
121 local len = string.match( rule, "%.slice%((%d+)%)" )
123 sig = string.sub( sig, len + 1 )
127 -- This is known to be a function swapping the first and nth
129 -- function jj(a,b){var c=a[0];a[0]=a[b%a.length];a[b]=c;return a}
130 local idx = string.match( rule, "=..%([^,]+,(%d+)%)" )
131 -- This swapping function may also appear inlined:
132 -- var b=a[0];a[0]=a[59%a.length];a[59]=b;
133 -- In that case we only catch one of the three rules.
135 idx = string.match( rule, ".%[(%d+)%]=." )
138 idx = tonumber( idx )
139 if not idx then idx = 0 end
141 sig = string.gsub( sig, "^(.)("..string.rep( ".", idx - 1 )..")(.)(.*)$", "%3%2%1%4" )
143 sig = string.gsub( sig, "^(.)(.)", "%2%1" )
147 -- Simply ignore other statements, in particular initial split
148 -- and final join and return statements
153 function descramble81( sig )
154 sig = string.reverse( sig )
155 local s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13 =
156 string.match( sig, "(.)(.......................)(.)(..............)(.)(......)(.)(....)(.)(...................)(.)(........)(.)" )
157 return s3..s2..s5..s4..s1..s6..s13..s8..s7..s10..s9..s12..s11
160 local descramblers = {
161 --[81] = descramble81
164 function descramble( sig, js_url )
165 vlc.msg.dbg( "Found "..string.len( sig ).."-character scrambled signature for youtube video URL, attempting to descramble... " )
167 sig = js_descramble( sig, js_url )
169 local descrambler = descramblers[string.len( sig )]
171 sig = descrambler( sig )
173 vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
179 -- Parse and pick our video URL
180 function pick_url( url_map, fmt, js_url )
182 for stream in string.gmatch( url_map, "[^,]+" ) do
183 -- Apparently formats are listed in quality order,
184 -- so we can afford to simply take the first one
185 local itag = string.match( stream, "itag=(%d+)" )
186 if not fmt or not itag or tonumber( itag ) == tonumber( fmt ) then
187 local url = string.match( stream, "url=([^&,]+)" )
189 url = vlc.strings.decode_uri( url )
191 local sig = string.match( stream, "sig=([^&,]+)" )
193 -- Scrambled signature
194 sig = string.match( stream, "s=([^&,]+)" )
196 sig = descramble( sig, js_url )
201 signature = "&signature="..sig
204 path = url..signature
214 if vlc.access ~= "http" and vlc.access ~= "https" then
217 youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
218 if not youtube_site then
219 -- FIXME we should be using a builtin list of known youtube websites
220 -- like "fr.youtube.com", "uk.youtube.com" etc..
221 youtube_site = string.find( vlc.path, ".youtube.com" )
222 if youtube_site == nil then
226 return ( string.match( vlc.path, "/watch%?" ) -- the html page
227 or string.match( vlc.path, "/get_video_info%?" ) -- info API
228 or string.match( vlc.path, "/v/" ) -- video in swf player
229 or string.match( vlc.path, "/player2.swf" ) ) -- another player url
234 if string.match( vlc.path, "/watch%?" )
235 then -- This is the HTML page's URL
236 -- fmt is the format of the video
237 -- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs)
238 fmt = get_url_param( vlc.path, "fmt" )
240 -- Try to find the video's title
241 line = vlc.readline()
242 if not line then break end
243 if string.match( line, "<meta name=\"title\"" ) then
244 _,_,name = string.find( line, "content=\"(.-)\"" )
245 name = vlc.strings.resolve_xml_special_chars( name )
246 name = vlc.strings.resolve_xml_special_chars( name )
248 if string.match( line, "<meta name=\"description\"" ) then
249 -- Don't ask me why they double encode ...
250 _,_,description = string.find( line, "content=\"(.-)\"" )
251 description = vlc.strings.resolve_xml_special_chars( description )
252 description = vlc.strings.resolve_xml_special_chars( description )
254 if string.match( line, "<meta property=\"og:image\"" ) then
255 _,_,arturl = string.find( line, "content=\"(.-)\"" )
257 -- This is not available in the video parameters (whereas it
258 -- is given by the get_video_info API as the "author" field)
260 artist = string.match( line, "yt%-uix%-sessionlink yt%-user%-name[^>]*>([^<]*)</" )
262 artist = vlc.strings.resolve_xml_special_chars( artist )
265 -- JSON parameters, also formerly known as "swfConfig",
266 -- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig" ...
267 if string.match( line, "ytplayer%.config" ) then
269 local js_url = string.match( line, "\"js\": \"(.-)\"" )
271 js_url = string.gsub( js_url, "\\/", "/" )
272 js_url = string.gsub( js_url, "^//", vlc.access.."://" )
276 fmt_list = string.match( line, "\"fmt_list\": \"(.-)\"" )
278 fmt_list = string.gsub( fmt_list, "\\/", "/" )
279 fmt = get_fmt( fmt_list )
283 url_map = string.match( line, "\"url_encoded_fmt_stream_map\": \"(.-)\"" )
285 -- FIXME: do this properly
286 url_map = string.gsub( url_map, "\\u0026", "&" )
287 path = pick_url( url_map, fmt, js_url )
291 -- If this is a live stream, the URL map will be empty
292 -- and we get the URL from this field instead
293 local hlsvp = string.match( line, "\"hlsvp\": \"(.-)\"" )
295 hlsvp = string.gsub( hlsvp, "\\/", "/" )
299 -- There is also another version of the parameters, encoded
300 -- differently, as an HTML attribute of an <object> or <embed>
301 -- tag; but we don't need it now
306 local video_id = get_url_param( vlc.path, "v" )
309 format = "&fmt=" .. fmt
313 -- Without "el=detailpage", /get_video_info fails for many
314 -- music videos with errors about copyrighted content being
315 -- "restricted from playback on certain sites"
316 path = "http://www.youtube.com/get_video_info?video_id="..video_id..format.."&el=detailpage"
317 vlc.msg.warn( "Couldn't extract video URL, falling back to alternate youtube API" )
322 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
327 arturl = get_arturl()
330 return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
332 elseif string.match( vlc.path, "/get_video_info%?" ) then -- video info API
333 local line = vlc.readline() -- data is on one line only
335 local fmt = get_url_param( vlc.path, "fmt" )
337 local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
339 fmt_list = vlc.strings.decode_uri( fmt_list )
340 fmt = get_fmt( fmt_list )
344 local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
346 url_map = vlc.strings.decode_uri( url_map )
347 path = pick_url( url_map, fmt )
351 -- If this is a live stream, the URL map will be empty
352 -- and we get the URL from this field instead
353 local hlsvp = string.match( line, "&hlsvp=([^&]*)" )
355 hlsvp = vlc.strings.decode_uri( hlsvp )
361 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
365 local title = string.match( line, "&title=([^&]*)" )
367 title = string.gsub( title, "+", " " )
368 title = vlc.strings.decode_uri( title )
370 local artist = string.match( line, "&author=([^&]*)" )
372 artist = string.gsub( artist, "+", " " )
373 artist = vlc.strings.decode_uri( artist )
375 local arturl = string.match( line, "&thumbnail_url=([^&]*)" )
377 arturl = vlc.strings.decode_uri( arturl )
380 return { { path = path, title = title, artist = artist, arturl = arturl } }
382 else -- This is the flash player's URL
383 video_id = get_url_param( vlc.path, "video_id" )
385 _,_,video_id = string.find( vlc.path, "/v/([^?]*)" )
388 vlc.msg.err( "Couldn't extract youtube video URL" )
391 fmt = get_url_param( vlc.path, "fmt" )
393 format = "&fmt=" .. fmt
397 return { { path = "http://www.youtube.com/watch?v="..video_id..format } }