2007-11-03 15:28:52 +01:00
|
|
|
|
--[[
|
|
|
|
|
|
2023-02-09 09:20:55 +01:00
|
|
|
|
Copyright © 2007-2023 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
|
|
|
|
|
|
2016-10-24 08:01:08 +02:00
|
|
|
|
-- Helper function to copy a parameter when building a new URL
|
|
|
|
|
function copy_url_param( url, name )
|
|
|
|
|
local value = get_url_param( url, name )
|
|
|
|
|
return ( value and "&"..name.."="..value or "" ) -- Ternary operator
|
|
|
|
|
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
|
2015-10-29 03:40:39 +01:00
|
|
|
|
return vlc.access.."://img.youtube.com/vi/"..video_id.."/default.jpg"
|
2007-05-15 00:27:36 +02:00
|
|
|
|
end
|
|
|
|
|
|
2012-09-18 01:13:28 +02:00
|
|
|
|
-- Pick the most suited format available
|
2012-09-17 05:54:59 +02:00
|
|
|
|
function get_fmt( fmt_list )
|
2015-10-29 03:46:11 +01:00
|
|
|
|
local prefres = vlc.var.inherit(nil, "preferred-resolution")
|
2012-09-17 05:54:59 +02:00
|
|
|
|
if prefres < 0 then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local fmt = nil
|
2017-11-21 21:59:35 +01:00
|
|
|
|
for itag,height in string.gmatch( fmt_list, "(%d+)/%d+x(%d+)[^,]*" ) do
|
2012-09-17 05:54:59 +02:00
|
|
|
|
-- 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
|
|
|
|
|
return fmt
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-04 19:17:23 +01:00
|
|
|
|
-- Helper emulating vlc.readline() to work around its failure on
|
|
|
|
|
-- very long lines (see #24957)
|
|
|
|
|
function read_long_line()
|
|
|
|
|
local eol
|
|
|
|
|
local pos = 0
|
|
|
|
|
local len = 32768
|
|
|
|
|
repeat
|
|
|
|
|
len = len * 2
|
|
|
|
|
local line = vlc.peek( len )
|
|
|
|
|
if not line then return nil end
|
|
|
|
|
eol = string.find( line, "\n", pos + 1 )
|
|
|
|
|
pos = len
|
|
|
|
|
until eol or len >= 1024 * 1024 -- No EOF detection, loop until limit
|
|
|
|
|
return vlc.read( eol or len )
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-11 14:17:58 +01:00
|
|
|
|
-- Buffering iterator to parse through the HTTP stream several times
|
|
|
|
|
-- without making several HTTP requests
|
|
|
|
|
function buf_iter( s )
|
|
|
|
|
s.i = s.i + 1
|
|
|
|
|
local line = s.lines[s.i]
|
|
|
|
|
if not line then
|
2015-11-25 02:50:10 +01:00
|
|
|
|
-- Put back together statements split across several lines,
|
|
|
|
|
-- otherwise we won't be able to parse them
|
|
|
|
|
repeat
|
|
|
|
|
local l = s.stream:readline()
|
|
|
|
|
if not l then break end
|
|
|
|
|
line = line and line..l or l -- Ternary operator
|
|
|
|
|
until string.match( line, "};$" )
|
|
|
|
|
|
2015-11-11 14:17:58 +01:00
|
|
|
|
if line then
|
|
|
|
|
s.lines[s.i] = line
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return line
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-11 16:14:21 +01:00
|
|
|
|
-- Helper to search and extract code from javascript stream
|
2016-10-24 08:03:17 +02:00
|
|
|
|
function js_extract( js, pattern )
|
2015-11-11 16:14:21 +01:00
|
|
|
|
js.i = 0 -- Reset to beginning
|
|
|
|
|
for line in buf_iter, js do
|
|
|
|
|
local ex = string.match( line, pattern )
|
|
|
|
|
if ex then
|
|
|
|
|
return ex
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-19 00:48:06 +02:00
|
|
|
|
-- Descramble the "n" parameter using the javascript code that does that
|
|
|
|
|
-- in the web page
|
|
|
|
|
function n_descramble( nparam, js )
|
2023-02-09 10:51:05 +01:00
|
|
|
|
if not js.stream then
|
|
|
|
|
if not js.url then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
js.stream = vlc.stream( js_url )
|
|
|
|
|
if not js.stream then
|
|
|
|
|
-- Retry once for transient errors
|
|
|
|
|
js.stream = vlc.stream( js_url )
|
|
|
|
|
if not js.stream then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-10-19 00:48:06 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Look for the descrambler function's name
|
2022-02-05 01:43:05 +01:00
|
|
|
|
-- a.C&&(b=a.get("n"))&&(b=Bpa[0](b),a.set("n",b),Bpa.length||iha(""))}};
|
|
|
|
|
-- var Bpa=[iha];
|
|
|
|
|
local callsite = js_extract( js, '[^;]*%.set%("n",[^};]*' )
|
|
|
|
|
if not callsite then
|
|
|
|
|
vlc.msg.dbg( "Couldn't extract YouTube video throttling parameter descrambling function name" )
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Try direct function name from following clause
|
|
|
|
|
local descrambler = string.match( callsite, '%.set%("n",.%),...?%.length||(...?)%(' )
|
|
|
|
|
local itm = nil
|
|
|
|
|
if not descrambler then
|
|
|
|
|
-- Try from main call site
|
|
|
|
|
descrambler = string.match( callsite, '[=%(,&|]([a-zA-Z0-9_$%[%]]+)%(.%),.%.set%("n",' )
|
|
|
|
|
if descrambler then
|
|
|
|
|
-- Check if this is only an intermediate variable
|
|
|
|
|
itm = string.match( descrambler, '^([^%[%]]+)%[' )
|
|
|
|
|
if itm then
|
|
|
|
|
descrambler = nil
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
-- Last chance: intermediate variable in following clause
|
|
|
|
|
itm = string.match( callsite, '%.set%("n",.%),(...?)%.length' )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not descrambler and itm then
|
|
|
|
|
-- Resolve intermediate variable
|
|
|
|
|
descrambler = js_extract( js, 'var '..itm..'=%[(...?)[%],]' )
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-19 00:48:06 +02:00
|
|
|
|
if not descrambler then
|
|
|
|
|
vlc.msg.dbg( "Couldn't extract YouTube video throttling parameter descrambling function name" )
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Fetch the code of the descrambler function
|
|
|
|
|
-- lha=function(a){var b=a.split(""),c=[310282131,"KLf3",b,null,function(d,e){d.push(e)},-45817231, [data and transformations...] ,1248130556];c[3]=c;c[15]=c;c[18]=c;try{c[40](c[14],c[2]),c[25](c[48]),c[21](c[32],c[23]), [scripted calls...] ,c[25](c[33],c[3])}catch(d){return"enhanced_except_4ZMBnuz-_w8_"+a}return b.join("")};
|
|
|
|
|
local code = js_extract( js, "^"..descrambler.."=function%([^)]*%){(.-)};" )
|
|
|
|
|
if not code then
|
|
|
|
|
vlc.msg.dbg( "Couldn't extract YouTube video throttling parameter descrambling code" )
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Split code into two main sections: 1/ data and transformations,
|
|
|
|
|
-- and 2/ a script of calls
|
|
|
|
|
local datac, script = string.match( code, "c=%[(.*)%];.-;try{(.*)}catch%(" )
|
|
|
|
|
if ( not datac ) or ( not script ) then
|
|
|
|
|
vlc.msg.dbg( "Couldn't extract YouTube video throttling parameter descrambling rules" )
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Split "n" parameter into a table as descrambling operates on it
|
|
|
|
|
-- as one of several arrays
|
|
|
|
|
local n = {}
|
|
|
|
|
for c in string.gmatch( nparam, "." ) do
|
|
|
|
|
table.insert( n, c )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Helper
|
|
|
|
|
local table_len = function( tab )
|
|
|
|
|
local len = 0
|
|
|
|
|
for i, val in ipairs( tab ) do
|
|
|
|
|
len = len + 1
|
|
|
|
|
end
|
|
|
|
|
return len
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-15 00:58:35 +01:00
|
|
|
|
-- Shared core section of compound transformations: it compounds
|
|
|
|
|
-- the "n" parameter with an input string, character by character,
|
|
|
|
|
-- using a Base64 alphabet as algebraic modulo group.
|
2021-11-15 00:20:46 +01:00
|
|
|
|
-- var h=f.length;d.forEach(function(l,m,n){this.push(n[m]=f[(f.indexOf(l)-f.indexOf(this[m])+m+h--)%f.length])},e.split(""))
|
|
|
|
|
local compound = function( ntab, str, alphabet )
|
2021-11-15 00:58:35 +01:00
|
|
|
|
if ntab ~= n or
|
|
|
|
|
type( str ) ~= "string" or
|
|
|
|
|
type( alphabet ) ~= "string" then
|
2021-10-19 00:48:06 +02:00
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
local input = {}
|
|
|
|
|
for c in string.gmatch( str, "." ) do
|
|
|
|
|
table.insert( input, c )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local len = string.len( alphabet )
|
|
|
|
|
for i, c in ipairs( ntab ) do
|
|
|
|
|
if type( c ) ~= "string" then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
local pos1 = string.find( alphabet, c, 1, true )
|
|
|
|
|
local pos2 = string.find( alphabet, input[i], 1, true )
|
|
|
|
|
if ( not pos1 ) or ( not pos2 ) then
|
|
|
|
|
return true
|
|
|
|
|
end
|
2021-11-15 00:20:46 +01:00
|
|
|
|
local pos = ( pos1 - pos2 ) % len
|
2021-10-19 00:48:06 +02:00
|
|
|
|
local newc = string.sub( alphabet, pos + 1, pos + 1 )
|
|
|
|
|
ntab[i] = newc
|
|
|
|
|
table.insert( input, newc )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- The data section contains among others function code for a number
|
|
|
|
|
-- of transformations, most of which are basic array operations.
|
|
|
|
|
-- We can match these functions' code to identify them, and emulate
|
|
|
|
|
-- the corresponding transformations.
|
|
|
|
|
local trans = {
|
|
|
|
|
reverse = {
|
|
|
|
|
func = function( tab )
|
|
|
|
|
local len = table_len( tab )
|
|
|
|
|
local tmp = {}
|
|
|
|
|
for i, val in ipairs( tab ) do
|
|
|
|
|
tmp[len - i + 1] = val
|
|
|
|
|
end
|
|
|
|
|
for i, val in ipairs( tmp ) do
|
|
|
|
|
tab[i] = val
|
|
|
|
|
end
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d){d.reverse()}
|
|
|
|
|
-- function(d){for(var e=d.length;e;)d.push(d.splice(--e,1)[0])}
|
|
|
|
|
"^function%(d%)",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
append = {
|
|
|
|
|
func = function( tab, val )
|
|
|
|
|
table.insert( tab, val )
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){d.push(e)}
|
|
|
|
|
"^function%(d,e%){d%.push%(e%)},",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
remove = {
|
|
|
|
|
func = function( tab, i )
|
|
|
|
|
if type( i ) ~= "number" then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
i = i % table_len( tab )
|
|
|
|
|
table.remove( tab, i + 1 )
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){e=(e%d.length+d.length)%d.length;d.splice(e,1)}
|
|
|
|
|
"^[^}]-;d%.splice%(e,1%)},",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
swap = {
|
|
|
|
|
func = function( tab, i )
|
|
|
|
|
if type( i ) ~= "number" then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
i = i % table_len( tab )
|
|
|
|
|
local tmp = tab[1]
|
|
|
|
|
tab[1] = tab[i + 1]
|
|
|
|
|
tab[i + 1] = tmp
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){e=(e%d.length+d.length)%d.length;var f=d[0];d[0]=d[e];d[e]=f}
|
|
|
|
|
-- function(d,e){e=(e%d.length+d.length)%d.length;d.splice(0,1,d.splice(e,1,d[0])[0])}
|
|
|
|
|
"^[^}]-;var f=d%[0%];d%[0%]=d%[e%];d%[e%]=f},",
|
|
|
|
|
"^[^}]-;d%.splice%(0,1,d%.splice%(e,1,d%[0%]%)%[0%]%)},",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
rotate = {
|
|
|
|
|
func = function( tab, shift )
|
|
|
|
|
if type( shift ) ~= "number" then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
local len = table_len( tab )
|
|
|
|
|
shift = shift % len
|
|
|
|
|
local tmp = {}
|
|
|
|
|
for i, val in ipairs( tab ) do
|
|
|
|
|
tmp[( i - 1 + shift ) % len + 1] = val
|
|
|
|
|
end
|
|
|
|
|
for i, val in ipairs( tmp ) do
|
|
|
|
|
tab[i] = val
|
|
|
|
|
end
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){for(e=(e%d.length+d.length)%d.length;e--;)d.unshift(d.pop())}
|
|
|
|
|
-- function(d,e){e=(e%d.length+d.length)%d.length;d.splice(-e).reverse().forEach(function(f){d.unshift(f)})}
|
|
|
|
|
"^[^}]-d%.unshift%(d.pop%(%)%)},",
|
|
|
|
|
"^[^}]-d%.unshift%(f%)}%)},",
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-11-15 00:58:35 +01:00
|
|
|
|
-- Here functions with no arguments are not really functions,
|
|
|
|
|
-- they're constants: treat them as such. These alphabets are
|
|
|
|
|
-- passed to and used by the compound transformations.
|
|
|
|
|
alphabet1 = {
|
|
|
|
|
func = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_",
|
|
|
|
|
match = {
|
|
|
|
|
-- function(){for(var d=64,e=[];++d-e.length-32;){switch(d){case 91:d=44;continue;case 123:d=65;break;case 65:d-=18;continue;case 58:d=96;continue;case 46:d=95}e.push(String.fromCharCode(d))}return e}
|
|
|
|
|
"^function%(%){[^}]-case 58:d=96;",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
alphabet2 = {
|
|
|
|
|
func = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
|
|
|
|
|
match = {
|
2021-11-23 18:07:33 +01:00
|
|
|
|
-- function(){for(var d=64,e=[];++d-e.length-32;){switch(d){case 58:d-=14;case 91:case 92:case 93:continue;case 123:d=47;case 94:case 95:case 96:continue;case 46:d=95}e.push(String.fromCharCode(d))}return e}
|
2021-11-15 00:58:35 +01:00
|
|
|
|
-- function(){for(var d=64,e=[];++d-e.length-32;)switch(d){case 46:d=95;default:e.push(String.fromCharCode(d));case 94:case 95:case 96:break;case 123:d-=76;case 92:case 93:continue;case 58:d=44;case 91:}return e}
|
|
|
|
|
"^function%(%){[^}]-case 58:d%-=14;",
|
|
|
|
|
"^function%(%){[^}]-case 58:d=44;",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
-- Compound transformations are based on a shared core section
|
|
|
|
|
-- that compounds the "n" parameter with an input string,
|
|
|
|
|
-- character by character, using a variation of a Base64
|
|
|
|
|
-- alphabet as algebraic modulo group.
|
|
|
|
|
compound = {
|
|
|
|
|
func = compound,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e,f){var h=f.length;d.forEach(function(l,m,n){this.push(n[m]=f[(f.indexOf(l)-f.indexOf(this[m])+m+h--)%f.length])},e.split(""))}
|
|
|
|
|
"^function%(d,e,f%)",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
-- These compound transformation variants first build their
|
|
|
|
|
-- Base64 alphabet themselves, before using it.
|
2021-10-19 00:48:06 +02:00
|
|
|
|
compound1 = {
|
|
|
|
|
func = function( ntab, str )
|
2021-11-15 00:20:46 +01:00
|
|
|
|
return compound( ntab, str, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){for(var f=64,h=[];++f-h.length-32;)switch(f){case 58:f=96;continue;case 91:f=44;break;case 65:f=47;continue;case 46:f=153;case 123:f-=58;default:h.push(String.fromCharCode(f))} [ compound... ] }
|
2021-11-15 00:58:35 +01:00
|
|
|
|
"^function%(d,e%){[^}]-case 58:f=96;",
|
2021-10-19 00:48:06 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
compound2 = {
|
|
|
|
|
func = function( ntab, str )
|
2021-11-15 00:20:46 +01:00
|
|
|
|
return compound( ntab, str,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
-- function(d,e){for(var f=64,h=[];++f-h.length-32;){switch(f){case 58:f-=14;case 91:case 92:case 93:continue;case 123:f=47;case 94:case 95:case 96:continue;case 46:f=95}h.push(String.fromCharCode(f))} [ compound... ] }
|
|
|
|
|
-- function(d,e){for(var f=64,h=[];++f-h.length-32;)switch(f){case 46:f=95;default:h.push(String.fromCharCode(f));case 94:case 95:case 96:break;case 123:f-=76;case 92:case 93:continue;case 58:f=44;case 91:} [ compound... ] }
|
2021-11-15 00:58:35 +01:00
|
|
|
|
"^function%(d,e%){[^}]-case 58:f%-=14;",
|
|
|
|
|
"^function%(d,e%){[^}]-case 58:f=44;",
|
2021-10-19 00:48:06 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
-- Fallback
|
|
|
|
|
unid = {
|
|
|
|
|
func = function( )
|
|
|
|
|
vlc.msg.dbg( "Couldn't apply unidentified YouTube video throttling parameter transformation, aborting descrambling" )
|
|
|
|
|
return true
|
|
|
|
|
end,
|
|
|
|
|
match = {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-- The data section actually mixes input data, reference to the
|
|
|
|
|
-- "n" parameter array, and self-reference to its own array, with
|
|
|
|
|
-- transformation functions used to modify itself. We parse it
|
|
|
|
|
-- as such into a table.
|
|
|
|
|
local data = {}
|
|
|
|
|
datac = datac..","
|
2022-08-31 09:31:14 +02:00
|
|
|
|
while datac and datac ~= "" do
|
2021-10-19 00:48:06 +02:00
|
|
|
|
local el = nil
|
|
|
|
|
-- Transformation functions
|
|
|
|
|
if string.match( datac, "^function%(" ) then
|
|
|
|
|
for name, tr in pairs( trans ) do
|
|
|
|
|
for i, match in ipairs( tr.match ) do
|
|
|
|
|
if string.match( datac, match ) then
|
|
|
|
|
el = tr.func
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if el then
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if not el then
|
|
|
|
|
el = trans.unid.func
|
|
|
|
|
vlc.msg.warn( "Couldn't parse unidentified YouTube video throttling parameter transformation" )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Compounding functions use a subfunction, so we need to be
|
|
|
|
|
-- more specific in how much parsed data we consume.
|
2021-11-15 00:58:35 +01:00
|
|
|
|
if el == trans.compound.func or
|
|
|
|
|
el == trans.compound1.func or
|
|
|
|
|
el == trans.compound2.func then
|
2021-10-19 00:48:06 +02:00
|
|
|
|
datac = string.match( datac, '^.-},e%.split%(""%)%)},(.*)$' )
|
2022-08-31 09:31:14 +02:00
|
|
|
|
or string.match( datac, "^.-},(.*)$" )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
else
|
|
|
|
|
datac = string.match( datac, "^.-},(.*)$" )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- String input data
|
|
|
|
|
elseif string.match( datac, '^"[^"]*",' ) then
|
|
|
|
|
el, datac = string.match( datac, '^"([^"]*)",(.*)$' )
|
|
|
|
|
-- Integer input data
|
2021-11-23 18:22:04 +01:00
|
|
|
|
-- 1818016376,-648890305,-1200559E3, ...
|
|
|
|
|
elseif string.match( datac, '^%-?%d+,' ) or
|
|
|
|
|
string.match( datac, '^%-?%d+[eE]%-?%d+,' ) then
|
2021-10-19 00:48:06 +02:00
|
|
|
|
el, datac = string.match( datac, "^(.-),(.*)$" )
|
|
|
|
|
el = tonumber( el )
|
|
|
|
|
-- Reference to "n" parameter array
|
|
|
|
|
elseif string.match( datac, '^b,' ) then
|
|
|
|
|
el = n
|
|
|
|
|
datac = string.match( datac, "^b,(.*)$" )
|
|
|
|
|
-- Replaced by self-reference to data array after its declaration
|
|
|
|
|
elseif string.match( datac, '^null,' ) then
|
|
|
|
|
el = data
|
|
|
|
|
datac = string.match( datac, "^null,(.*)$" )
|
|
|
|
|
else
|
|
|
|
|
vlc.msg.warn( "Couldn't parse unidentified YouTube video throttling parameter descrambling data" )
|
|
|
|
|
el = false -- Lua tables can't contain nil values
|
|
|
|
|
datac = string.match( datac, "^[^,]-,(.*)$" )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
table.insert( data, el )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Debugging helper to print data array elements
|
|
|
|
|
local prd = function( el, tab )
|
|
|
|
|
if not el then
|
|
|
|
|
return "???"
|
|
|
|
|
elseif el == n then
|
|
|
|
|
return "n"
|
|
|
|
|
elseif el == data then
|
|
|
|
|
return "data"
|
|
|
|
|
elseif type( el ) == "string" then
|
|
|
|
|
return '"'..el..'"'
|
|
|
|
|
elseif type( el ) == "number" then
|
|
|
|
|
el = tostring( el )
|
|
|
|
|
if type( tab ) == "table" then
|
|
|
|
|
el = el.." -> "..( el % table_len( tab ) )
|
|
|
|
|
end
|
|
|
|
|
return el
|
|
|
|
|
else
|
|
|
|
|
for name, tr in pairs( trans ) do
|
|
|
|
|
if el == tr.func then
|
|
|
|
|
return name
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return tostring( el )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- The script section contains a series of calls to elements of
|
|
|
|
|
-- the data section array onto other elements of it: calls to
|
|
|
|
|
-- transformations, with a reference to the data array itself or
|
|
|
|
|
-- the "n" parameter array as first argument, and often input data
|
|
|
|
|
-- as a second argument. We parse and emulate those calls to follow
|
|
|
|
|
-- the descrambling script.
|
2021-11-15 00:39:15 +01:00
|
|
|
|
-- c[40](c[14],c[2]),c[25](c[48]),c[14](c[1],c[24],c[42]()), [...]
|
2022-08-31 09:39:18 +02:00
|
|
|
|
if not string.match( script, "c%[(%d+)%]%(c%[(%d+)%]([^)]-)%)" ) then
|
|
|
|
|
vlc.msg.dbg( "Couldn't parse and execute YouTube video throttling parameter descrambling rules" )
|
|
|
|
|
return nil
|
|
|
|
|
end
|
2021-11-15 00:39:15 +01:00
|
|
|
|
for ifunc, itab, args in string.gmatch( script, "c%[(%d+)%]%(c%[(%d+)%]([^)]-)%)" ) do
|
|
|
|
|
local iarg1 = string.match( args, "^,c%[(%d+)%]" )
|
|
|
|
|
local iarg2 = string.match( args, "^,[^,]-,c%[(%d+)%]" )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
|
|
|
|
|
local func = data[tonumber( ifunc ) + 1]
|
|
|
|
|
local tab = data[tonumber( itab ) + 1]
|
2021-11-15 00:39:15 +01:00
|
|
|
|
local arg1 = iarg1 and data[tonumber( iarg1 ) + 1]
|
|
|
|
|
local arg2 = iarg2 and data[tonumber( iarg2 ) + 1]
|
2021-10-19 00:48:06 +02:00
|
|
|
|
|
|
|
|
|
-- Uncomment to debug transformation chain
|
2021-11-15 00:39:15 +01:00
|
|
|
|
--vlc.msg.err( '"n" parameter transformation: '..prd( func ).."("..prd( tab )..( arg1 ~= nil and ( ", "..prd( arg1, tab ) ) or "" )..( arg2 ~= nil and ( ", "..prd( arg2, tab ) ) or "" )..") "..ifunc.."("..itab..( iarg1 and ( ", "..iarg1 ) or "" )..( iarg2 and ( ", "..iarg2 ) or "" )..")" )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
--local nprev = table.concat( n )
|
|
|
|
|
|
|
|
|
|
if type( func ) ~= "function" or type( tab ) ~= "table"
|
2021-11-15 00:39:15 +01:00
|
|
|
|
or func( tab, arg1, arg2 ) then
|
2021-10-19 00:48:06 +02:00
|
|
|
|
vlc.msg.dbg( "Invalid data type encountered during YouTube video throttling parameter descrambling transformation chain, aborting" )
|
|
|
|
|
vlc.msg.dbg( "Couldn't descramble YouTube throttling URL parameter: data transfer will get throttled" )
|
|
|
|
|
vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Uncomment to debug transformation chain
|
|
|
|
|
--local nnew = table.concat( n )
|
|
|
|
|
--if nprev ~= nnew then
|
|
|
|
|
-- vlc.msg.dbg( '"n" parameter transformation: '..nprev.." -> "..nnew )
|
|
|
|
|
--end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return table.concat( n )
|
|
|
|
|
end
|
|
|
|
|
|
2013-09-07 19:52:04 +02:00
|
|
|
|
-- Descramble the URL signature using the javascript code that does that
|
|
|
|
|
-- in the web page
|
2021-10-19 00:20:41 +02:00
|
|
|
|
function sig_descramble( sig, js )
|
2023-02-09 10:51:05 +01:00
|
|
|
|
if not js.stream then
|
|
|
|
|
if not js.url then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
js.stream = vlc.stream( js.url )
|
|
|
|
|
if not js.stream then
|
|
|
|
|
-- Retry once for transient errors
|
|
|
|
|
js.stream = vlc.stream( js.url )
|
|
|
|
|
if not js.stream then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-09-07 19:52:04 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Look for the descrambler function's name
|
2021-08-31 19:25:34 +02:00
|
|
|
|
-- if(h.s){var l=h.sp,m=wja(decodeURIComponent(h.s));f.set(l,encodeURIComponent(m))}
|
2018-09-09 03:32:59 +02:00
|
|
|
|
-- k.s (from stream map field "s") holds the input scrambled signature
|
|
|
|
|
-- k.sp (from stream map field "sp") holds a parameter name (normally
|
2019-06-15 15:53:21 +02:00
|
|
|
|
-- "signature" or "sig") to set with the output, descrambled signature
|
2021-10-18 21:32:09 +02:00
|
|
|
|
local descrambler = js_extract( js, "[=%(,&|](...?)%(decodeURIComponent%(.%.s%)%)" )
|
2015-11-11 14:17:58 +01:00
|
|
|
|
if not descrambler then
|
2016-07-17 00:19:15 +02:00
|
|
|
|
vlc.msg.dbg( "Couldn't extract youtube video URL signature descrambling function name" )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
return nil
|
2013-09-07 19:52:04 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-11 22:51:08 +01:00
|
|
|
|
-- Fetch the code of the descrambler function
|
2016-06-24 03:50:24 +02:00
|
|
|
|
-- Go=function(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("")};
|
2016-10-24 08:03:17 +02:00
|
|
|
|
local rules = js_extract( js, "^"..descrambler.."=function%([^)]*%){(.-)};" )
|
2015-11-11 22:51:08 +01:00
|
|
|
|
if not rules then
|
2016-07-17 00:19:15 +02:00
|
|
|
|
vlc.msg.dbg( "Couldn't extract youtube video URL signature descrambling rules" )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
return nil
|
2015-11-11 22:51:08 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Get the name of the helper object providing transformation definitions
|
|
|
|
|
local helper = string.match( rules, ";(..)%...%(" )
|
|
|
|
|
if not helper then
|
2016-07-17 00:19:15 +02:00
|
|
|
|
vlc.msg.dbg( "Couldn't extract youtube video URL signature transformation helper name" )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
return nil
|
2015-11-11 22:51:08 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Fetch the helper object code
|
|
|
|
|
-- 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)}};
|
2016-10-24 08:03:17 +02:00
|
|
|
|
local transformations = js_extract( js, "[ ,]"..helper.."={(.-)};" )
|
2015-11-11 22:51:08 +01:00
|
|
|
|
if not transformations then
|
2016-07-17 00:19:15 +02:00
|
|
|
|
vlc.msg.dbg( "Couldn't extract youtube video URL signature transformation code" )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
return nil
|
2013-09-07 19:52:04 +02:00
|
|
|
|
end
|
|
|
|
|
|
2014-07-28 03:26:56 +02:00
|
|
|
|
-- Parse the helper object to map available transformations
|
|
|
|
|
local trans = {}
|
|
|
|
|
for meth,code in string.gmatch( transformations, "(..):function%([^)]*%){([^}]*)}" ) do
|
|
|
|
|
-- a=a.reverse()
|
|
|
|
|
if string.match( code, "%.reverse%(" ) then
|
|
|
|
|
trans[meth] = "reverse"
|
2013-09-07 19:52:04 +02:00
|
|
|
|
|
2014-07-28 03:26:56 +02:00
|
|
|
|
-- a.splice(0,b)
|
|
|
|
|
elseif string.match( code, "%.splice%(") then
|
|
|
|
|
trans[meth] = "slice"
|
2013-09-07 19:52:04 +02:00
|
|
|
|
|
2014-07-28 03:26:56 +02:00
|
|
|
|
-- var c=a[0];a[0]=a[b%a.length];a[b]=c
|
|
|
|
|
elseif string.match( code, "var c=" ) then
|
|
|
|
|
trans[meth] = "swap"
|
|
|
|
|
else
|
|
|
|
|
vlc.msg.warn("Couldn't parse unknown youtube video URL signature transformation")
|
2013-09-22 04:29:47 +02:00
|
|
|
|
end
|
2014-07-28 03:26:56 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Parse descrambling rules, map them to known transformations
|
|
|
|
|
-- and apply them on the signature
|
|
|
|
|
local missing = false
|
|
|
|
|
for meth,idx in string.gmatch( rules, "..%.(..)%([^,]+,(%d+)%)" ) do
|
|
|
|
|
idx = tonumber( idx )
|
|
|
|
|
|
|
|
|
|
if trans[meth] == "reverse" then
|
|
|
|
|
sig = string.reverse( sig )
|
|
|
|
|
|
|
|
|
|
elseif trans[meth] == "slice" then
|
|
|
|
|
sig = string.sub( sig, idx + 1 )
|
|
|
|
|
|
|
|
|
|
elseif trans[meth] == "swap" then
|
2013-09-07 19:52:04 +02:00
|
|
|
|
if idx > 1 then
|
|
|
|
|
sig = string.gsub( sig, "^(.)("..string.rep( ".", idx - 1 )..")(.)(.*)$", "%3%2%1%4" )
|
|
|
|
|
elseif idx == 1 then
|
|
|
|
|
sig = string.gsub( sig, "^(.)(.)", "%2%1" )
|
|
|
|
|
end
|
2014-07-28 03:26:56 +02:00
|
|
|
|
else
|
|
|
|
|
vlc.msg.dbg("Couldn't apply unknown youtube video URL signature transformation")
|
|
|
|
|
missing = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if missing then
|
|
|
|
|
vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
|
2013-09-07 19:52:04 +02:00
|
|
|
|
end
|
|
|
|
|
return sig
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-01 09:27:34 +01:00
|
|
|
|
-- Parse and assemble video stream URL
|
2021-10-19 00:20:41 +02:00
|
|
|
|
function stream_url( params, js )
|
2019-12-01 09:27:34 +01:00
|
|
|
|
local url = string.match( params, "url=([^&]+)" )
|
|
|
|
|
if not url then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
url = vlc.strings.decode_uri( url )
|
|
|
|
|
|
|
|
|
|
-- Descramble any scrambled signature and append it to URL
|
|
|
|
|
local s = string.match( params, "s=([^&]+)" )
|
|
|
|
|
if s then
|
|
|
|
|
s = vlc.strings.decode_uri( s )
|
|
|
|
|
vlc.msg.dbg( "Found "..string.len( s ).."-character scrambled signature for youtube video URL, attempting to descramble... " )
|
2021-10-19 00:20:41 +02:00
|
|
|
|
local ds = sig_descramble( s, js )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
if not ds then
|
|
|
|
|
vlc.msg.dbg( "Couldn't descramble YouTube video URL signature" )
|
2019-12-01 09:27:34 +01:00
|
|
|
|
vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
|
2021-10-18 23:09:38 +02:00
|
|
|
|
ds = s
|
2019-12-01 09:27:34 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local sp = string.match( params, "sp=([^&]+)" )
|
|
|
|
|
if not sp then
|
|
|
|
|
vlc.msg.warn( "Couldn't extract signature parameters for youtube video URL, guessing" )
|
|
|
|
|
sp = "signature"
|
|
|
|
|
end
|
2021-10-18 23:09:38 +02:00
|
|
|
|
url = url.."&"..sp.."="..vlc.strings.encode_uri_component( ds )
|
2019-12-01 09:27:34 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-18 19:28:58 +02:00
|
|
|
|
-- Parse and pick our video stream URL (classic parameters, out of use)
|
2013-09-07 19:52:04 +02:00
|
|
|
|
function pick_url( url_map, fmt, js_url )
|
2012-09-28 04:15:49 +02:00
|
|
|
|
for stream in string.gmatch( url_map, "[^,]+" ) do
|
|
|
|
|
local itag = string.match( stream, "itag=(%d+)" )
|
|
|
|
|
if not fmt or not itag or tonumber( itag ) == tonumber( fmt ) then
|
2023-02-09 09:45:09 +01:00
|
|
|
|
return nil -- stream_url( stream, js_url )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-12-01 09:27:34 +01:00
|
|
|
|
return nil
|
2012-09-17 05:54:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-02-09 11:33:22 +01:00
|
|
|
|
-- Pick suitable stream among available formats
|
|
|
|
|
function pick_stream( formats, fmt )
|
2023-02-09 12:10:14 +01:00
|
|
|
|
if not formats then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Remove subobject fields to ease parsing of stream object array
|
|
|
|
|
formats = string.gsub( formats, '"[^"]-":{[^{}]-},?', '' )
|
|
|
|
|
|
2023-02-11 08:05:48 +01:00
|
|
|
|
if tonumber( fmt ) then
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- Legacy match from URL parameter
|
2023-02-11 08:05:48 +01:00
|
|
|
|
fmt = tonumber( fmt )
|
2023-02-09 11:33:22 +01:00
|
|
|
|
for stream in string.gmatch( formats, '{(.-)}' ) do
|
2019-12-01 09:31:28 +01:00
|
|
|
|
local itag = tonumber( string.match( stream, '"itag":(%d+)' ) )
|
|
|
|
|
if fmt == itag then
|
2023-02-09 11:33:22 +01:00
|
|
|
|
return stream
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-02-09 11:33:22 +01:00
|
|
|
|
return nil
|
2019-12-01 09:31:28 +01:00
|
|
|
|
else
|
|
|
|
|
-- Compare the different available formats listed with our
|
|
|
|
|
-- quality targets
|
|
|
|
|
local prefres = vlc.var.inherit( nil, "preferred-resolution" )
|
2023-02-09 11:33:22 +01:00
|
|
|
|
local bestres, pick
|
|
|
|
|
for stream in string.gmatch( formats, '{(.-)}' ) do
|
2019-12-01 09:31:28 +01:00
|
|
|
|
local height = tonumber( string.match( stream, '"height":(%d+)' ) )
|
|
|
|
|
|
2023-02-11 08:05:48 +01:00
|
|
|
|
-- We have no preference mechanism for audio formats,
|
|
|
|
|
-- so just pick the first one
|
|
|
|
|
if fmt == "audio" and not height then
|
|
|
|
|
return stream
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- Better than nothing
|
2023-02-11 08:05:48 +01:00
|
|
|
|
if ( not pick and fmt ~= "video" ) or ( height and ( not bestres
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- Better quality within limits
|
|
|
|
|
or ( ( prefres < 0 or height <= prefres ) and height > bestres )
|
|
|
|
|
-- Lower quality more suited to limits
|
|
|
|
|
or ( prefres > -1 and bestres > prefres and height < bestres )
|
|
|
|
|
) ) then
|
|
|
|
|
bestres = height
|
|
|
|
|
pick = stream
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-02-09 11:33:22 +01:00
|
|
|
|
return pick
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
2023-02-09 11:33:22 +01:00
|
|
|
|
end
|
2019-12-01 09:31:28 +01:00
|
|
|
|
|
2023-02-09 11:33:22 +01:00
|
|
|
|
-- Parse and pick our video stream URL (new-style parameters)
|
2023-02-09 12:10:14 +01:00
|
|
|
|
function pick_stream_url( muxed, adaptive, js_url, fmt )
|
2023-02-09 10:51:05 +01:00
|
|
|
|
-- Shared JavaScript resources - lazy initialization
|
|
|
|
|
local js = { url = js_url, stream = nil, lines = {}, i = 0 }
|
|
|
|
|
if not js.url then
|
|
|
|
|
vlc.msg.warn( "Couldn't extract YouTube JavaScript player code URL, descrambling functions unavailable" )
|
2021-10-19 00:20:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-02-11 08:05:48 +01:00
|
|
|
|
local pick = nil
|
|
|
|
|
if tonumber( fmt ) then
|
|
|
|
|
-- Specific numeric itag, search in both lists
|
|
|
|
|
pick = pick_stream( muxed, fmt )
|
|
|
|
|
if not pick then
|
|
|
|
|
pick = pick_stream( adaptive, fmt )
|
|
|
|
|
end
|
|
|
|
|
elseif ( fmt == "audio" or fmt == "video" ) then
|
|
|
|
|
-- Specifically audio or video only, no fallback
|
2023-02-09 12:10:14 +01:00
|
|
|
|
pick = pick_stream( adaptive, fmt )
|
2023-02-11 08:05:48 +01:00
|
|
|
|
else
|
2023-02-11 09:04:39 +01:00
|
|
|
|
if fmt == "hd" then
|
|
|
|
|
-- Try and leverage full array of adaptive formats
|
|
|
|
|
local audio = pick_stream( adaptive, "audio" )
|
|
|
|
|
local video = pick_stream( adaptive, "video" )
|
|
|
|
|
if audio and video then
|
|
|
|
|
local audio_url = assemble_stream_url( audio, js )
|
|
|
|
|
local video_url = assemble_stream_url( video, js )
|
|
|
|
|
if audio_url and video_url then
|
|
|
|
|
return video_url, audio_url
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not pick then
|
|
|
|
|
-- Default or fallback: safe old multiplexed streams,
|
|
|
|
|
-- but reduced to a single, low-definition format
|
|
|
|
|
-- available in some cases
|
|
|
|
|
pick = pick_stream( muxed, fmt )
|
|
|
|
|
end
|
2023-02-09 12:10:14 +01:00
|
|
|
|
end
|
2023-02-11 08:05:48 +01:00
|
|
|
|
|
2023-02-09 12:10:14 +01:00
|
|
|
|
if not pick then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
2023-02-11 08:45:15 +01:00
|
|
|
|
return assemble_stream_url( pick, js )
|
|
|
|
|
end
|
2023-02-09 12:10:14 +01:00
|
|
|
|
|
2023-02-11 08:45:15 +01:00
|
|
|
|
-- Parse, descramble and assemble elements of video stream URL
|
|
|
|
|
function assemble_stream_url( pick, js )
|
|
|
|
|
-- 1/ URL signature
|
2020-05-30 11:19:20 +02:00
|
|
|
|
-- Either the "url" or the "signatureCipher" parameter is present,
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- depending on whether the URL signature is scrambled.
|
2021-10-19 00:48:06 +02:00
|
|
|
|
local url
|
2020-05-30 11:19:20 +02:00
|
|
|
|
local cipher = string.match( pick, '"signatureCipher":"(.-)"' )
|
2020-08-17 18:25:48 +02:00
|
|
|
|
or string.match( pick, '"[a-zA-Z]*[Cc]ipher":"(.-)"' )
|
2019-12-01 09:31:28 +01:00
|
|
|
|
if cipher then
|
|
|
|
|
-- Scrambled signature: some assembly required
|
2021-10-19 00:48:06 +02:00
|
|
|
|
url = stream_url( cipher, js )
|
|
|
|
|
end
|
|
|
|
|
if not url then
|
|
|
|
|
-- Unscrambled signature, already included in ready-to-use URL
|
|
|
|
|
url = string.match( pick, '"url":"(.-)"' )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not url then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
2023-02-11 08:45:15 +01:00
|
|
|
|
-- 2/ Data transfer throttling
|
2021-10-19 00:48:06 +02:00
|
|
|
|
-- The "n" parameter is scrambled too, and needs to be descrambled
|
|
|
|
|
-- and replaced in place, otherwise the data transfer gets throttled
|
|
|
|
|
-- down to between 40 and 80 kB/s, below real-time playability level.
|
|
|
|
|
local n = string.match( url, "[?&]n=([^&]+)" )
|
|
|
|
|
if n then
|
|
|
|
|
n = vlc.strings.decode_uri( n )
|
2023-02-09 09:31:21 +01:00
|
|
|
|
local dn = nil -- n_descramble( n, js )
|
2021-10-19 00:48:06 +02:00
|
|
|
|
if dn then
|
|
|
|
|
url = string.gsub( url, "([?&])n=[^&]+", "%1n="..vlc.strings.encode_uri_component( dn ), 1 )
|
|
|
|
|
else
|
2022-08-31 10:03:21 +02:00
|
|
|
|
vlc.msg.err( "Couldn't descramble YouTube throttling URL parameter: data transfer will get throttled" )
|
|
|
|
|
--vlc.msg.err( "Couldn't process youtube video URL, please check for updates to this script" )
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-10-19 00:48:06 +02:00
|
|
|
|
|
|
|
|
|
return url
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
|
|
|
|
|
2007-05-15 00:27:36 +02:00
|
|
|
|
-- Probe function.
|
|
|
|
|
function probe()
|
2021-04-03 12:02:34 +02:00
|
|
|
|
return ( ( vlc.access == "http" or vlc.access == "https" ) and (
|
|
|
|
|
((
|
2018-02-02 15:22:24 +01:00
|
|
|
|
string.match( vlc.path, "^www%.youtube%.com/" )
|
2020-11-12 05:48:10 +01:00
|
|
|
|
or string.match( vlc.path, "^music%.youtube%.com/" )
|
2020-11-12 05:42:59 +01:00
|
|
|
|
or string.match( vlc.path, "^gaming%.youtube%.com/" ) -- out of use
|
2018-02-02 15:22:24 +01:00
|
|
|
|
) and (
|
2016-10-24 07:54:30 +02:00
|
|
|
|
string.match( vlc.path, "/watch%?" ) -- the html page
|
2016-02-09 19:48:17 +01:00
|
|
|
|
or string.match( vlc.path, "/live$" ) -- user live stream html page
|
|
|
|
|
or string.match( vlc.path, "/live%?" ) -- user live stream html page
|
2023-02-09 09:20:55 +01:00
|
|
|
|
or string.match( vlc.path, "/shorts/" ) -- YouTube Shorts HTML page
|
2012-09-17 05:54:59 +02:00
|
|
|
|
or string.match( vlc.path, "/get_video_info%?" ) -- info API
|
2011-08-07 10:53:37 +02:00
|
|
|
|
or string.match( vlc.path, "/v/" ) -- video in swf player
|
2014-10-25 03:51:45 +02:00
|
|
|
|
or string.match( vlc.path, "/embed/" ) -- embedded player iframe
|
2021-04-03 12:02:34 +02:00
|
|
|
|
)) or
|
|
|
|
|
string.match( vlc.path, "^consent%.youtube%.com/" )
|
|
|
|
|
) )
|
2007-05-15 00:27:36 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Parse function.
|
|
|
|
|
function parse()
|
2021-04-03 12:02:34 +02:00
|
|
|
|
if string.match( vlc.path, "^consent%.youtube%.com/" ) then
|
|
|
|
|
-- Cookie consent redirection
|
|
|
|
|
-- Location: https://consent.youtube.com/m?continue=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXXXXXXXXXXX&gl=FR&m=0&pc=yt&uxe=23983172&hl=fr&src=1
|
|
|
|
|
-- Set-Cookie: CONSENT=PENDING+355; expires=Fri, 01-Jan-2038 00:00:00 GMT; path=/; domain=.youtube.com
|
|
|
|
|
local url = get_url_param( vlc.path, "continue" )
|
|
|
|
|
if not url then
|
|
|
|
|
vlc.msg.err( "Couldn't handle YouTube cookie consent redirection, please check for updates to this script or try disabling HTTP cookie forwarding" )
|
|
|
|
|
return { }
|
|
|
|
|
end
|
|
|
|
|
return { { path = vlc.strings.decode_uri( url ), options = { ":no-http-forward-cookies" } } }
|
|
|
|
|
elseif not string.match( vlc.path, "^www%.youtube%.com/" ) then
|
2020-11-12 05:42:59 +01:00
|
|
|
|
-- Skin subdomain
|
|
|
|
|
return { { path = vlc.access.."://"..string.gsub( vlc.path, "^([^/]*)/", "www.youtube.com/" ) } }
|
2021-04-03 12:02:34 +02:00
|
|
|
|
|
2020-11-12 05:42:59 +01:00
|
|
|
|
elseif string.match( vlc.path, "/watch%?" )
|
2016-02-09 19:48:17 +01:00
|
|
|
|
or string.match( vlc.path, "/live$" )
|
|
|
|
|
or string.match( vlc.path, "/live%?" )
|
2023-02-09 09:20:55 +01:00
|
|
|
|
or string.match( vlc.path, "/shorts/" )
|
2007-05-15 00:27:36 +02:00
|
|
|
|
then -- This is the HTML page's URL
|
2023-02-11 09:04:39 +01:00
|
|
|
|
local path, path2, title, description, artist, arturl, js_url
|
2023-02-09 10:23:37 +01:00
|
|
|
|
|
|
|
|
|
-- Retired YouTube API for video format itag parameter,
|
2023-02-11 08:05:48 +01:00
|
|
|
|
-- still supported and extended as youtube.lua API
|
2023-02-09 10:23:37 +01:00
|
|
|
|
-- https://en.wikipedia.org/w/index.php?title=YouTube&oldid=716878321#Quality_and_formats
|
2023-02-09 10:08:04 +01:00
|
|
|
|
local fmt = get_url_param( vlc.path, "fmt" )
|
2023-02-09 10:23:37 +01:00
|
|
|
|
|
2007-05-15 00:27:36 +02:00
|
|
|
|
while true do
|
2020-11-04 19:22:10 +01:00
|
|
|
|
-- The new HTML code layout has fewer and longer lines; always
|
|
|
|
|
-- use the long line workaround until we get more visibility.
|
|
|
|
|
local line = new_layout and read_long_line() or vlc.readline()
|
2007-05-15 00:27:36 +02:00
|
|
|
|
if not line then break end
|
2020-09-08 06:30:54 +02:00
|
|
|
|
|
|
|
|
|
-- The next line is the major configuration line that we need.
|
2020-11-04 19:17:23 +01:00
|
|
|
|
-- It is very long so we need this workaround (see #24957).
|
2020-11-03 00:46:46 +01:00
|
|
|
|
if string.match( line, '^ *<div id="player%-api">' ) then
|
2020-11-04 19:17:23 +01:00
|
|
|
|
line = read_long_line()
|
|
|
|
|
if not line then break end
|
2020-09-08 06:30:54 +02:00
|
|
|
|
end
|
2020-08-17 18:42:13 +02:00
|
|
|
|
|
2020-11-03 00:38:49 +01:00
|
|
|
|
if not title then
|
|
|
|
|
local meta = string.match( line, '<meta property="og:title"( .-)>' )
|
|
|
|
|
if meta then
|
|
|
|
|
title = string.match( meta, ' content="(.-)"' )
|
|
|
|
|
if title then
|
|
|
|
|
title = vlc.strings.resolve_xml_special_chars( title )
|
|
|
|
|
end
|
|
|
|
|
end
|
2007-05-15 00:27:36 +02:00
|
|
|
|
end
|
2015-02-18 19:17:47 +01:00
|
|
|
|
|
2016-10-25 07:27:48 +02:00
|
|
|
|
if not description then
|
2020-07-20 12:21:23 +02:00
|
|
|
|
-- FIXME: there is another version of this available,
|
|
|
|
|
-- without the double JSON string encoding, but we're
|
|
|
|
|
-- unlikely to access it due to #24957
|
|
|
|
|
description = string.match( line, '\\"shortDescription\\":\\"(.-[^\\])\\"')
|
2016-10-25 07:27:48 +02:00
|
|
|
|
if description then
|
2020-11-03 01:20:21 +01:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
|
|
|
|
description = string.gsub( description, '\\(["\\/])', '%1' )
|
|
|
|
|
else
|
|
|
|
|
description = string.match( line, '"shortDescription":"(.-[^\\])"')
|
|
|
|
|
end
|
|
|
|
|
if description then
|
|
|
|
|
if string.match( description, '^"' ) then
|
2020-09-07 20:07:02 +02:00
|
|
|
|
description = ""
|
|
|
|
|
end
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-09-07 20:05:27 +02:00
|
|
|
|
-- This way of unescaping is technically wrong
|
|
|
|
|
-- so as little as possible of it should be done
|
2020-07-20 12:21:23 +02:00
|
|
|
|
description = string.gsub( description, '\\(["\\/])', '%1' )
|
|
|
|
|
description = string.gsub( description, '\\n', '\n' )
|
2020-09-07 20:05:27 +02:00
|
|
|
|
description = string.gsub( description, '\\r', '\r' )
|
2020-07-20 12:21:23 +02:00
|
|
|
|
description = string.gsub( description, "\\u0026", "&" )
|
2016-10-25 07:27:48 +02:00
|
|
|
|
end
|
2007-05-17 22:34:58 +02:00
|
|
|
|
end
|
2015-02-18 19:17:47 +01:00
|
|
|
|
|
2020-11-03 00:38:49 +01:00
|
|
|
|
if not arturl then
|
|
|
|
|
local meta = string.match( line, '<meta property="og:image"( .-)>' )
|
|
|
|
|
if meta then
|
|
|
|
|
arturl = string.match( meta, ' content="(.-)"' )
|
|
|
|
|
if arturl then
|
|
|
|
|
arturl = vlc.strings.resolve_xml_special_chars( arturl )
|
|
|
|
|
end
|
|
|
|
|
end
|
2011-08-07 09:05:51 +02:00
|
|
|
|
end
|
2015-02-18 19:17:47 +01:00
|
|
|
|
|
2019-12-01 14:13:31 +01:00
|
|
|
|
if not artist then
|
|
|
|
|
artist = string.match(line, '\\"author\\":\\"(.-)\\"')
|
2020-07-20 11:57:05 +02:00
|
|
|
|
if artist then
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-11-03 00:14:45 +01:00
|
|
|
|
artist = string.gsub( artist, '\\(["\\/])', '%1' )
|
2020-11-03 01:20:21 +01:00
|
|
|
|
else
|
|
|
|
|
artist = string.match( line, '"author":"(.-)"' )
|
|
|
|
|
end
|
|
|
|
|
if artist then
|
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-07-20 11:57:05 +02:00
|
|
|
|
artist = string.gsub( artist, "\\u0026", "&" )
|
|
|
|
|
end
|
2007-05-17 22:34:58 +02:00
|
|
|
|
end
|
2015-02-18 19:17:47 +01:00
|
|
|
|
|
2020-11-04 19:22:10 +01:00
|
|
|
|
if not new_layout then
|
2020-11-03 01:20:21 +01:00
|
|
|
|
if string.match( line, '<script nonce="' ) then
|
|
|
|
|
vlc.msg.dbg( "Detected new YouTube HTML code layout" )
|
2020-11-04 19:22:10 +01:00
|
|
|
|
new_layout = true
|
2020-11-03 01:20:21 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-12 06:04:57 +01:00
|
|
|
|
-- We need this when parsing the main stream configuration;
|
|
|
|
|
-- it can indeed be found on that same line (among others).
|
|
|
|
|
if not js_url then
|
|
|
|
|
js_url = string.match( line, '"jsUrl":"(.-)"' )
|
2020-10-26 08:09:26 +01:00
|
|
|
|
or string.match( line, "\"js\": *\"(.-)\"" )
|
2013-09-07 19:52:04 +02:00
|
|
|
|
if js_url then
|
|
|
|
|
js_url = string.gsub( js_url, "\\/", "/" )
|
2017-02-01 07:35:46 +01:00
|
|
|
|
-- Resolve URL
|
|
|
|
|
if string.match( js_url, "^/[^/]" ) then
|
|
|
|
|
local authority = string.match( vlc.path, "^([^/]*)/" )
|
|
|
|
|
js_url = "//"..authority..js_url
|
|
|
|
|
end
|
2013-12-07 18:23:51 +01:00
|
|
|
|
js_url = string.gsub( js_url, "^//", vlc.access.."://" )
|
2013-09-07 19:52:04 +02:00
|
|
|
|
end
|
2020-11-12 06:04:57 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- JSON parameters, also formerly known as "swfConfig",
|
2022-11-18 06:17:50 +01:00
|
|
|
|
-- "SWF_ARGS", "swfArgs", "PLAYER_CONFIG", "playerConfig",
|
|
|
|
|
-- "ytplayer.config" ...
|
|
|
|
|
if string.match( line, "ytInitialPlayerResponse ?= ?{" )
|
|
|
|
|
or string.match( line, "ytplayer%.config" ) then
|
2013-09-07 19:52:04 +02:00
|
|
|
|
|
2020-07-20 11:46:44 +02:00
|
|
|
|
-- Classic parameters - out of use since early 2020
|
2011-09-15 20:37:06 +02:00
|
|
|
|
if not fmt then
|
2015-01-28 18:02:25 +01:00
|
|
|
|
fmt_list = string.match( line, "\"fmt_list\": *\"(.-)\"" )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
if fmt_list then
|
|
|
|
|
fmt_list = string.gsub( fmt_list, "\\/", "/" )
|
|
|
|
|
fmt = get_fmt( fmt_list )
|
2011-09-15 20:37:06 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-28 18:02:25 +01:00
|
|
|
|
url_map = string.match( line, "\"url_encoded_fmt_stream_map\": *\"(.-)\"" )
|
2011-08-07 08:47:10 +02:00
|
|
|
|
if url_map then
|
2019-12-01 09:31:28 +01:00
|
|
|
|
vlc.msg.dbg( "Found classic parameters for youtube video stream, parsing..." )
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2011-08-07 08:47:10 +02:00
|
|
|
|
url_map = string.gsub( url_map, "\\u0026", "&" )
|
2013-09-07 19:52:04 +02:00
|
|
|
|
path = pick_url( url_map, fmt, js_url )
|
2010-06-04 16:19:14 +02:00
|
|
|
|
end
|
2012-10-10 01:20:48 +02:00
|
|
|
|
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- New-style parameters
|
|
|
|
|
if not path then
|
|
|
|
|
local stream_map = string.match( line, '\\"formats\\":%[(.-)%]' )
|
|
|
|
|
if stream_map then
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-07-20 11:57:05 +02:00
|
|
|
|
stream_map = string.gsub( stream_map, '\\(["\\/])', '%1' )
|
2020-11-03 01:20:21 +01:00
|
|
|
|
else
|
|
|
|
|
stream_map = string.match( line, '"formats":%[(.-)%]' )
|
|
|
|
|
end
|
|
|
|
|
if stream_map then
|
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2019-12-01 09:31:28 +01:00
|
|
|
|
stream_map = string.gsub( stream_map, "\\u0026", "&" )
|
2023-02-09 12:10:14 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local adaptive_map = string.match( line, '"adaptiveFormats":%[(.-)%]' )
|
|
|
|
|
if adaptive_map then
|
|
|
|
|
-- FIXME: do this properly (see #24958)
|
|
|
|
|
adaptive_map = string.gsub( adaptive_map, "\\u0026", "&" )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if stream_map or adaptive_map then
|
|
|
|
|
vlc.msg.dbg( "Found new-style parameters for youtube video stream, parsing..." )
|
2023-02-11 09:04:39 +01:00
|
|
|
|
path, path2 = pick_stream_url( stream_map, adaptive_map, js_url, fmt )
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-10 01:20:48 +02:00
|
|
|
|
if not path then
|
|
|
|
|
-- If this is a live stream, the URL map will be empty
|
2015-02-18 19:17:47 +01:00
|
|
|
|
-- and we get the URL from this field instead
|
2019-01-19 03:07:38 +01:00
|
|
|
|
local hlsvp = string.match( line, '\\"hlsManifestUrl\\": *\\"(.-)\\"' )
|
2020-11-03 01:20:21 +01:00
|
|
|
|
or string.match( line, '"hlsManifestUrl":"(.-)"' )
|
2012-10-10 01:20:48 +02:00
|
|
|
|
if hlsvp then
|
|
|
|
|
hlsvp = string.gsub( hlsvp, "\\/", "/" )
|
|
|
|
|
path = hlsvp
|
|
|
|
|
end
|
|
|
|
|
end
|
2007-08-18 02:57:17 +02:00
|
|
|
|
end
|
2007-08-25 00:25:17 +02:00
|
|
|
|
end
|
2012-09-17 05:54:59 +02:00
|
|
|
|
|
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
|
|
|
|
|
|
2023-02-11 09:04:39 +01:00
|
|
|
|
local options = { }
|
|
|
|
|
if path2 then
|
|
|
|
|
table.insert( options, ":input-slave="..path2 )
|
|
|
|
|
end
|
|
|
|
|
return { { path = path; name = title; description = description; artist = artist; arturl = arturl; options = options } }
|
2012-09-17 05:54:59 +02:00
|
|
|
|
|
2021-10-18 19:55:56 +02:00
|
|
|
|
elseif string.match( vlc.path, "/get_video_info%?" ) then
|
|
|
|
|
-- video info API, retired since summer 2021
|
|
|
|
|
-- Replacement Innertube API requires HTTP POST requests
|
|
|
|
|
-- and so remains for now unworkable from lua parser scripts
|
|
|
|
|
-- (see #26185)
|
|
|
|
|
|
2020-09-08 06:30:54 +02:00
|
|
|
|
local line = vlc.read( 1024*1024 ) -- data is on one line only
|
2020-08-17 18:46:12 +02:00
|
|
|
|
if not line then
|
2020-09-08 06:30:54 +02:00
|
|
|
|
vlc.msg.err( "YouTube API output missing" )
|
2020-08-17 18:46:12 +02:00
|
|
|
|
return { }
|
|
|
|
|
end
|
2012-09-17 05:54:59 +02:00
|
|
|
|
|
2020-11-12 05:58:28 +01:00
|
|
|
|
local js_url = get_url_param( vlc.path, "jsurl" )
|
|
|
|
|
if js_url then
|
|
|
|
|
js_url= vlc.strings.decode_uri( js_url )
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-20 11:46:44 +02:00
|
|
|
|
-- Classic parameters - out of use since early 2020
|
2012-09-17 05:54:59 +02:00
|
|
|
|
local fmt = get_url_param( vlc.path, "fmt" )
|
|
|
|
|
if not fmt then
|
|
|
|
|
local fmt_list = string.match( line, "&fmt_list=([^&]*)" )
|
|
|
|
|
if fmt_list then
|
|
|
|
|
fmt_list = vlc.strings.decode_uri( fmt_list )
|
|
|
|
|
fmt = get_fmt( fmt_list )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local url_map = string.match( line, "&url_encoded_fmt_stream_map=([^&]*)" )
|
|
|
|
|
if url_map then
|
2019-12-01 09:31:28 +01:00
|
|
|
|
vlc.msg.dbg( "Found classic parameters for youtube video stream, parsing..." )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
url_map = vlc.strings.decode_uri( url_map )
|
2020-11-12 05:58:28 +01:00
|
|
|
|
path = pick_url( url_map, fmt, js_url )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-12-01 09:31:28 +01:00
|
|
|
|
-- New-style parameters
|
|
|
|
|
if not path then
|
|
|
|
|
local stream_map = string.match( line, '%%22formats%%22%%3A%%5B(.-)%%5D' )
|
|
|
|
|
if stream_map then
|
|
|
|
|
vlc.msg.dbg( "Found new-style parameters for youtube video stream, parsing..." )
|
|
|
|
|
stream_map = vlc.strings.decode_uri( stream_map )
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2019-12-01 09:31:28 +01:00
|
|
|
|
stream_map = string.gsub( stream_map, "\\u0026", "&" )
|
2023-02-09 12:10:14 +01:00
|
|
|
|
path = pick_stream_url( stream_map, nil, js_url, fmt )
|
2019-12-01 09:31:28 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-10 01:20:48 +02:00
|
|
|
|
if not path then
|
|
|
|
|
-- If this is a live stream, the URL map will be empty
|
2015-02-18 19:17:47 +01:00
|
|
|
|
-- and we get the URL from this field instead
|
2019-01-19 03:07:38 +01:00
|
|
|
|
local hlsvp = string.match( line, "%%22hlsManifestUrl%%22%%3A%%22(.-)%%22" )
|
2012-10-10 01:20:48 +02:00
|
|
|
|
if hlsvp then
|
|
|
|
|
hlsvp = vlc.strings.decode_uri( hlsvp )
|
|
|
|
|
path = hlsvp
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-12 06:33:25 +01:00
|
|
|
|
if not path and get_url_param( vlc.path, "el" ) ~= "detailpage" then
|
|
|
|
|
-- Retry with the other known value for the "el" parameter;
|
|
|
|
|
-- either value has historically been wrong and failed for
|
|
|
|
|
-- some videos but not others.
|
|
|
|
|
local video_id = get_url_param( vlc.path, "video_id" )
|
|
|
|
|
if video_id then
|
|
|
|
|
path = vlc.access.."://www.youtube.com/get_video_info?video_id="..video_id.."&el=detailpage"..copy_url_param( vlc.path, "fmt" )..copy_url_param( vlc.path, "jsurl" )
|
|
|
|
|
vlc.msg.warn( "Couldn't extract video URL, retrying with alternate YouTube API parameters" )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-09-17 05:54:59 +02:00
|
|
|
|
if not path then
|
|
|
|
|
vlc.msg.err( "Couldn't extract youtube video URL, please check for updates to this script" )
|
|
|
|
|
return { }
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-01 13:15:03 +01:00
|
|
|
|
local title = string.match( line, "%%22title%%22%%3A%%22(.-)%%22" )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
if title then
|
|
|
|
|
title = string.gsub( title, "+", " " )
|
|
|
|
|
title = vlc.strings.decode_uri( title )
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-07-20 11:57:05 +02:00
|
|
|
|
title = string.gsub( title, "\\u0026", "&" )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
end
|
2020-07-20 12:21:23 +02:00
|
|
|
|
-- FIXME: description gets truncated if it contains a double quote
|
|
|
|
|
local description = string.match( line, "%%22shortDescription%%22%%3A%%22(.-)%%22" )
|
|
|
|
|
if description then
|
|
|
|
|
description = string.gsub( description, "+", " " )
|
|
|
|
|
description = vlc.strings.decode_uri( description )
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-07-20 12:21:23 +02:00
|
|
|
|
description = string.gsub( description, '\\(["\\/])', '%1' )
|
|
|
|
|
description = string.gsub( description, '\\n', '\n' )
|
2020-11-12 03:38:58 +01:00
|
|
|
|
description = string.gsub( description, '\\r', '\r' )
|
2020-07-20 12:21:23 +02:00
|
|
|
|
description = string.gsub( description, "\\u0026", "&" )
|
|
|
|
|
end
|
2019-12-01 13:15:03 +01:00
|
|
|
|
local artist = string.match( line, "%%22author%%22%%3A%%22(.-)%%22" )
|
2013-07-23 00:48:11 +02:00
|
|
|
|
if artist then
|
|
|
|
|
artist = string.gsub( artist, "+", " " )
|
2013-08-05 03:12:14 +02:00
|
|
|
|
artist = vlc.strings.decode_uri( artist )
|
2020-08-17 18:36:21 +02:00
|
|
|
|
-- FIXME: do this properly (see #24958)
|
2020-07-20 11:57:05 +02:00
|
|
|
|
artist = string.gsub( artist, "\\u0026", "&" )
|
2013-07-23 00:48:11 +02:00
|
|
|
|
end
|
2019-12-01 13:15:03 +01:00
|
|
|
|
local arturl = string.match( line, "%%22playerMicroformatRenderer%%22%%3A%%7B%%22thumbnail%%22%%3A%%7B%%22thumbnails%%22%%3A%%5B%%7B%%22url%%22%%3A%%22(.-)%%22" )
|
2012-09-17 05:54:59 +02:00
|
|
|
|
if arturl then
|
|
|
|
|
arturl = vlc.strings.decode_uri( arturl )
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-19 08:18:13 +02:00
|
|
|
|
return { { path = path, name = title, description = description, artist = artist, arturl = arturl } }
|
2012-09-17 05:54:59 +02:00
|
|
|
|
|
2016-10-24 07:44:10 +02:00
|
|
|
|
else -- Other supported URL formats
|
2016-10-24 07:51:00 +02:00
|
|
|
|
local video_id = string.match( vlc.path, "/[^/]+/([^?]*)" )
|
2011-08-07 10:53:37 +02:00
|
|
|
|
if not video_id then
|
|
|
|
|
vlc.msg.err( "Couldn't extract youtube video URL" )
|
|
|
|
|
return { }
|
|
|
|
|
end
|
2016-10-24 08:01:08 +02:00
|
|
|
|
return { { path = vlc.access.."://www.youtube.com/watch?v="..video_id..copy_url_param( vlc.path, "fmt" ) } }
|
2007-05-15 00:27:36 +02:00
|
|
|
|
end
|
|
|
|
|
end
|