|
|
@ -425,9 +425,9 @@ struct Video
|
|
|
|
json.array do
|
|
|
|
json.array do
|
|
|
|
self.captions.each do |caption|
|
|
|
|
self.captions.each do |caption|
|
|
|
|
json.object do
|
|
|
|
json.object do
|
|
|
|
json.field "label", caption.name.simpleText
|
|
|
|
json.field "label", caption.name
|
|
|
|
json.field "languageCode", caption.languageCode
|
|
|
|
json.field "languageCode", caption.languageCode
|
|
|
|
json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name.simpleText)}"
|
|
|
|
json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -706,8 +706,12 @@ struct Video
|
|
|
|
def captions : Array(Caption)
|
|
|
|
def captions : Array(Caption)
|
|
|
|
return @captions.as(Array(Caption)) if @captions
|
|
|
|
return @captions.as(Array(Caption)) if @captions
|
|
|
|
captions = info["captions"]?.try &.["playerCaptionsTracklistRenderer"]?.try &.["captionTracks"]?.try &.as_a.map do |caption|
|
|
|
|
captions = info["captions"]?.try &.["playerCaptionsTracklistRenderer"]?.try &.["captionTracks"]?.try &.as_a.map do |caption|
|
|
|
|
caption = Caption.from_json(caption.to_json)
|
|
|
|
name = caption["name"]["simpleText"]? || caption["name"]["runs"][0]["text"]
|
|
|
|
caption.name.simpleText = caption.name.simpleText.split(" - ")[0]
|
|
|
|
languageCode = caption["languageCode"].to_s
|
|
|
|
|
|
|
|
baseUrl = caption["baseUrl"].to_s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
caption = Caption.new(name.to_s, languageCode, baseUrl)
|
|
|
|
|
|
|
|
caption.name = caption.name.split(" - ")[0]
|
|
|
|
caption
|
|
|
|
caption
|
|
|
|
end
|
|
|
|
end
|
|
|
|
captions ||= [] of Caption
|
|
|
|
captions ||= [] of Caption
|
|
|
@ -782,18 +786,19 @@ struct Video
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
struct CaptionName
|
|
|
|
struct Caption
|
|
|
|
include JSON::Serializable
|
|
|
|
property name
|
|
|
|
|
|
|
|
property languageCode
|
|
|
|
|
|
|
|
property baseUrl
|
|
|
|
|
|
|
|
|
|
|
|
property simpleText : String
|
|
|
|
getter name : String
|
|
|
|
end
|
|
|
|
getter languageCode : String
|
|
|
|
|
|
|
|
getter baseUrl : String
|
|
|
|
|
|
|
|
|
|
|
|
struct Caption
|
|
|
|
setter name
|
|
|
|
include JSON::Serializable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
property name : CaptionName
|
|
|
|
def initialize(@name, @languageCode, @baseUrl)
|
|
|
|
property baseUrl : String
|
|
|
|
end
|
|
|
|
property languageCode : String
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class VideoRedirect < Exception
|
|
|
|
class VideoRedirect < Exception
|
|
|
@ -989,9 +994,33 @@ def fetch_video(id, region)
|
|
|
|
|
|
|
|
|
|
|
|
# Try to pull streams from embed URL
|
|
|
|
# Try to pull streams from embed URL
|
|
|
|
if info["reason"]?
|
|
|
|
if info["reason"]?
|
|
|
|
|
|
|
|
required_parameters = {
|
|
|
|
|
|
|
|
"video_id" => id,
|
|
|
|
|
|
|
|
"eurl" => "https://youtube.googleapis.com/v/#{id}",
|
|
|
|
|
|
|
|
"html5" => "1",
|
|
|
|
|
|
|
|
"gl" => "US",
|
|
|
|
|
|
|
|
"hl" => "en",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if info["reason"].as_s.includes?("inappropriate")
|
|
|
|
|
|
|
|
# The html5, c and cver parameters are required in order to extract age-restricted videos
|
|
|
|
|
|
|
|
# See https://github.com/yt-dlp/yt-dlp/commit/4e6767b5f2e2523ebd3dd1240584ead53e8c8905
|
|
|
|
|
|
|
|
required_parameters.merge!({
|
|
|
|
|
|
|
|
"c" => "TVHTML5",
|
|
|
|
|
|
|
|
"cver" => "6.20180913",
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# In order to actually extract video info without error, the `x-youtube-client-version`
|
|
|
|
|
|
|
|
# has to be set to the same version as `cver` above.
|
|
|
|
|
|
|
|
additional_headers = HTTP::Headers{"x-youtube-client-version" => "6.20180913"}
|
|
|
|
|
|
|
|
else
|
|
|
|
embed_page = YT_POOL.client &.get("/embed/#{id}").body
|
|
|
|
embed_page = YT_POOL.client &.get("/embed/#{id}").body
|
|
|
|
sts = embed_page.match(/"sts"\s*:\s*(?<sts>\d+)/).try &.["sts"]? || ""
|
|
|
|
sts = embed_page.match(/"sts"\s*:\s*(?<sts>\d+)/).try &.["sts"]? || ""
|
|
|
|
embed_info = HTTP::Params.parse(YT_POOL.client &.get("/get_video_info?html5=1&video_id=#{id}&eurl=https://youtube.googleapis.com/v/#{id}&gl=US&hl=en&sts=#{sts}").body)
|
|
|
|
required_parameters["sts"] = sts
|
|
|
|
|
|
|
|
additional_headers = HTTP::Headers{} of String => String
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
embed_info = HTTP::Params.parse(YT_POOL.client &.get("/get_video_info?#{URI::Params.encode(required_parameters)}",
|
|
|
|
|
|
|
|
headers: additional_headers).body)
|
|
|
|
|
|
|
|
|
|
|
|
if embed_info["player_response"]?
|
|
|
|
if embed_info["player_response"]?
|
|
|
|
player_response = JSON.parse(embed_info["player_response"])
|
|
|
|
player_response = JSON.parse(embed_info["player_response"])
|
|
|
|