Refactor Invidious's VTT logic to use WebVtt.build

This commit is contained in:
syeopite 2023-08-24 15:10:50 -07:00
parent 54fa59cbb0
commit 0cb7d0b441
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
3 changed files with 29 additions and 91 deletions

View File

@ -101,20 +101,17 @@ module Invidious::Routes::API::V1::Videos
if caption.name.includes? "auto-generated" if caption.name.includes? "auto-generated"
caption_xml = YT_POOL.client &.get(url).body caption_xml = YT_POOL.client &.get(url).body
settings_field = {
"Kind" => "captions",
"Language" => "#{tlang || caption.language_code}",
}
if caption_xml.starts_with?("<?xml") if caption_xml.starts_with?("<?xml")
webvtt = caption.timedtext_to_vtt(caption_xml, tlang) webvtt = caption.timedtext_to_vtt(caption_xml, tlang)
else else
caption_xml = XML.parse(caption_xml) caption_xml = XML.parse(caption_xml)
webvtt = String.build do |str| webvtt = WebVTT.build(settings_field) do |webvtt|
str << <<-END_VTT
WEBVTT
Kind: captions
Language: #{tlang || caption.language_code}
END_VTT
caption_nodes = caption_xml.xpath_nodes("//transcript/text") caption_nodes = caption_xml.xpath_nodes("//transcript/text")
caption_nodes.each_with_index do |node, i| caption_nodes.each_with_index do |node, i|
start_time = node["start"].to_f.seconds start_time = node["start"].to_f.seconds
@ -127,9 +124,6 @@ module Invidious::Routes::API::V1::Videos
end_time = start_time + duration end_time = start_time + duration
end end
start_time = "#{start_time.hours.to_s.rjust(2, '0')}:#{start_time.minutes.to_s.rjust(2, '0')}:#{start_time.seconds.to_s.rjust(2, '0')}.#{start_time.milliseconds.to_s.rjust(3, '0')}"
end_time = "#{end_time.hours.to_s.rjust(2, '0')}:#{end_time.minutes.to_s.rjust(2, '0')}:#{end_time.seconds.to_s.rjust(2, '0')}.#{end_time.milliseconds.to_s.rjust(3, '0')}"
text = HTML.unescape(node.content) text = HTML.unescape(node.content)
text = text.gsub(/<font color="#[a-fA-F0-9]{6}">/, "") text = text.gsub(/<font color="#[a-fA-F0-9]{6}">/, "")
text = text.gsub(/<\/font>/, "") text = text.gsub(/<\/font>/, "")
@ -137,12 +131,7 @@ module Invidious::Routes::API::V1::Videos
text = "<v #{md["name"]}>#{md["text"]}</v>" text = "<v #{md["name"]}>#{md["text"]}</v>"
end end
str << <<-END_CUE webvtt.line(start_time, end_time, text)
#{start_time} --> #{end_time}
#{text}
END_CUE
end end
end end
end end
@ -215,11 +204,7 @@ module Invidious::Routes::API::V1::Videos
storyboard = storyboard[0] storyboard = storyboard[0]
end end
String.build do |str| WebVTT.build do |vtt|
str << <<-END_VTT
WEBVTT
END_VTT
start_time = 0.milliseconds start_time = 0.milliseconds
end_time = storyboard[:interval].milliseconds end_time = storyboard[:interval].milliseconds
@ -231,12 +216,8 @@ module Invidious::Routes::API::V1::Videos
storyboard[:storyboard_height].times do |j| storyboard[:storyboard_height].times do |j|
storyboard[:storyboard_width].times do |k| storyboard[:storyboard_width].times do |k|
str << <<-END_CUE current_cue_url = "#{url}#xywh=#{storyboard[:width] * k},#{storyboard[:height] * j},#{storyboard[:width] - 2},#{storyboard[:height]}"
#{start_time}.000 --> #{end_time}.000 vtt.line(start_time, end_time, current_cue_url)
#{url}#xywh=#{storyboard[:width] * k},#{storyboard[:height] * j},#{storyboard[:width] - 2},#{storyboard[:height]}
END_CUE
start_time += storyboard[:interval].milliseconds start_time += storyboard[:interval].milliseconds
end_time += storyboard[:interval].milliseconds end_time += storyboard[:interval].milliseconds

View File

@ -52,17 +52,13 @@ module Invidious::Videos
break break
end end
end end
result = String.build do |result|
result << <<-END_VTT
WEBVTT
Kind: captions
Language: #{tlang || @language_code}
settings_field = {
"Kind" => "captions",
"Language" => "#{tlang || @language_code}",
}
END_VTT result = WebVTT.build(settings_field) do |vtt|
result << "\n\n"
cues.each_with_index do |node, i| cues.each_with_index do |node, i|
start_time = node["t"].to_f.milliseconds start_time = node["t"].to_f.milliseconds
@ -76,29 +72,16 @@ module Invidious::Videos
end_time = start_time + duration end_time = start_time + duration
end end
# start_time text = String.build do |io|
result << start_time.hours.to_s.rjust(2, '0')
result << ':' << start_time.minutes.to_s.rjust(2, '0')
result << ':' << start_time.seconds.to_s.rjust(2, '0')
result << '.' << start_time.milliseconds.to_s.rjust(3, '0')
result << " --> "
# end_time
result << end_time.hours.to_s.rjust(2, '0')
result << ':' << end_time.minutes.to_s.rjust(2, '0')
result << ':' << end_time.seconds.to_s.rjust(2, '0')
result << '.' << end_time.milliseconds.to_s.rjust(3, '0')
result << "\n"
node.children.each do |s| node.children.each do |s|
result << s.content io << s.content
end
result << "\n"
result << "\n"
end end
end end
vtt.line(start_time, end_time, text)
end
end
return result return result
end end
end end

View File

@ -34,41 +34,15 @@ module Invidious::Videos
# Convert into array of TranscriptLine # Convert into array of TranscriptLine
lines = self.parse(initial_data) lines = self.parse(initial_data)
settings_field = {
"Kind" => "captions",
"Language" => target_language
}
# Taken from Invidious::Videos::Captions::Metadata.timedtext_to_vtt() # Taken from Invidious::Videos::Captions::Metadata.timedtext_to_vtt()
vtt = String.build do |vtt| vtt = WebVTT.build(settings_field) do |vtt|
vtt << <<-END_VTT
WEBVTT
Kind: captions
Language: #{target_language}
END_VTT
vtt << "\n\n"
lines.each do |line| lines.each do |line|
start_time = line.start_ms vtt.line(line.start_ms, line.end_ms, line.line)
end_time = line.end_ms
# start_time
vtt << start_time.hours.to_s.rjust(2, '0')
vtt << ':' << start_time.minutes.to_s.rjust(2, '0')
vtt << ':' << start_time.seconds.to_s.rjust(2, '0')
vtt << '.' << start_time.milliseconds.to_s.rjust(3, '0')
vtt << " --> "
# end_time
vtt << end_time.hours.to_s.rjust(2, '0')
vtt << ':' << end_time.minutes.to_s.rjust(2, '0')
vtt << ':' << end_time.seconds.to_s.rjust(2, '0')
vtt << '.' << end_time.milliseconds.to_s.rjust(3, '0')
vtt << "\n"
vtt << line.line
vtt << "\n"
vtt << "\n"
end end
end end