mirror of
https://gitea.invidious.io/iv-org/invidious.git
synced 2024-08-15 00:53:41 +00:00
Videos: Make use of the video decoding
This commit is contained in:
parent
56a7488161
commit
ec8b7916fa
3 changed files with 65 additions and 86 deletions
|
@ -163,7 +163,6 @@ if CONFIG.feed_threads > 0
|
|||
Invidious::Jobs.register Invidious::Jobs::RefreshFeedsJob.new(PG_DB)
|
||||
end
|
||||
|
||||
DECRYPT_FUNCTION = DecryptFunction.new(CONFIG.decrypt_polling)
|
||||
if CONFIG.statistics_enabled
|
||||
Invidious::Jobs.register Invidious::Jobs::StatisticsRefreshJob.new(PG_DB, SOFTWARE)
|
||||
end
|
||||
|
|
|
@ -1,73 +1,28 @@
|
|||
alias SigProc = Proc(Array(String), Int32, Array(String))
|
||||
require "http/params"
|
||||
require "./sig_helper"
|
||||
|
||||
struct DecryptFunction
|
||||
@decrypt_function = [] of {SigProc, Int32}
|
||||
@decrypt_time = Time.monotonic
|
||||
struct Invidious::DecryptFunction
|
||||
@last_update = Time.monotonic - 42.days
|
||||
|
||||
def initialize(@use_polling = true)
|
||||
def initialize
|
||||
self.check_update
|
||||
end
|
||||
|
||||
def update_decrypt_function
|
||||
@decrypt_function = fetch_decrypt_function
|
||||
def check_update
|
||||
now = Time.monotonic
|
||||
if (now - @last_update) > 60.seconds
|
||||
LOGGER.debug("Signature: Player might be outdated, updating")
|
||||
Invidious::SigHelper::Client.force_update
|
||||
@last_update = Time.monotonic
|
||||
end
|
||||
end
|
||||
|
||||
private def fetch_decrypt_function(id = "CvFH_6DNRCY")
|
||||
document = YT_POOL.client &.get("/watch?v=#{id}&gl=US&hl=en").body
|
||||
url = document.match(/src="(?<url>\/s\/player\/[^\/]+\/player_ias[^\/]+\/en_US\/base.js)"/).not_nil!["url"]
|
||||
player = YT_POOL.client &.get(url).body
|
||||
|
||||
function_name = player.match(/^(?<name>[^=]+)=function\(\w\){\w=\w\.split\(""\);[^\. ]+\.[^( ]+/m).not_nil!["name"]
|
||||
function_body = player.match(/^#{Regex.escape(function_name)}=function\(\w\){(?<body>[^}]+)}/m).not_nil!["body"]
|
||||
function_body = function_body.split(";")[1..-2]
|
||||
|
||||
var_name = function_body[0][0, 2]
|
||||
var_body = player.delete("\n").match(/var #{Regex.escape(var_name)}={(?<body>(.*?))};/).not_nil!["body"]
|
||||
|
||||
operations = {} of String => SigProc
|
||||
var_body.split("},").each do |operation|
|
||||
op_name = operation.match(/^[^:]+/).not_nil![0]
|
||||
op_body = operation.match(/\{[^}]+/).not_nil![0]
|
||||
|
||||
case op_body
|
||||
when "{a.reverse()"
|
||||
operations[op_name] = ->(a : Array(String), _b : Int32) { a.reverse }
|
||||
when "{a.splice(0,b)"
|
||||
operations[op_name] = ->(a : Array(String), b : Int32) { a.delete_at(0..(b - 1)); a }
|
||||
else
|
||||
operations[op_name] = ->(a : Array(String), b : Int32) { c = a[0]; a[0] = a[b % a.size]; a[b % a.size] = c; a }
|
||||
end
|
||||
end
|
||||
|
||||
decrypt_function = [] of {SigProc, Int32}
|
||||
function_body.each do |function|
|
||||
function = function.lchop(var_name).delete("[].")
|
||||
|
||||
op_name = function.match(/[^\(]+/).not_nil![0]
|
||||
value = function.match(/\(\w,(?<value>[\d]+)\)/).not_nil!["value"].to_i
|
||||
|
||||
decrypt_function << {operations[op_name], value}
|
||||
end
|
||||
|
||||
return decrypt_function
|
||||
end
|
||||
|
||||
def decrypt_signature(fmt : Hash(String, JSON::Any))
|
||||
return "" if !fmt["s"]? || !fmt["sp"]?
|
||||
|
||||
sp = fmt["sp"].as_s
|
||||
sig = fmt["s"].as_s.split("")
|
||||
if !@use_polling
|
||||
now = Time.monotonic
|
||||
if now - @decrypt_time > 60.seconds || @decrypt_function.size == 0
|
||||
@decrypt_function = fetch_decrypt_function
|
||||
@decrypt_time = Time.monotonic
|
||||
end
|
||||
end
|
||||
|
||||
@decrypt_function.each do |proc, value|
|
||||
sig = proc.call(sig, value)
|
||||
end
|
||||
|
||||
return "&#{sp}=#{sig.join("")}"
|
||||
def decrypt_signature(str : String) : String?
|
||||
self.check_update
|
||||
return SigHelper::Client.decrypt_sig(str)
|
||||
rescue ex
|
||||
LOGGER.debug(ex.message || "Signature: Unknown error")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
private DECRYPT_FUNCTION = IV::DecryptFunction.new
|
||||
|
||||
enum VideoType
|
||||
Video
|
||||
Livestream
|
||||
|
@ -98,20 +100,47 @@ struct Video
|
|||
|
||||
# Methods for parsing streaming data
|
||||
|
||||
def convert_url(fmt)
|
||||
if cfr = fmt["signatureCipher"]?.try { |h| HTTP::Params.parse(h.as_s) }
|
||||
sp = cfr["sp"]
|
||||
url = URI.parse(cfr["url"])
|
||||
params = url.query_params
|
||||
|
||||
LOGGER.debug("Videos: Decoding '#{cfr}'")
|
||||
|
||||
unsig = DECRYPT_FUNCTION.decrypt_signature(cfr["s"])
|
||||
params[sp] = unsig if unsig
|
||||
else
|
||||
url = URI.parse(fmt["url"].as_s)
|
||||
params = url.query_params
|
||||
end
|
||||
|
||||
n = DECRYPT_FUNCTION.decrypt_nsig(params["n"])
|
||||
params["n"] = n if n
|
||||
|
||||
params["host"] = url.host.not_nil!
|
||||
if region = self.info["region"]?.try &.as_s
|
||||
params["region"] = region
|
||||
end
|
||||
|
||||
url.query_params = params
|
||||
LOGGER.trace("Videos: new url is '#{url}'")
|
||||
|
||||
return url.to_s
|
||||
rescue ex
|
||||
LOGGER.debug("Videos: Error when parsing video URL")
|
||||
LOGGER.trace(ex.inspect_with_backtrace)
|
||||
return ""
|
||||
end
|
||||
|
||||
def fmt_stream
|
||||
return @fmt_stream.as(Array(Hash(String, JSON::Any))) if @fmt_stream
|
||||
|
||||
fmt_stream = info["streamingData"]?.try &.["formats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
fmt_stream.each do |fmt|
|
||||
if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
|
||||
s.each do |k, v|
|
||||
fmt[k] = JSON::Any.new(v)
|
||||
end
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
|
||||
end
|
||||
fmt_stream = info.dig?("streamingData", "formats")
|
||||
.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}®ion=#{self.info["region"]}") if self.info["region"]?
|
||||
fmt_stream.each do |fmt|
|
||||
fmt["url"] = JSON::Any.new(self.convert_url(fmt))
|
||||
end
|
||||
|
||||
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
|
||||
|
@ -121,21 +150,17 @@ struct Video
|
|||
|
||||
def adaptive_fmts
|
||||
return @adaptive_fmts.as(Array(Hash(String, JSON::Any))) if @adaptive_fmts
|
||||
fmt_stream = info["streamingData"]?.try &.["adaptiveFormats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
fmt_stream.each do |fmt|
|
||||
if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) }
|
||||
s.each do |k, v|
|
||||
fmt[k] = JSON::Any.new(v)
|
||||
end
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}#{DECRYPT_FUNCTION.decrypt_signature(fmt)}")
|
||||
end
|
||||
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
|
||||
fmt["url"] = JSON::Any.new("#{fmt["url"]}®ion=#{self.info["region"]}") if self.info["region"]?
|
||||
fmt_stream = info.dig("streamingData", "adaptiveFormats")
|
||||
.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any)
|
||||
|
||||
fmt_stream.each do |fmt|
|
||||
fmt["url"] = JSON::Any.new(self.convert_url(fmt))
|
||||
end
|
||||
|
||||
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
|
||||
@adaptive_fmts = fmt_stream
|
||||
|
||||
return @adaptive_fmts.as(Array(Hash(String, JSON::Any)))
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue