From 8b07a7b157ab37d385cc600f48c9a799403a851b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 12 Aug 2018 09:24:59 -0500 Subject: [PATCH] Add 'fps' to '/videos/:id' endpoint --- src/invidious.cr | 74 +++++++++++++++++++++++------------------ src/invidious/videos.cr | 12 +++++-- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index e66b97ae..949b6678 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1921,32 +1921,37 @@ get "/api/v1/videos/:id" do |env| json.field "adaptiveFormats" do json.array do - adaptive_fmts.each_with_index do |adaptive_fmt, i| + adaptive_fmts.each do |fmt| json.object do - json.field "index", adaptive_fmt["index"] - json.field "bitrate", adaptive_fmt["bitrate"] - json.field "init", adaptive_fmt["init"] - json.field "url", adaptive_fmt["url"] - json.field "itag", adaptive_fmt["itag"] - json.field "type", adaptive_fmt["type"] - json.field "clen", adaptive_fmt["clen"] - json.field "lmt", adaptive_fmt["lmt"] - json.field "projectionType", adaptive_fmt["projection_type"] + json.field "index", fmt["index"] + json.field "bitrate", fmt["bitrate"] + json.field "init", fmt["init"] + json.field "url", fmt["url"] + json.field "itag", fmt["itag"] + json.field "type", fmt["type"] + json.field "clen", fmt["clen"] + json.field "lmt", fmt["lmt"] + json.field "projectionType", fmt["projection_type"] - fmt_info = itag_to_metadata(adaptive_fmt["itag"]) - json.field "container", fmt_info["ext"] - json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"] + fmt_info = itag_to_metadata?(fmt["itag"]) + if fmt_info + fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.to_i || 30 + json.field "fps", fps + json.field "container", fmt_info["ext"] + json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"] - if fmt_info["fps"]? - json.field "fps", fmt_info["fps"] - end + if fmt_info["height"]? + json.field "resolution", "#{fmt_info["height"]}p" - if fmt_info["height"]? - json.field "qualityLabel", "#{fmt_info["height"]}p" - json.field "resolution", "#{fmt_info["height"]}p" + quality_label = "#{fmt_info["height"]}p" + if fps > 30 + quality_label += "60" + end + json.field "qualityLabel", quality_label - if fmt_info["width"]? - json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}" + if fmt_info["width"]? + json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}" + end end end end @@ -1963,20 +1968,25 @@ get "/api/v1/videos/:id" do |env| json.field "type", fmt["type"] json.field "quality", fmt["quality"] - fmt_info = itag_to_metadata(fmt["itag"]) - json.field "container", fmt_info["ext"] - json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"] + fmt_info = itag_to_metadata?(fmt["itag"]) + if fmt_info + fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.to_i || 30 + json.field "fps", fps + json.field "container", fmt_info["ext"] + json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"] - if fmt_info["fps"]? - json.field "fps", fmt_info["fps"] - end + if fmt_info["height"]? + json.field "resolution", "#{fmt_info["height"]}p" - if fmt_info["height"]? - json.field "qualityLabel", "#{fmt_info["height"]}p" - json.field "resolution", "#{fmt_info["height"]}p" + quality_label = "#{fmt_info["height"]}p" + if fps > 30 + quality_label += "60" + end + json.field "qualityLabel", quality_label - if fmt_info["width"]? - json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}" + if fmt_info["width"]? + json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}" + end end end end diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index e14f88f4..dbe307cd 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -328,7 +328,7 @@ def fetch_video(id) return video end -def itag_to_metadata(itag : String) +def itag_to_metadata?(itag : String) # See https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L380-#L476 formats = {"5" => {"ext" => "flv", "width" => 400, "height" => 240, "acodec" => "mp3", "abr" => 64, "vcodec" => "h263"}, "6" => {"ext" => "flv", "width" => 450, "height" => 270, "acodec" => "mp3", "abr" => 64, "vcodec" => "h263"}, @@ -414,6 +414,14 @@ def itag_to_metadata(itag : String) "308" => {"ext" => "webm", "height" => 1440, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, "313" => {"ext" => "webm", "height" => 2160, "format" => "DASH video", "vcodec" => "vp9"}, "315" => {"ext" => "webm", "height" => 2160, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "330" => {"ext" => "webm", "height" => 144, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "331" => {"ext" => "webm", "height" => 240, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "332" => {"ext" => "webm", "height" => 360, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "333" => {"ext" => "webm", "height" => 480, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "334" => {"ext" => "webm", "height" => 720, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "335" => {"ext" => "webm", "height" => 1080, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "336" => {"ext" => "webm", "height" => 1440, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, + "337" => {"ext" => "webm", "height" => 2160, "format" => "DASH video", "vcodec" => "vp9", "fps" => 60}, # Dash webm audio "171" => {"ext" => "webm", "acodec" => "vorbis", "format" => "DASH audio", "abr" => 128}, @@ -425,7 +433,7 @@ def itag_to_metadata(itag : String) "251" => {"ext" => "webm", "format" => "DASH audio", "acodec" => "opus", "abr" => 160}, } - return formats[itag] + return formats[itag]? end def process_video_params(query, preferences)