]> git.sesse.net Git - vlc/blob - share/lua/playlist/youtube.lua
220717bae9e59bec55ba16c1d270da4574a682f4
[vlc] / share / lua / playlist / youtube.lua
1 --[[
2  $Id$
3
4  Copyright © 2007-2012 the VideoLAN team
5
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.
10
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.
15
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.
19 --]]
20
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.."=([^&]*)" )
24     return res
25 end
26
27 function get_arturl()
28     local iurl = get_url_param( vlc.path, "iurl" )
29     if iurl then
30         return iurl
31     end
32     local video_id = get_url_param( vlc.path, "v" )
33     if not video_id then
34         return nil
35     end
36     return "http://img.youtube.com/vi/"..video_id.."/default.jpg"
37 end
38
39 function get_prefres()
40     local prefres = -1
41     if vlc.var and vlc.var.inherit then
42         prefres = vlc.var.inherit(nil, "preferred-resolution")
43         if prefres == nil then
44             prefres = -1
45         end
46     end
47     return prefres
48 end
49
50 -- Pick the most suited format available
51 function get_fmt( fmt_list )
52     local prefres = get_prefres()
53     if prefres < 0 then
54         return nil
55     end
56
57     local fmt = nil
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
62         fmt = itag
63         if tonumber(height) <= prefres then
64             break
65         end
66     end
67     return fmt
68 end
69
70 -- Parse and pick our video URL
71 function pick_url( url_map, fmt )
72     local path = nil
73     for stream in string.gmatch( url_map, "[^,]+" ) do
74         -- Apparently formats are listed in quality order,
75         -- so we can afford to simply take the first one
76         local itag = string.match( stream, "itag=(%d+)" )
77         if not fmt or not itag or tonumber( itag ) == tonumber( fmt ) then
78             local url = string.match( stream, "url=([^&,]+)" )
79             if url then
80                 url = vlc.strings.decode_uri( url )
81
82                 local sig = string.match( stream, "sig=([^&,]+)" )
83                 local signature = ""
84                 if sig then
85                     signature = "&signature="..sig
86                 end
87
88                 path = url..signature
89                 break
90             end
91         end
92     end
93     return path
94 end
95
96 -- Probe function.
97 function probe()
98     if vlc.access ~= "http" and vlc.access ~= "https" then
99         return false
100     end
101     youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
102     if not youtube_site then
103         -- FIXME we should be using a builtin list of known youtube websites
104         -- like "fr.youtube.com", "uk.youtube.com" etc..
105         youtube_site = string.find( vlc.path, ".youtube.com" )
106         if youtube_site == nil then
107             return false
108         end
109     end
110     return (  string.match( vlc.path, "/watch%?" ) -- the html page
111             or string.match( vlc.path, "/get_video_info%?" ) -- info API
112             or string.match( vlc.path, "/v/" ) -- video in swf player
113             or string.match( vlc.path, "/player2.swf" ) ) -- another player url
114 end
115
116 -- Parse function.
117 function parse()
118     if string.match( vlc.path, "/watch%?" )
119     then -- This is the HTML page's URL
120         -- fmt is the format of the video
121         -- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs)
122         fmt = get_url_param( vlc.path, "fmt" )
123         while true do
124             -- Try to find the video's title
125             line = vlc.readline()
126             if not line then break end
127             if string.match( line, "<meta name=\"title\"" ) then
128                 _,_,name = string.find( line, "content=\"(.-)\"" )
129                 name = vlc.strings.resolve_xml_special_chars( name )
130                 name = vlc.strings.resolve_xml_special_chars( name )
131             end
132             if string.match( line, "<meta name=\"description\"" ) then
133                -- Don't ask me why they double encode ...
134                 _,_,description = string.find( line, "content=\"(.-)\"" )
135                 description = vlc.strings.resolve_xml_special_chars( description )
136                 description = vlc.strings.resolve_xml_special_chars( description )
137             end
138             if string.match( line, "<meta property=\"og:image\"" ) then
139                 _,_,arturl = string.find( line, "content=\"(.-)\"" )
140             end
141             if string.match( line, " rel=\"author\"" ) then
142                 _,_,artist = string.find( line, "href=\"/user/([^\"]*)\"" )
143             end
144             -- JSON parameters, also formerly known as "swfConfig",
145             -- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig" ...
146             if string.match( line, "ytplayer%.config" ) then
147                 if not fmt then
148                     fmt_list = string.match( line, "\"fmt_list\": \"(.-)\"" )
149                     if fmt_list then
150                         fmt_list = string.gsub( fmt_list, "\\/", "/" )
151                         fmt = get_fmt( fmt_list )
152                     end
153                 end
154
155                 url_map = string.match( line, "\"url_encoded_fmt_stream_map\": \"(.-)\"" )
156                 if url_map then
157                     -- FIXME: do this properly
158                     url_map = string.gsub( url_map, "\\u0026", "&" )
159                     path = pick_url( url_map, fmt )
160                 end
161
162                 if not path then
163                     -- If this is a live stream, the URL map will be empty
164                     -- and we get the URL from this field instead 
165                     local hlsvp = string.match( line, "\"hlsvp\": \"(.-)\"" )
166                     if hlsvp then
167                         hlsvp = string.gsub( hlsvp, "\\/", "/" )
168                         path = hlsvp
169                     end
170                 end
171             -- There is also another version of the parameters, encoded
172             -- differently, as an HTML attribute of an <object> or <embed>
173             -- tag; but we don't need it now
174             end
175         end
176
177         if not path then
178             local video_id = get_url_param( vlc.path, "v" )
179             if video_id then
180                 if fmt then
181                     format = "&fmt=" .. fmt
182                 else
183                     format = ""
184                 end 
185                 -- Without "el=detailpage", /get_video_info fails for many
186                 -- music videos with errors about copyrighted content being
187                 -- "restricted from playback on certain sites"
188                 path = "http://www.youtube.com/get_video_info?video_id="..video_id..format.."&el=detailpage"
189                 vlc.msg.warn( "Couldn't extract video URL, falling back to alternate youtube API" )
190             end
191         end
192
193         if not path then
194             vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
195             return { }
196         end
197
198         if not arturl then
199             arturl = get_arturl()
200         end
201
202         return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
203
204     elseif string.match( vlc.path, "/get_video_info%?" ) then -- video info API
205         local line = vlc.readline() -- data is on one line only
206
207         local fmt = get_url_param( vlc.path, "fmt" )
208         if not fmt then
209             local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
210             if fmt_list then
211                 fmt_list = vlc.strings.decode_uri( fmt_list )
212                 fmt = get_fmt( fmt_list )
213             end
214         end
215
216         local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
217         if url_map then
218             url_map = vlc.strings.decode_uri( url_map )
219             path = pick_url( url_map, fmt )
220         end
221
222         if not path then
223             -- If this is a live stream, the URL map will be empty
224             -- and we get the URL from this field instead 
225             local hlsvp = string.match( line, "&hlsvp=([^&]*)" )
226             if hlsvp then
227                 hlsvp = vlc.strings.decode_uri( hlsvp )
228                 path = hlsvp
229             end
230         end
231
232         if not path then
233             vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
234             return { }
235         end
236
237         local title = string.match( line, "&title=([^&]*)" )
238         if title then
239             title = string.gsub( title, "+", " " )
240             title = vlc.strings.decode_uri( title )
241         end
242         local artist = string.match( line, "&author=([^&]*)" )
243         local arturl = string.match( line, "&thumbnail_url=([^&]*)" )
244         if arturl then
245             arturl = vlc.strings.decode_uri( arturl )
246         end
247
248         return { { path = path, title = title, artist = artist, arturl = arturl } }
249
250     else -- This is the flash player's URL
251         video_id = get_url_param( vlc.path, "video_id" )
252         if not video_id then
253             _,_,video_id = string.find( vlc.path, "/v/([^?]*)" )
254         end
255         if not video_id then
256             vlc.msg.err( "Couldn't extract youtube video URL" )
257             return { }
258         end
259         fmt = get_url_param( vlc.path, "fmt" )
260         if fmt then
261             format = "&fmt=" .. fmt
262         else
263             format = ""
264         end
265         return { { path = "http://www.youtube.com/watch?v="..video_id..format } }
266     end
267 end