This commit is contained in:
DUOLabs333 2023-01-03 10:21:16 -05:00
parent 8df1c3bb57
commit b49ed65a07

View File

@ -30,60 +30,60 @@ module Invidious::Videos
return captions_list return captions_list
end end
def timedtext_to_vtt(timedtext : String, tlang = nil) : String def timedtext_to_vtt(timedtext : String, tlang = nil) : String
#In the future, we could just directly work with the url. This is more of a POC # In the future, we could just directly work with the url. This is more of a POC
cues = [] of XML::Node cues = [] of XML::Node
tree = XML.parse(timedtext) tree = XML.parse(timedtext)
tree = tree.children.first() tree = tree.children.first
tree.children.each do |item| tree.children.each do |item|
if item.name == "body" if item.name == "body"
item.children.each do |cue| item.children.each do |cue|
if cue.name == "p" if cue.name == "p"
cues << cue cues << cue
end end
end end
break break
end end
end end
result = String.build do |result| result = String.build do |result|
result << <<-END_VTT result << <<-END_VTT
WEBVTT WEBVTT
Kind: captions Kind: captions
Language: #{tlang || @language_code} Language: #{tlang || @language_code}
END_VTT END_VTT
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
duration = node["d"]?.try &.to_f.milliseconds duration = node["d"]?.try &.to_f.milliseconds
duration ||= start_time duration ||= start_time
if cues.size > i + 1 if cues.size > i + 1
end_time = cues[i + 1]["t"].to_f.milliseconds end_time = cues[i + 1]["t"].to_f.milliseconds
else else
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')}" 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')}" 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 = String.build do |text| text = String.build do |text|
node.children.each do |s| node.children.each do |s|
text << s.content text << s.content
end end
end end
result << start_time + " --> " + end_time + "\n" result << start_time + " --> " + end_time + "\n"
result << text + "\n" result << text + "\n"
result << "\n" result << "\n"
end end
end end
return result return result
end end
# List of all caption languages available on Youtube. # List of all caption languages available on Youtube.
LANGUAGES = { LANGUAGES = {
"", "",
@ -217,6 +217,5 @@ module Invidious::Videos
"Yoruba", "Yoruba",
"Zulu", "Zulu",
} }
end end
end end