4 Copyright © 2007-2012 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 -- Parse and pick our video URL
71 function pick_url( url_map, fmt )
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=([^&,]+)" )
80 url = vlc.strings.decode_uri( url )
82 local sig = string.match( stream, "sig=([^&,]+)" )
85 signature = "&signature="..sig
98 if vlc.access ~= "http" and vlc.access ~= "https" then
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
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
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" )
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 )
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 )
138 if string.match( line, "<meta property=\"og:image\"" ) then
139 _,_,arturl = string.find( line, "content=\"(.-)\"" )
141 if string.match( line, " rel=\"author\"" ) then
142 _,_,artist = string.find( line, "href=\"/user/([^\"]*)\"" )
144 -- JSON parameters, also formerly known as "swfConfig",
145 -- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig" ...
146 if string.match( line, "ytplayer%.config" ) then
148 fmt_list = string.match( line, "\"fmt_list\": \"(.-)\"" )
150 fmt_list = string.gsub( fmt_list, "\\/", "/" )
151 fmt = get_fmt( fmt_list )
155 url_map = string.match( line, "\"url_encoded_fmt_stream_map\": \"(.-)\"" )
157 -- FIXME: do this properly
158 url_map = string.gsub( url_map, "\\u0026", "&" )
159 path = pick_url( url_map, fmt )
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\": \"(.-)\"" )
167 hlsvp = string.gsub( hlsvp, "\\/", "/" )
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
178 local video_id = get_url_param( vlc.path, "v" )
181 format = "&fmt=" .. fmt
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" )
194 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
199 arturl = get_arturl()
202 return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
204 elseif string.match( vlc.path, "/get_video_info%?" ) then -- video info API
205 local line = vlc.readline() -- data is on one line only
207 local fmt = get_url_param( vlc.path, "fmt" )
209 local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
211 fmt_list = vlc.strings.decode_uri( fmt_list )
212 fmt = get_fmt( fmt_list )
216 local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
218 url_map = vlc.strings.decode_uri( url_map )
219 path = pick_url( url_map, fmt )
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=([^&]*)" )
227 hlsvp = vlc.strings.decode_uri( hlsvp )
233 vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
237 local title = string.match( line, "&title=([^&]*)" )
239 title = string.gsub( title, "+", " " )
240 title = vlc.strings.decode_uri( title )
242 local artist = string.match( line, "&author=([^&]*)" )
243 local arturl = string.match( line, "&thumbnail_url=([^&]*)" )
245 arturl = vlc.strings.decode_uri( arturl )
248 return { { path = path, title = title, artist = artist, arturl = arturl } }
250 else -- This is the flash player's URL
251 video_id = get_url_param( vlc.path, "video_id" )
253 _,_,video_id = string.find( vlc.path, "/v/([^?]*)" )
256 vlc.msg.err( "Couldn't extract youtube video URL" )
259 fmt = get_url_param( vlc.path, "fmt" )
261 format = "&fmt=" .. fmt
265 return { { path = "http://www.youtube.com/watch?v="..video_id..format } }