Videos: Use the new Stream classes across the code

This commit is contained in:
Samantaz Fox 2023-02-04 17:14:55 +01:00
parent 0d37faf284
commit 1adb035312
No known key found for this signature in database
GPG key ID: F42821059186176E
9 changed files with 123 additions and 177 deletions

View file

@ -4,10 +4,10 @@ module Invidious::Frontend::WatchPage
# A handy structure to pass many elements at # A handy structure to pass many elements at
# once to the download widget function # once to the download widget function
struct VideoAssets struct VideoAssets
getter full_videos : Array(Hash(String, JSON::Any)) getter full_videos : Array(Videos::ProgressiveHttpStream)
getter video_streams : Array(Hash(String, JSON::Any)) getter video_streams : Array(Videos::AdaptativeVideoStream)
getter audio_streams : Array(Hash(String, JSON::Any)) getter audio_streams : Array(Videos::AdaptativeAudioStream)
getter captions : Array(Invidious::Videos::Caption) getter captions : Array(Videos::Caption)
def initialize( def initialize(
@full_videos, @full_videos,
@ -48,38 +48,33 @@ module Invidious::Frontend::WatchPage
# Non-DASH videos (audio+video) # Non-DASH videos (audio+video)
video_assets.full_videos.each do |option| video_assets.full_videos.each do |option|
mimetype = option["mimeType"].as_s.split(";")[0] height = Invidious::Videos::Formats.itag_to_metadata?(option.itag).try &.["height"]?
height = Invidious::Videos::Formats.itag_to_metadata?(option["itag"]).try &.["height"]? value = {"itag": option.itag, "ext": option.mime_type.split("/")[1]}.to_json
value = {"itag": option["itag"], "ext": mimetype.split("/")[1]}.to_json
str << "\t\t\t<option value='" << value << "'>" str << "\t\t\t<option value='" << value << "'>"
str << (height || "~240") << "p - " << mimetype str << (height || option.video_height) << "p - " << option.mime_type
str << "</option>\n" str << "</option>\n"
end end
# DASH video streams # DASH video streams
video_assets.video_streams.each do |option| video_assets.video_streams.each do |option|
mimetype = option["mimeType"].as_s.split(";")[0] value = {"itag": option.itag, "ext": option.mime_type.split("/")[1]}.to_json
value = {"itag": option["itag"], "ext": mimetype.split("/")[1]}.to_json
str << "\t\t\t<option value='" << value << "'>" str << "\t\t\t<option value='" << value << "'>"
str << option["qualityLabel"] << " - " << mimetype << " @ " << option["fps"] << "fps - video only" str << option.label << " - " << option.mime_type
str << " @ " << option.video_fps << "fps - video only"
str << "</option>\n" str << "</option>\n"
end end
# DASH audio streams # DASH audio streams
video_assets.audio_streams.each do |option| video_assets.audio_streams.each do |option|
mimetype = option["mimeType"].as_s.split(";")[0] value = {"itag": option.itag, "ext": option.mime_type.split("/")[1]}.to_json
value = {"itag": option["itag"], "ext": mimetype.split("/")[1]}.to_json
str << "\t\t\t<option value='" << value << "'>" str << "\t\t\t<option value='" << value << "'>"
str << mimetype << " @ " << (option["bitrate"]?.try &.as_i./ 1000) << "k - audio only" str << option.mime_type << " @ " << (option.bitrate // 1000) << "kbps - audio only"
str << "</option>\n" str << "</option>\n"
end end

View file

@ -79,72 +79,67 @@ module Invidious::JSONify::APIv1
video.adaptive_fmts.each do |fmt| video.adaptive_fmts.each do |fmt|
json.object do json.object do
# Only available on regular videos, not livestreams/OTF streams # Only available on regular videos, not livestreams/OTF streams
if init_range = fmt["initRange"]? json.field "init", fmt.init_range.to_s if fmt.init_range
json.field "init", "#{init_range["start"]}-#{init_range["end"]}" json.field "index", fmt.index_range.to_s if fmt.index_range
end
if index_range = fmt["indexRange"]? # Livestream chunk infos. Should be present when `init` and `index` aren't
json.field "index", "#{index_range["start"]}-#{index_range["end"]}" json.field "targetDurationSec", fmt.target_duration if fmt.target_duration
end json.field "maxDvrDurationSec", fmt.max_dvr_duration if fmt.max_dvr_duration
# Not available on MPEG-4 Timed Text (`text/mp4`) streams (livestreams only) # Not available on MPEG-4 Timed Text (`text/mp4`) streams (livestreams only)
json.field "bitrate", fmt["bitrate"].as_i.to_s if fmt["bitrate"]? json.field "bitrate", fmt.bitrate.to_s if fmt.responds_to?(:bitrate)
if proxy if proxy
json.field "url", Invidious::HttpServer::Utils.proxy_video_url( json.field "url", Invidious::HttpServer::Utils.proxy_video_url(
fmt["url"].to_s, absolute: true fmt.url, absolute: true
) )
else else
json.field "url", fmt["url"] json.field "url", fmt.url
end end
json.field "itag", fmt["itag"].as_i.to_s json.field "itag", fmt.itag.to_s
json.field "type", fmt["mimeType"] json.field "type", fmt.raw_mime_type
json.field "clen", fmt["contentLength"]? || "-1" json.field "clen", fmt.content_length if fmt.responds_to?(:content_length)
json.field "encoding", fmt.codecs
# Last modified is a unix timestamp with µS, with the dot omitted. # Last modified is a unix timestamp with µS, with the dot omitted.
# E.g: 1638056732(.)141582 # E.g: 1638056732(.)141582
# #
# On livestreams, it's not present, so always fall back to the # On livestreams, it's not present, so always fall back to the
# current unix timestamp (up to mS precision) for compatibility. # current unix timestamp (up to mS precision) for compatibility.
last_modified = fmt["lastModified"]? last_modified = fmt.last_modified || Time.utc
last_modified ||= "#{Time.utc.to_unix_ms.to_s}000" json.field "lmt", "#{last_modified.to_unix_ms}000"
json.field "lmt", last_modified
json.field "projectionType", fmt["projectionType"] json.field "projectionType", fmt.projection_type.to_s.upcase
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"]) if fmt.is_a?(Videos::AdaptativeVideoStream)
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.as_i || 30 json.field "fps", fmt.video_fps
json.field "fps", fps json.field "size", "#{fmt.video_width}x#{fmt.video_height}"
end
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt.itag)
json.field "container", fmt_info["ext"] json.field "container", fmt_info["ext"]
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
if fmt_info["height"]? if fmt_info["height"]?
json.field "resolution", "#{fmt_info["height"]}p" json.field "resolution", "#{fmt_info["height"]}p"
quality_label = "#{fmt_info["height"]}p" quality_label = "#{fmt_info["height"]}p"
if fps > 30 quality_label += "60" if fmt.responds_to?(:video_fps) && fmt.video_fps > 30
quality_label += "60"
end
json.field "qualityLabel", quality_label json.field "qualityLabel", quality_label
if fmt_info["width"]?
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
end
end end
end end
# Livestream chunk infos
json.field "targetDurationSec", fmt["targetDurationSec"].as_i if fmt.has_key?("targetDurationSec")
json.field "maxDvrDurationSec", fmt["maxDvrDurationSec"].as_i if fmt.has_key?("maxDvrDurationSec")
# Audio-related data # Audio-related data
json.field "audioQuality", fmt["audioQuality"] if fmt.has_key?("audioQuality") if fmt.is_a?(Videos::AdaptativeAudioStream)
json.field "audioSampleRate", fmt["audioSampleRate"].as_s.to_i if fmt.has_key?("audioSampleRate") json.field "audioQuality", fmt.audio_quality
json.field "audioChannels", fmt["audioChannels"] if fmt.has_key?("audioChannels") json.field "audioSampleRate", fmt.audio_sample_rate
json.field "audioChannels", fmt.audio_channels
end
# Extra misc stuff # Extra misc stuff
json.field "colorInfo", fmt["colorInfo"] if fmt.has_key?("colorInfo") # json.field "colorInfo", fmt["colorInfo"] if fmt.has_key?("colorInfo")
json.field "captionTrack", fmt["captionTrack"] if fmt.has_key?("captionTrack") # json.field "captionTrack", fmt["captionTrack"] if fmt.has_key?("captionTrack")
end end
end end
end end
@ -154,30 +149,25 @@ module Invidious::JSONify::APIv1
json.array do json.array do
video.fmt_stream.each do |fmt| video.fmt_stream.each do |fmt|
json.object do json.object do
json.field "url", fmt["url"] json.field "url", fmt.url
json.field "itag", fmt["itag"].as_i.to_s json.field "itag", fmt.itag.to_s
json.field "type", fmt["mimeType"] json.field "type", fmt.raw_mime_type
json.field "quality", fmt["quality"] json.field "quality", fmt.label
fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt["itag"]) json.field "encoding", fmt.codecs
if fmt_info
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.as_i || 30 json.field "size", "#{fmt.video_width}x#{fmt.video_height}"
json.field "fps", fps json.field "fps", fmt.video_fps
if fmt_info = Invidious::Videos::Formats.itag_to_metadata?(fmt.itag)
json.field "container", fmt_info["ext"] json.field "container", fmt_info["ext"]
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
if fmt_info["height"]? if fmt_info["height"]?
json.field "resolution", "#{fmt_info["height"]}p" json.field "resolution", "#{fmt_info["height"]}p"
quality_label = "#{fmt_info["height"]}p" quality_label = "#{fmt_info["height"]}p"
if fps > 30 quality_label += "60" if fmt.video_fps > 30
quality_label += "60"
end
json.field "qualityLabel", quality_label json.field "qualityLabel", quality_label
if fmt_info["width"]?
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
end
end end
end end
end end

View file

@ -4,7 +4,7 @@ module Invidious::Routes::API::Manifest
env.response.headers.add("Access-Control-Allow-Origin", "*") env.response.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/dash+xml" env.response.content_type = "application/dash+xml"
local = env.params.query["local"]?.try &.== "true" local = (env.params.query["local"]? == "true")
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]? region = env.params.query["region"]?
@ -38,18 +38,18 @@ module Invidious::Routes::API::Manifest
return manifest return manifest
end end
adaptive_fmts = video.adaptive_fmts # Transform URLs for proxying
if local if local
adaptive_fmts.each do |fmt| video.adaptive_fmts.each do |fmt|
fmt["url"] = JSON::Any.new("#{HOST_URL}#{URI.parse(fmt["url"].as_s).request_target}") fmt.url = Invidious::HttpServer::Utils.proxy_video_url(fmt.url, absolute: true)
end end
end end
audio_streams = video.audio_streams.sort_by { |stream| {stream["bitrate"].as_i} }.reverse! audio_streams = video.audio_streams.sort_by(&.bitrate).reverse!
video_streams = video.video_streams.sort_by { |stream| {stream["width"].as_i, stream["fps"].as_i} }.reverse! video_streams = video.video_streams.sort_by { |fmt| {fmt.video_width, fmt.video_fps} }.reverse!
manifest = XML.build(indent: " ", encoding: "UTF-8") do |xml| # Build the manifest
return 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:full:2011", minBufferTime: "PT1.5S", type: "static", "profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static",
mediaPresentationDuration: "PT#{video.length_seconds}S") do mediaPresentationDuration: "PT#{video.length_seconds}S") do
@ -57,34 +57,28 @@ module Invidious::Routes::API::Manifest
i = 0 i = 0
{"audio/mp4"}.each do |mime_type| {"audio/mp4"}.each do |mime_type|
mime_streams = audio_streams.select { |stream| stream["mimeType"].as_s.starts_with? mime_type } formats = video.audio_streams.select(&.mime_type.== mime_type)
next if mime_streams.empty? next if formats.empty?
mime_streams.each do |fmt| formats.each do |fmt|
# OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415) # OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415)
next if !(fmt.has_key?("indexRange") && fmt.has_key?("initRange")) next if (fmt.index_range.nil? || fmt.init_range.nil?)
# Different representations of the same audio should be groupped into one AdaptationSet. # Different representations of the same audio should be groupped into one AdaptationSet.
# However, most players don't support auto quality switching, so we have to trick them # However, most players don't support auto quality switching, so we have to trick them
# into providing a quality selector. # into providing a quality selector.
# See https://github.com/iv-org/invidious/issues/3074 for more details. # See https://github.com/iv-org/invidious/issues/3074 for more details.
xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, label: fmt["bitrate"].to_s + "k") do xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, label: "#{(fmt.bitrate // 1000)} kbps") do
codecs = fmt["mimeType"].as_s.split("codecs=")[1].strip('"')
bandwidth = fmt["bitrate"].as_i
itag = fmt["itag"].as_i
url = fmt["url"].as_s
xml.element("Role", schemeIdUri: "urn:mpeg:dash:role:2011", value: i == 0 ? "main" : "alternate") xml.element("Role", schemeIdUri: "urn:mpeg:dash:role:2011", value: i == 0 ? "main" : "alternate")
xml.element("Representation", id: fmt.itag, codecs: fmt.codecs, bandwidth: fmt.bitrate) do
xml.element("Representation", id: fmt["itag"], codecs: codecs, bandwidth: bandwidth) do xml.element("AudioChannelConfiguration", schemeIdUri: "urn:mpeg:dash:23003:3:audio_channel_configuration:2011", value: fmt.audio_channels)
xml.element("AudioChannelConfiguration", schemeIdUri: "urn:mpeg:dash:23003:3:audio_channel_configuration:2011", xml.element("BaseURL") { xml.text fmt.url }
value: "2") xml.element("SegmentBase", indexRange: fmt.index_range.to_s) do
xml.element("BaseURL") { xml.text url } xml.element("Initialization", range: fmt.init_range.to_s)
xml.element("SegmentBase", indexRange: "#{fmt["indexRange"]["start"]}-#{fmt["indexRange"]["end"]}") do
xml.element("Initialization", range: "#{fmt["initRange"]["start"]}-#{fmt["initRange"]["end"]}")
end end
end end
end end
i += 1 i += 1
end end
end end
@ -92,33 +86,26 @@ module Invidious::Routes::API::Manifest
potential_heights = {4320, 2160, 1440, 1080, 720, 480, 360, 240, 144} potential_heights = {4320, 2160, 1440, 1080, 720, 480, 360, 240, 144}
{"video/mp4"}.each do |mime_type| {"video/mp4"}.each do |mime_type|
mime_streams = video_streams.select { |stream| stream["mimeType"].as_s.starts_with? mime_type } mime_streams = video.video_streams.select(&.mime_type.== mime_type)
next if mime_streams.empty? next if mime_streams.empty?
heights = [] of Int32 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
mime_streams.each do |fmt| mime_streams.each do |fmt|
# OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415) # OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415)
next if !(fmt.has_key?("indexRange") && fmt.has_key?("initRange")) next if (fmt.index_range.nil? || fmt.init_range.nil?)
codecs = fmt["mimeType"].as_s.split("codecs=")[1].strip('"')
bandwidth = fmt["bitrate"].as_i
itag = fmt["itag"].as_i
url = fmt["url"].as_s
width = fmt["width"].as_i
height = fmt["height"].as_i
# Resolutions reported by YouTube player (may not accurately reflect source) # Resolutions reported by YouTube player (may not accurately reflect source)
height = potential_heights.min_by { |x| (height - x).abs } height = potential_heights.min_by { |x| (fmt.video_height - x).abs }
next if unique_res && heights.includes? height next if unique_res && heights.includes? height
heights << height heights << height
xml.element("Representation", id: itag, codecs: codecs, width: width, height: height, xml.element("Representation", id: fmt.itag, codecs: fmt.codecs, width: fmt.video_width, height: height,
startWithSAP: "1", maxPlayoutRate: "1", startWithSAP: "1", maxPlayoutRate: "1", bandwidth: fmt.bitrate, frameRate: fmt.video_fps) do
bandwidth: bandwidth, frameRate: fmt["fps"]) do xml.element("BaseURL") { xml.text fmt.url }
xml.element("BaseURL") { xml.text url } xml.element("SegmentBase", indexRange: fmt.index_range.to_s) do
xml.element("SegmentBase", indexRange: "#{fmt["indexRange"]["start"]}-#{fmt["indexRange"]["end"]}") do xml.element("Initialization", range: fmt.init_range.to_s)
xml.element("Initialization", range: "#{fmt["initRange"]["start"]}-#{fmt["initRange"]["end"]}")
end end
end end
end end
@ -129,8 +116,6 @@ module Invidious::Routes::API::Manifest
end end
end end
end end
return manifest
end end
# /api/manifest/dash/id/videoplayback # /api/manifest/dash/id/videoplayback

View file

@ -157,8 +157,8 @@ module Invidious::Routes::Embed
adaptive_fmts = video.adaptive_fmts adaptive_fmts = video.adaptive_fmts
if params.local if params.local
fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(URI.parse(fmt["url"].as_s).request_target) } fmt_stream.each { |fmt| fmt.url = HttpServer::Utils.proxy_video_url(fmt.url) }
adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(URI.parse(fmt["url"].as_s).request_target) } adaptive_fmts.each { |fmt| fmt.url = HttpServer::Utils.proxy_video_url(fmt.url) }
end end
video_streams = video.video_streams video_streams = video.video_streams
@ -192,10 +192,10 @@ module Invidious::Routes::Embed
thumbnail = "/vi/#{video.id}/maxres.jpg" thumbnail = "/vi/#{video.id}/maxres.jpg"
if params.raw if params.raw
url = fmt_stream[0]["url"].as_s url = fmt_stream[0].url
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
url = fmt["url"].as_s if fmt["quality"].as_s == params.quality url = fmt.url if fmt.label == params.quality
end end
return env.redirect url return env.redirect url

View file

@ -277,13 +277,14 @@ module Invidious::Routes::VideoPlayback
return error_template(500, ex) return error_template(500, ex)
end end
fmt = video.fmt_stream.find(nil) { |f| f["itag"].as_i == itag } || video.adaptive_fmts.find(nil) { |f| f["itag"].as_i == itag } fmt = video.fmt_stream.find(nil, &.itag.== itag) || video.adaptive_fmts.find(nil, &.itag.== itag)
url = fmt.try &.["url"]?.try &.as_s
if !url if !fmt
haltf env, status_code: 404 haltf env, status_code: 404
end end
url = fmt.url
if local if local
url = URI.parse(url).request_target.not_nil! url = URI.parse(url).request_target.not_nil!
url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title

View file

@ -129,8 +129,8 @@ module Invidious::Routes::Watch
adaptive_fmts = video.adaptive_fmts adaptive_fmts = video.adaptive_fmts
if params.local if params.local
fmt_stream.each { |fmt| fmt["url"] = JSON::Any.new(URI.parse(fmt["url"].as_s).request_target) } fmt_stream.each { |fmt| fmt.url = HttpServer::Utils.proxy_video_url(fmt.url) }
adaptive_fmts.each { |fmt| fmt["url"] = JSON::Any.new(URI.parse(fmt["url"].as_s).request_target) } adaptive_fmts.each { |fmt| fmt.url = HttpServer::Utils.proxy_video_url(fmt.url) }
end end
video_streams = video.video_streams video_streams = video.video_streams
@ -168,21 +168,21 @@ module Invidious::Routes::Watch
if params.raw if params.raw
if params.listen if params.listen
url = audio_streams[0]["url"].as_s url = audio_streams[0].url
if params.quality.ends_with? "k" if params.quality.ends_with? "k"
audio_streams.each do |fmt| audio_streams.each do |fmt|
if fmt["bitrate"].as_i == params.quality.rchop("k").to_i if fmt.bitrate == params.quality.rchop("k").to_i
url = fmt["url"].as_s url = fmt.url
end end
end end
end end
else else
url = fmt_stream[0]["url"].as_s url = fmt_stream[0].url
fmt_stream.each do |fmt| fmt_stream.each do |fmt|
if fmt["quality"].as_s == params.quality if fmt.label == params.quality
url = fmt["url"].as_s url = fmt.url
end end
end end
end end

