From d876fd7f5b193ce444e8575da7dc70ce5386b364 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Tue, 4 Jun 2019 20:54:38 -0500 Subject: [PATCH] Add 'unique_res' option to '/api/manifest/dash/id' --- src/invidious.cr | 16 +++++++++++----- src/invidious/videos.cr | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index e8621b23..f94ce576 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -4692,15 +4692,18 @@ get "/api/manifest/dash/id/:id" do |env| id = env.params.url["id"] 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) begin video = get_video(id, PG_DB, proxies, region: region) rescue ex : VideoRedirect url = "/api/manifest/dash/id/#{ex.message}" - if local - url += "?local=true" + if env.params.query + url += "?#{env.params.query}" end - next env.redirect url rescue ex 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.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 xml.element("Period") do 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 audio_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt| codecs = fmt["type"].split("codecs=")[1].strip('"') - bandwidth = fmt["bitrate"] + bandwidth = fmt["bitrate"].to_i * 1000 itag = fmt["itag"] url = fmt["url"] @@ -4765,6 +4768,7 @@ get "/api/manifest/dash/id/:id" do |env| end {"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 video_streams.select { |stream| stream["type"].starts_with? mime_type }.each do |fmt| 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) 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, startWithSAP: "1", maxPlayoutRate: "1", diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index a5841491..99c6df5c 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -1235,11 +1235,11 @@ def process_video_params(query, preferences) continue = query["continue"]?.try &.to_i? continue_autoplay = query["continue_autoplay"]?.try &.to_i? 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 } quality = query["quality"]? 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? video_loop = query["loop"]?.try &.to_i? volume = query["volume"]?.try &.to_i?