Add 'unique_res' option to '/api/manifest/dash/id'

This commit is contained in:
Omar Roth 2019-06-04 20:54:38 -05:00
parent 352e409a6e
commit d876fd7f5b
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2
2 changed files with 13 additions and 7 deletions

View File

@ -4692,15 +4692,18 @@ get "/api/manifest/dash/id/:id" do |env|
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]? region = env.params.query["region"]?
# Since some implementations create playlists based on resolution regardless of different codecs,
# we can opt to only add a source to a representation if it has a unique height
unique_res = env.params.query["unique_res"]? && (env.params.query["unique_res"] == "true" || env.params.query["unique_res"] == "1")
client = make_client(YT_URL) client = make_client(YT_URL)
begin begin
video = get_video(id, PG_DB, proxies, region: region) video = get_video(id, PG_DB, proxies, region: region)
rescue ex : VideoRedirect rescue ex : VideoRedirect
url = "/api/manifest/dash/id/#{ex.message}" url = "/api/manifest/dash/id/#{ex.message}"
if local if env.params.query
url += "?local=true" url += "?#{env.params.query}"
end end
next env.redirect url next env.redirect url
rescue ex rescue ex
env.response.status_code = 403 env.response.status_code = 403
@ -4737,7 +4740,7 @@ get "/api/manifest/dash/id/:id" do |env|
XML.build(indent: " ", encoding: "UTF-8") do |xml| XML.build(indent: " ", encoding: "UTF-8") do |xml|
xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011", xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011",
"profiles": "urn:mpeg:dash:profile:isoff-live:2011", minBufferTime: "PT1.5S", type: "static", "profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static",
mediaPresentationDuration: "PT#{video.info["length_seconds"]}S") do mediaPresentationDuration: "PT#{video.info["length_seconds"]}S") do
xml.element("Period") do xml.element("Period") do
i = 0 i = 0
@ -4746,7 +4749,7 @@ get "/api/manifest/dash/id/:id" do |env|
xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true) do xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true) do
audio_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt| audio_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt|
codecs = fmt["type"].split("codecs=")[1].strip('"') codecs = fmt["type"].split("codecs=")[1].strip('"')
bandwidth = fmt["bitrate"] bandwidth = fmt["bitrate"].to_i * 1000
itag = fmt["itag"] itag = fmt["itag"]
url = fmt["url"] url = fmt["url"]
@ -4765,6 +4768,7 @@ get "/api/manifest/dash/id/:id" do |env|
end end
{"video/mp4", "video/webm"}.each do |mime_type| {"video/mp4", "video/webm"}.each do |mime_type|
heights = [] of Int32
xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, scanType: "progressive") do xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, scanType: "progressive") do
video_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt| video_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt|
codecs = fmt["type"].split("codecs=")[1].strip('"') codecs = fmt["type"].split("codecs=")[1].strip('"')
@ -4775,6 +4779,8 @@ get "/api/manifest/dash/id/:id" do |env|
# Resolutions reported by YouTube player (may not accurately reflect source) # Resolutions reported by YouTube player (may not accurately reflect source)
height = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144].sort_by { |i| (height - i).abs }[0] height = [4320, 2160, 1440, 1080, 720, 480, 360, 240, 144].sort_by { |i| (height - i).abs }[0]
next if unique_res && heights.includes? height
heights << height
xml.element("Representation", id: itag, codecs: codecs, width: width, height: height, xml.element("Representation", id: itag, codecs: codecs, width: width, height: height,
startWithSAP: "1", maxPlayoutRate: "1", startWithSAP: "1", maxPlayoutRate: "1",

View File

@ -1235,11 +1235,11 @@ def process_video_params(query, preferences)
continue = query["continue"]?.try &.to_i? continue = query["continue"]?.try &.to_i?
continue_autoplay = query["continue_autoplay"]?.try &.to_i? continue_autoplay = query["continue_autoplay"]?.try &.to_i?
listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe
local = query["local"]? && (query["local"] == "true").to_unsafe local = query["local"]? && (query["local"] == "true" || query["listen"] == "1").to_unsafe
preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase } preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase }
quality = query["quality"]? quality = query["quality"]?
region = query["region"]? region = query["region"]?
related_videos = query["related_videos"]? related_videos = query["related_videos"]? && (query["related_videos"] == "true" || query["related_videos"] == "1").to_unsafe
speed = query["speed"]?.try &.to_f? speed = query["speed"]?.try &.to_f?
video_loop = query["loop"]?.try &.to_i? video_loop = query["loop"]?.try &.to_i?
volume = query["volume"]?.try &.to_i? volume = query["volume"]?.try &.to_i?