2007-11-03 15:28:52 +01:00
|
|
|
--[[
|
|
|
|
$Id$
|
|
|
|
|
2011-08-07 08:47:10 +02:00
|
|
|
Copyright © 2007-2011 the VideoLAN team
|
2007-11-03 15:28:52 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
|
|
--]]
|
2007-05-15 00:27:36 +02:00
|
|
|
|
|
|
|
-- Helper function to get a parameter's value in a URL
|
|
|
|
function get_url_param( url, name )
|
2008-09-12 18:02:00 +02:00
|
|
|
local _, _, res = string.find( url, "[&?]"..name.."=([^&]*)" )
|
|
|
|
return res
|
2007-08-25 00:25:17 +02:00
|
|
|
end
|
|
|
|
|
2011-08-07 09:28:34 +02:00
|
|
|
function get_arturl()
|
2011-09-25 06:07:13 +02:00
|
|
|
local iurl = get_url_param( vlc.path, "iurl" )
|
|
|
|
if iurl then
|
|
|
|
return iurl
|
|
|
|
end
|
|
|
|
local video_id = get_url_param( vlc.path, "v" )
|
2011-08-07 09:28:34 +02:00
|
|
|
if not video_id then
|
|
|
|
return nil
|
2007-08-25 00:25:17 +02:00
|
|
|
end
|
2011-08-07 09:28:34 +02:00
|
|
|
return "http://img.youtube.com/vi/"..video_id.."/default.jpg"
|
2007-05-15 00:27:36 +02:00
|
|
|
end
|
|
|
|
|
2011-09-15 20:37:06 +02:00
|
|
|
function get_prefres()
|
|
|
|
local prefres = -1
|
|
|
|
if vlc.var and vlc.var.inherit then
|
|
|
|
prefres = vlc.var.inherit(nil, "preferred-resolution")
|
|
|
|
if prefres == nil then
|
|
|
|
prefres = -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return prefres
|
|
|
|
end
|
|
|
|
|
2007-05-15 00:27:36 +02:00
|
|
|
-- Probe function.
|
|
|
|
function probe()
|
2011-02-28 19:29:42 +01:00
|
|
|
if vlc.access ~= "http" and vlc.access ~= "https" then
|
2007-10-07 19:54:41 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
youtube_site = string.match( string.sub( vlc.path, 1, 8 ), "youtube" )
|
|
|
|
if not youtube_site then
|
|
|
|
-- FIXME we should be using a builtin list of known youtube websites
|
|
|
|
-- like "fr.youtube.com", "uk.youtube.com" etc..
|
|
|
|
youtube_site = string.find( vlc.path, ".youtube.com" )
|
|
|
|
if youtube_site == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2011-08-07 10:53:37 +02:00
|
|
|
return ( string.match( vlc.path, "/watch%?" ) -- the html page
|
|
|
|
or string.match( vlc.path, "/v/" ) -- video in swf player
|
|
|
|
or string.match( vlc.path, "/player2.swf" ) ) -- another player url
|
2007-05-15 00:27:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Parse function.
|
|
|
|
function parse()
|
2011-08-07 10:53:37 +02:00
|
|
|
if string.match( vlc.path, "/watch%?" )
|
2007-05-15 00:27:36 +02:00
|
|
|
then -- This is the HTML page's URL
|
2011-08-07 08:47:10 +02:00
|
|
|
-- fmt is the format of the video
|
|
|
|
-- (cf. http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs)
|
2010-06-04 16:19:14 +02:00
|
|
|
fmt = get_url_param( vlc.path, "fmt" )
|
2007-05-15 00:27:36 +02:00
|
|
|
while true do
|
|
|
|
-- Try to find the video's title
|
|
|
|
line = vlc.readline()
|
|
|
|
if not line then break end
|
|
|
|
if string.match( line, "<meta name=\"title\"" ) then
|
2008-09-12 18:02:00 +02:00
|
|
|
_,_,name = string.find( line, "content=\"(.-)\"" )
|
2010-06-13 13:38:18 +02:00
|
|
|
name = vlc.strings.resolve_xml_special_chars( name )
|
2010-10-04 15:34:53 +02:00
|
|
|
name = vlc.strings.resolve_xml_special_chars( name )
|
2007-05-15 00:27:36 +02:00
|
|
|
end
|
2007-05-17 22:34:58 +02:00
|
|
|
if string.match( line, "<meta name=\"description\"" ) then
|
2008-09-12 18:02:00 +02:00
|
|
|
-- Don't ask me why they double encode ...
|
2011-08-07 08:47:10 +02:00
|
|
|
_,_,description = string.find( line, "content=\"(.-)\"" )
|
|
|
|
description = vlc.strings.resolve_xml_special_chars( description )
|
|
|
|
description = vlc.strings.resolve_xml_special_chars( description )
|
2007-05-17 22:34:58 +02:00
|
|
|
end
|
2011-08-07 09:05:51 +02:00
|
|
|
if string.match( line, "<meta property=\"og:image\"" ) then
|
|
|
|
_,_,arturl = string.find( line, "content=\"(.-)\"" )
|
|
|
|
end
|
2011-08-07 08:54:59 +02:00
|
|
|
if string.match( line, " rel=\"author\"" ) then
|
|
|
|
_,_,artist = string.find( line, "href=\"/user/([^\"]*)\"" )
|
2007-05-17 22:34:58 +02:00
|
|
|
end
|
2011-08-07 08:47:10 +02:00
|
|
|
-- JSON parameters, also formerly known as "swfConfig",
|
2012-01-19 03:45:19 +01:00
|
|
|
-- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG" ...
|
|
|
|
if string.match( line, "playerConfig" ) then
|
2011-09-15 20:37:06 +02:00
|
|
|
if not fmt then
|
|
|
|
prefres = get_prefres()
|
|
|
|
if prefres >= 0 then
|
|
|
|
fmt_list = string.match( line, "\"fmt_list\": \"(.-)\"" )
|
|
|
|
if fmt_list then
|
|
|
|
for itag,height in string.gmatch( fmt_list, "(%d+)\\/%d+x(%d+)\\/[^,]+" ) do
|
|
|
|
-- Apparently formats are listed in quality
|
|
|
|
-- order, so we take the first one that works,
|
|
|
|
-- or fallback to the lowest quality
|
|
|
|
fmt = itag
|
|
|
|
if tonumber(height) <= prefres then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-07 08:47:10 +02:00
|
|
|
url_map = string.match( line, "\"url_encoded_fmt_stream_map\": \"(.-)\"" )
|
|
|
|
if url_map then
|
2011-08-04 20:32:00 +02:00
|
|
|
-- FIXME: do this properly
|
2011-08-07 08:47:10 +02:00
|
|
|
url_map = string.gsub( url_map, "\\u0026", "&" )
|
2012-01-09 02:25:51 +01:00
|
|
|
for url,itag in string.gmatch( url_map, "url=([^&,]+)[^,]*&itag=(%d+)" ) do
|
2010-06-04 16:19:14 +02:00
|
|
|
-- Apparently formats are listed in quality order,
|
|
|
|
-- so we can afford to simply take the first one
|
|
|
|
if not fmt or tonumber( itag ) == tonumber( fmt ) then
|
2011-08-05 21:37:48 +02:00
|
|
|
url = vlc.strings.decode_uri( url )
|
2010-06-04 16:19:14 +02:00
|
|
|
path = url
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-07 08:47:10 +02:00
|
|
|
-- There is also another version of the parameters, encoded
|
|
|
|
-- differently, as an HTML attribute of an <object> or <embed>
|
|
|
|
-- tag; but we don't need it now
|
2007-08-18 02:57:17 +02:00
|
|
|
end
|
2007-08-25 00:25:17 +02:00
|
|
|
end
|
2010-06-04 16:19:14 +02:00
|
|
|
|
|
|
|
if not path then
|
2011-08-07 08:47:10 +02:00
|
|
|
vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
|
|
|
|
return { }
|
2007-05-15 00:27:36 +02:00
|
|
|
end
|
2011-08-07 10:53:37 +02:00
|
|
|
|
|
|
|
if not arturl then
|
|
|
|
arturl = get_arturl()
|
|
|
|
end
|
|
|
|
|
2011-03-08 00:34:26 +01:00
|
|
|
return { { path = path; name = name; description = description; artist = artist; arturl = arturl } }
|
2007-05-15 00:27:36 +02:00
|
|
|
else -- This is the flash player's URL
|
2007-08-25 00:25:17 +02:00
|
|
|
video_id = get_url_param( vlc.path, "video_id" )
|
2011-08-07 10:53:37 +02:00
|
|
|
if not video_id then
|
|
|
|
_,_,video_id = string.find( vlc.path, "/v/([^?]*)" )
|
|
|
|
end
|
|
|
|
if not video_id then
|
|
|
|
vlc.msg.err( "Couldn't extract youtube video URL" )
|
|
|
|
return { }
|
|
|
|
end
|
2008-12-06 18:51:01 +01:00
|
|
|
fmt = get_url_param( vlc.path, "fmt" )
|
|
|
|
if fmt then
|
|
|
|
format = "&fmt=" .. fmt
|
|
|
|
else
|
|
|
|
format = ""
|
|
|
|
end
|
2011-08-07 10:53:37 +02:00
|
|
|
return { { path = "http://www.youtube.com/watch?v="..video_id..format } }
|
2007-05-15 00:27:36 +02:00
|
|
|
end
|
|
|
|
end
|