Integrate transcript captions into captions API

This commit is contained in:
syeopite 2023-07-23 05:02:02 -07:00
parent caac7e2166
commit e4942b188f
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
5 changed files with 91 additions and 54 deletions

View File

@ -182,6 +182,19 @@ https_only: false
#force_resolve:
##
## Use Innertube's transcripts API instead of timedtext for closed captions
##
## Useful for larger instances as InnerTube is **not ratelimited**. See https://github.com/iv-org/invidious/issues/2567
##
## Subtitle experience may differ slightly on Invidious.
##
## Accepted values: true, false
## Default: false
##
# use_innertube_for_captions: false
# -----------------------------
# Logging
# -----------------------------

View File

@ -129,6 +129,9 @@ class Config
# Use quic transport for youtube api
property use_quic : Bool = false
# Use Innertube's transcripts API instead of timedtext for closed captions
property use_innertube_for_captions : Bool = false
# Saved cookies in "name1=value1; name2=value2..." format
@[YAML::Field(converter: Preferences::StringToCookies)]
property cookies : HTTP::Cookies = HTTP::Cookies.new

View File

@ -87,6 +87,13 @@ module Invidious::Routes::API::V1::Videos
caption = caption[0]
end
if CONFIG.use_innertube_for_captions
params = Invidious::Videos::Transcript.generate_param(id, caption.language_code, caption.auto_generated)
initial_data = YoutubeAPI.transcript(params.to_s)
webvtt = Invidious::Videos::Transcript.convert_transcripts_to_vtt(initial_data, caption.language_code)
else
# Timedtext API handling
url = URI.parse("#{caption.base_url}&tlang=#{tlang}").request_target
# Auto-generated captions often have cues that aren't aligned properly with the video,
@ -153,6 +160,7 @@ module Invidious::Routes::API::V1::Videos
.gsub(/([0-9:.]{12} --> [0-9:.]{12}).+/, "\\1")
end
end
end
if title = env.params.query["title"]?
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/

View File

@ -6,7 +6,9 @@ module Invidious::Videos
property language_code : String
property base_url : String
def initialize(@name, @language_code, @base_url)
property auto_generated : Bool
def initialize(@name, @language_code, @base_url, @auto_generated)
end
# Parse the JSON structure from Youtube
@ -25,7 +27,12 @@ module Invidious::Videos
language_code = caption["languageCode"].to_s
base_url = caption["baseUrl"].to_s
captions_list << CaptionMetadata.new(name, language_code, base_url)
auto_generated = false
if caption["kind"]? && caption["kind"] == "asr"
auto_generated = true
end
captions_list << CaptionMetadata.new(name, language_code, base_url, auto_generated)
end
return captions_list

View File

@ -85,7 +85,13 @@ module Invidious::Videos
lines = [] of TranscriptLine
body.each do |line|
# Transcript section headers. They are not apart of the captions and as such we can safely skip them.
if line.as_h.has_key?("transcriptSectionHeaderRenderer")
next
end
line = line["transcriptSegmentRenderer"]
start_ms = line["startMs"].as_s.to_i.millisecond
end_ms = line["endMs"].as_s.to_i.millisecond