Skip to content

Commit ff5d79e

Browse files
committed
Update video extractor
1 parent 4ee3ec0 commit ff5d79e

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/invidious.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3649,7 +3649,7 @@ get "/api/v1/top" do |env|
36493649
generate_thumbnails(json, video.id, config, Kemal.config)
36503650
end
36513651

3652-
json.field "lengthSeconds", video.info["length_seconds"].to_i
3652+
json.field "lengthSeconds", video.length_seconds
36533653
json.field "viewCount", video.views
36543654

36553655
json.field "author", video.author
@@ -4494,7 +4494,7 @@ get "/api/manifest/dash/id/:id" do |env|
44944494
XML.build(indent: " ", encoding: "UTF-8") do |xml|
44954495
xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011",
44964496
"profiles": "urn:mpeg:dash:profile:full:2011", minBufferTime: "PT1.5S", type: "static",
4497-
mediaPresentationDuration: "PT#{video.info["length_seconds"]}S") do
4497+
mediaPresentationDuration: "PT#{video.length_seconds}S") do
44984498
xml.element("Period") do
44994499
i = 0
45004500

src/invidious/videos.cr

+26-11
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ struct Video
329329

330330
json.field "subCountText", self.sub_count_text
331331

332-
json.field "lengthSeconds", self.info["length_seconds"].to_i
332+
json.field "lengthSeconds", self.length_seconds
333333
json.field "allowRatings", self.allow_ratings
334334
json.field "rating", self.info["avg_rating"].to_f32
335335
json.field "isListed", self.is_listed
@@ -563,7 +563,14 @@ struct Video
563563
fmt["clen"] = fmt_stream["contentLength"]?.try &.as_s || "0"
564564
fmt["bitrate"] = fmt_stream["bitrate"]?.try &.as_i.to_s || "0"
565565
fmt["itag"] = fmt_stream["itag"].as_i.to_s
566-
fmt["url"] = fmt_stream["url"].as_s
566+
if fmt_stream["url"]?
567+
fmt["url"] = fmt_stream["url"].as_s
568+
end
569+
if fmt_stream["cipher"]?
570+
HTTP::Params.parse(fmt_stream["cipher"].as_s).each do |key, value|
571+
fmt[key] = value
572+
end
573+
end
567574
fmt["quality"] = fmt_stream["quality"].as_s
568575

569576
if fmt_stream["width"]?
@@ -635,8 +642,14 @@ struct Video
635642
fmt["clen"] = adaptive_fmt["contentLength"]?.try &.as_s || "0"
636643
fmt["bitrate"] = adaptive_fmt["bitrate"]?.try &.as_i.to_s || "0"
637644
fmt["itag"] = adaptive_fmt["itag"].as_i.to_s
638-
fmt["url"] = adaptive_fmt["url"].as_s
639-
645+
if adaptive_fmt["url"]?
646+
fmt["url"] = adaptive_fmt["url"].as_s
647+
end
648+
if adaptive_fmt["cipher"]?
649+
HTTP::Params.parse(adaptive_fmt["cipher"].as_s).each do |key, value|
650+
fmt[key] = value
651+
end
652+
end
640653
if index = adaptive_fmt["indexRange"]?
641654
fmt["index"] = "#{index["start"]}-#{index["end"]}"
642655
end
@@ -827,7 +840,7 @@ struct Video
827840
end
828841

829842
def length_seconds
830-
return self.info["length_seconds"].to_i
843+
self.player_response["videoDetails"]["lengthSeconds"].as_s.to_i
831844
end
832845

833846
db_mapping({
@@ -1162,17 +1175,19 @@ def fetch_video(id, region)
11621175
end
11631176
end
11641177

1165-
if info["errorcode"]?.try &.== "2"
1178+
if info["errorcode"]?.try &.== "2" || !info["player_response"]
11661179
raise "Video unavailable."
11671180
end
11681181

1169-
if !info["title"]? || info["title"].empty?
1170-
raise "Video unavailable."
1182+
if info["reason"]?
1183+
raise info["reason"]
11711184
end
11721185

1173-
title = info["title"]
1174-
author = info["author"]? || ""
1175-
ucid = info["ucid"]? || ""
1186+
player_json = JSON.parse(info["player_response"])
1187+
1188+
title = player_json["videoDetails"]["title"].as_s
1189+
author = player_json["videoDetails"]["author"]?.try &.as_s || ""
1190+
ucid = player_json["videoDetails"]["ucid"]?.try &.as_s || ""
11761191

11771192
views = html.xpath_node(%q(//meta[@itemprop="interactionCount"]))
11781193
.try &.["content"].to_i64? || 0_i64

src/invidious/views/embed.ecr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
var video_data = {
3131
id: '<%= video.id %>',
3232
plid: '<%= plid %>',
33-
length_seconds: '<%= video.info["length_seconds"].to_f %>',
33+
length_seconds: '<%= video.length_seconds.to_f %>',
3434
video_series: <%= video_series.to_json %>,
3535
params: <%= params.to_json %>,
3636
preferences: <%= preferences.to_json %>,

src/invidious/views/watch.ecr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
var video_data = {
3131
id: '<%= video.id %>',
3232
plid: '<%= plid %>',
33-
length_seconds: <%= video.info["length_seconds"].to_f %>,
33+
length_seconds: <%= video.length_seconds.to_f %>,
3434
play_next: <%= !rvs.empty? && !plid && params.continue %>,
3535
next_video: '<%= rvs.select { |rv| rv["id"]? }[0]?.try &.["id"] %>',
3636
youtube_comments_text: '<%= HTML.escape(translate(locale, "View YouTube comments")) %>',

0 commit comments

Comments
 (0)