Invidious is an alternative front-end to YouTube
Fix caption parsing on age restricted videos
syeopite54b19a04bb11
diff --git a/src/invidious.cr b/src/invidious.crindex f7c8980a..57809c0b 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -1961,9 +1961,9 @@ get "/api/v1/captions/:id" do |env|json.array docaptions.each do |caption|json.object do- json.field "label", caption.name.simpleText+ json.field "label", caption.namejson.field "languageCode", caption.languageCode- json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name.simpleText)}"+ json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name)}"endendend@@ -1979,7 +1979,7 @@ get "/api/v1/captions/:id" do |env|if langcaption = captions.select { |caption| caption.languageCode == lang }else- caption = captions.select { |caption| caption.name.simpleText == label }+ caption = captions.select { |caption| caption.name == label }endif caption.empty?@@ -1993,7 +1993,7 @@ get "/api/v1/captions/:id" do |env|# Auto-generated captions often have cues that aren't aligned properly with the video,# as well as some other markup that makes it cumbersome, so we try to fix that here- if caption.name.simpleText.includes? "auto-generated"+ if caption.name.includes? "auto-generated"caption_xml = YT_POOL.client &.get(url).bodycaption_xml = XML.parse(caption_xml)diff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.crindex 5db32788..5e1e9431 100644--- a/src/invidious/routes/embed.cr+++ b/src/invidious/routes/embed.cr@@ -165,11 +165,11 @@ class Invidious::Routes::Embed < Invidious::Routes::BaseRoutecaptions = video.captionspreferred_captions = captions.select { |caption|- params.preferred_captions.includes?(caption.name.simpleText) ||+ params.preferred_captions.includes?(caption.name) ||params.preferred_captions.includes?(caption.languageCode.split("-")[0])}preferred_captions.sort_by! { |caption|- (params.preferred_captions.index(caption.name.simpleText) ||+ (params.preferred_captions.index(caption.name) ||params.preferred_captions.index(caption.languageCode.split("-")[0])).not_nil!}captions = captions - preferred_captionsdiff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.crindex d0338882..c6c7c154 100644--- a/src/invidious/routes/watch.cr+++ b/src/invidious/routes/watch.cr@@ -150,11 +150,11 @@ class Invidious::Routes::Watch < Invidious::Routes::BaseRoutecaptions = video.captionspreferred_captions = captions.select { |caption|- params.preferred_captions.includes?(caption.name.simpleText) ||+ params.preferred_captions.includes?(caption.name) ||params.preferred_captions.includes?(caption.languageCode.split("-")[0])}preferred_captions.sort_by! { |caption|- (params.preferred_captions.index(caption.name.simpleText) ||+ (params.preferred_captions.index(caption.name) ||params.preferred_captions.index(caption.languageCode.split("-")[0])).not_nil!}captions = captions - preferred_captionsdiff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex 3e64537e..1c9f5d03 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -425,9 +425,9 @@ struct Videojson.array doself.captions.each do |caption|json.object do- json.field "label", caption.name.simpleText+ json.field "label", caption.namejson.field "languageCode", caption.languageCode- json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name.simpleText)}"+ json.field "url", "/api/v1/captions/#{id}?label=#{URI.encode_www_form(caption.name)}"endendend@@ -706,8 +706,12 @@ struct Videodef captions : Array(Caption)return @captions.as(Array(Caption)) if @captionscaptions = info["captions"]?.try &.["playerCaptionsTracklistRenderer"]?.try &.["captionTracks"]?.try &.as_a.map do |caption|- caption = Caption.from_json(caption.to_json)- caption.name.simpleText = caption.name.simpleText.split(" - ")[0]+ name = caption["name"]["simpleText"]? || caption["name"]["runs"][0]["text"]+ languageCode = caption["languageCode"].to_s+ baseUrl = caption["baseUrl"].to_s++ caption = Caption.new(name.to_s, languageCode, baseUrl)+ caption.name = caption.name.split(" - ")[0]captionendcaptions ||= [] of Caption@@ -782,18 +786,19 @@ struct Videoendend-struct CaptionName- include JSON::Serializable+class Caption+ property name+ property languageCode+ property baseUrl- property simpleText : String-end+ getter name : String+ getter languageCode : String+ getter baseUrl : String-struct Caption- include JSON::Serializable+ setter name- property name : CaptionName- property baseUrl : String- property languageCode : String+ def initialize(@name, @languageCode, @baseUrl)+ endendclass VideoRedirect < Exceptiondiff --git a/src/invidious/views/components/player.ecr b/src/invidious/views/components/player.ecrindex cff3e60a..c37d20d5 100644--- a/src/invidious/views/components/player.ecr+++ b/src/invidious/views/components/player.ecr@@ -25,13 +25,13 @@<% end %><% preferred_captions.each do |caption| %>- <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name.simpleText %>&hl=<%= env.get("preferences").as(Preferences).locale %>"- label="<%= caption.name.simpleText %>">+ <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name %>&hl=<%= env.get("preferences").as(Preferences).locale %>"+ label="<%= caption.name %>"><% end %><% captions.each do |caption| %>- <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name.simpleText %>&hl=<%= env.get("preferences").as(Preferences).locale %>"- label="<%= caption.name.simpleText %>">+ <track kind="captions" src="/api/v1/captions/<%= video.id %>?label=<%= caption.name %>&hl=<%= env.get("preferences").as(Preferences).locale %>"+ label="<%= caption.name %>"><% end %><% end %></video>diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecrindex 91e03725..f21fdb04 100644--- a/src/invidious/views/watch.ecr+++ b/src/invidious/views/watch.ecr@@ -178,8 +178,8 @@ we're going to need to do it here in order to allow for translations.</option><% end %><% captions.each do |caption| %>- <option value='{"id":"<%= video.id %>","label":"<%= caption.name.simpleText %>","title":"<%= URI.encode_www_form(video.title) %>-<%= video.id %>.<%= caption.languageCode %>.vtt"}'>- <%= translate(locale, "Subtitles - `x` (.vtt)", caption.name.simpleText) %>+ <option value='{"id":"<%= video.id %>","label":"<%= caption.name %>","title":"<%= URI.encode_www_form(video.title) %>-<%= video.id %>.<%= caption.languageCode %>.vtt"}'>+ <%= translate(locale, "Subtitles - `x` (.vtt)", caption.name) %></option><% end %></select>