View file

@ -94,53 +94,28 @@ struct Video
# Methods for parsing streaming data # Methods for parsing streaming data
def fmt_stream def fmt_stream : Array(IV::Videos::ProgressiveHttpStream)
return @fmt_stream.as(Array(Hash(String, JSON::Any))) if @fmt_stream if formats = info.dig?("streamingData", "formats")
return IV::Videos.parse_progressive_formats(formats)
fmt_stream = info["streamingData"]?.try &.["formats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any) else
fmt_stream.each do |fmt| return [] of IV::Videos::ProgressiveHttpStream
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"]}&region=#{self.info["region"]}") if self.info["region"]?
end end
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
@fmt_stream = fmt_stream
return @fmt_stream.as(Array(Hash(String, JSON::Any)))
end end
def adaptive_fmts def adaptive_fmts : Array(IV::Videos::AdaptativeStream)
return @adaptive_fmts.as(Array(Hash(String, JSON::Any))) if @adaptive_fmts if formats = info.dig?("streamingData", "adaptiveFormats")
fmt_stream = info["streamingData"]?.try &.["adaptiveFormats"]?.try &.as_a.map &.as_h || [] of Hash(String, JSON::Any) return IV::Videos.parse_adaptative_formats(formats)
fmt_stream.each do |fmt| else
if s = (fmt["cipher"]? || fmt["signatureCipher"]?).try { |h| HTTP::Params.parse(h.as_s) } return [] of IV::Videos::AdaptativeStream
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"]}&region=#{self.info["region"]}") if self.info["region"]?
end 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 end
def video_streams def video_streams : Array(IV::Videos::AdaptativeVideoStream)
adaptive_fmts.select &.["mimeType"]?.try &.as_s.starts_with?("video") adaptive_fmts.select(IV::Videos::AdaptativeVideoStream)
end end
def audio_streams def audio_streams : Array(IV::Videos::AdaptativeAudioStream)
adaptive_fmts.select &.["mimeType"]?.try &.as_s.starts_with?("audio") adaptive_fmts.select(IV::Videos::AdaptativeAudioStream)
end end
# Misc. methods # Misc. methods

