Skip to content

Commit 5800844

Browse files
authored
Merge pull request #3060 from SamantazFox/video-api-newpipe-compat
Video API: Improve NewPipe compatibility
2 parents 3bbd709 + b7f0b05 commit 5800844

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/invidious/routes/api/manifest.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ module Invidious::Routes::API::Manifest
6262

6363
xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true) do
6464
mime_streams.each do |fmt|
65+
# OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415)
66+
next if !(fmt.has_key?("indexRange") && fmt.has_key?("initRange"))
67+
6568
codecs = fmt["mimeType"].as_s.split("codecs=")[1].strip('"')
6669
bandwidth = fmt["bitrate"].as_i
6770
itag = fmt["itag"].as_i
@@ -90,6 +93,9 @@ module Invidious::Routes::API::Manifest
9093
heights = [] of Int32
9194
xml.element("AdaptationSet", id: i, mimeType: mime_type, startWithSAP: 1, subsegmentAlignment: true, scanType: "progressive") do
9295
mime_streams.each do |fmt|
96+
# OTF streams aren't supported yet (See https://github.com/TeamNewPipe/NewPipe/issues/2415)
97+
next if !(fmt.has_key?("indexRange") && fmt.has_key?("initRange"))
98+
9399
codecs = fmt["mimeType"].as_s.split("codecs=")[1].strip('"')
94100
bandwidth = fmt["bitrate"].as_i
95101
itag = fmt["itag"].as_i

src/invidious/videos.cr

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,25 @@ struct Video
374374
json.array do
375375
self.adaptive_fmts.each do |fmt|
376376
json.object do
377-
json.field "index", "#{fmt["indexRange"]["start"]}-#{fmt["indexRange"]["end"]}"
378-
json.field "bitrate", fmt["bitrate"].as_i.to_s
379-
json.field "init", "#{fmt["initRange"]["start"]}-#{fmt["initRange"]["end"]}"
377+
# Only available on regular videos, not livestreams/OTF streams
378+
if init_range = fmt["initRange"]?
379+
json.field "init", "#{init_range["start"]}-#{init_range["end"]}"
380+
end
381+
if index_range = fmt["indexRange"]?
382+
json.field "index", "#{index_range["start"]}-#{index_range["end"]}"
383+
end
384+
385+
# Not available on MPEG-4 Timed Text (`text/mp4`) streams (livestreams only)
386+
json.field "bitrate", fmt["bitrate"].as_i.to_s if fmt["bitrate"]?
387+
380388
json.field "url", fmt["url"]
381389
json.field "itag", fmt["itag"].as_i.to_s
382390
json.field "type", fmt["mimeType"]
383-
json.field "clen", fmt["contentLength"]
391+
json.field "clen", fmt["contentLength"]? || "-1"
384392
json.field "lmt", fmt["lastModified"]
385393
json.field "projectionType", fmt["projectionType"]
386394

387-
fmt_info = itag_to_metadata?(fmt["itag"])
388-
if fmt_info
395+
if fmt_info = itag_to_metadata?(fmt["itag"])
389396
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.as_i || 30
390397
json.field "fps", fps
391398
json.field "container", fmt_info["ext"]
@@ -405,6 +412,15 @@ struct Video
405412
end
406413
end
407414
end
415+
416+
# Audio-related data
417+
json.field "audioQuality", fmt["audioQuality"] if fmt.has_key?("audioQuality")
418+
json.field "audioSampleRate", fmt["audioSampleRate"].as_s.to_i if fmt.has_key?("audioSampleRate")
419+
json.field "audioChannels", fmt["audioChannels"] if fmt.has_key?("audioChannels")
420+
421+
# Extra misc stuff
422+
json.field "colorInfo", fmt["colorInfo"] if fmt.has_key?("colorInfo")
423+
json.field "captionTrack", fmt["captionTrack"] if fmt.has_key?("captionTrack")
408424
end
409425
end
410426
end
@@ -612,6 +628,7 @@ struct Video
612628
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
613629
fmt["url"] = JSON::Any.new("#{fmt["url"]}&region=#{self.info["region"]}") if self.info["region"]?
614630
end
631+
615632
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
616633
@fmt_stream = fmt_stream
617634
return @fmt_stream.as(Array(Hash(String, JSON::Any)))
@@ -631,9 +648,7 @@ struct Video
631648
fmt["url"] = JSON::Any.new("#{fmt["url"]}&host=#{URI.parse(fmt["url"].as_s).host}")
632649
fmt["url"] = JSON::Any.new("#{fmt["url"]}&region=#{self.info["region"]}") if self.info["region"]?
633650
end
634-
# See https://github.com/TeamNewPipe/NewPipe/issues/2415
635-
# Some streams are segmented by URL `sq/` rather than index, for now we just filter them out
636-
fmt_stream.reject! { |f| !f["indexRange"]? }
651+
637652
fmt_stream.sort_by! { |f| f["width"]?.try &.as_i || 0 }
638653
@adaptive_fmts = fmt_stream
639654
return @adaptive_fmts.as(Array(Hash(String, JSON::Any)))

0 commit comments

Comments
 (0)