View file

@ -1,5 +1,5 @@
module Invidious::Videos::Formats module Invidious::Videos::Formats
def self.itag_to_metadata?(itag : JSON::Any) def self.itag_to_metadata?(itag : Int)
return FORMATS[itag.to_s]? return FORMATS[itag.to_s]?
end end

View file

@ -11,19 +11,19 @@
best_m4a_stream_index = 0 best_m4a_stream_index = 0
best_m4a_stream_bitrate = 0 best_m4a_stream_bitrate = 0
audio_streams.each_with_index do |fmt, i| audio_streams.each_with_index do |fmt, i|
bandwidth = fmt["bitrate"].as_i bandwidth = fmt.bitrate
if (fmt["mimeType"].as_s.starts_with?("audio/mp4") && bandwidth > best_m4a_stream_bitrate) if (fmt.mime_type == "audio/mp4" && bandwidth > best_m4a_stream_bitrate)
best_m4a_stream_bitrate = bandwidth best_m4a_stream_bitrate = bandwidth
best_m4a_stream_index = i best_m4a_stream_index = i
end end
end end
audio_streams.each_with_index do |fmt, i| audio_streams.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url = "/latest_version?id=#{video.id}&itag=#{fmt.itag}"
src_url += "&local=true" if params.local src_url += "&local=true" if params.local
bitrate = fmt["bitrate"] bitrate = fmt.bitrate // 1000
mimetype = HTML.escape(fmt["mimeType"].as_s) mimetype = HTML.escape(fmt.raw_mime_type)
selected = (i == best_m4a_stream_index) selected = (i == best_m4a_stream_index)
%> %>
@ -38,14 +38,14 @@
<% end %> <% end %>
<% <%
fmt_stream.reject! { |f| f["itag"] == 17 } fmt_stream.reject!(&.itag.== 17)
fmt_stream.sort_by! {|f| params.quality == f["quality"] ? 0 : 1 } fmt_stream.sort_by! { |f| params.quality == f.label ? 0 : 1 }
fmt_stream.each_with_index do |fmt, i| fmt_stream.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}" src_url = "/latest_version?id=#{video.id}&itag=#{fmt.itag}"
src_url += "&local=true" if params.local src_url += "&local=true" if params.local
quality = fmt["quality"] quality = fmt.label
mimetype = HTML.escape(fmt["mimeType"].as_s) mimetype = HTML.escape(fmt.raw_mime_type)
selected = params.quality ? (params.quality == quality) : (i == 0) selected = params.quality ? (params.quality == quality) : (i == 0)
%> %>