mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-30 02:32:56 +05:30
Add support for timedtext captions
This commit is contained in:
parent
865704dc7b
commit
8df1c3bb57
@ -90,6 +90,10 @@ module Invidious::Routes::API::V1::Videos
|
||||
# as well as some other markup that makes it cumbersome, so we try to fix that here
|
||||
if caption.name.includes? "auto-generated"
|
||||
caption_xml = YT_POOL.client &.get(url).body
|
||||
|
||||
if caption_xml.starts_with?("<?xml")
|
||||
webvtt =caption.timedtext_to_vtt(caption_xml,tlang)
|
||||
else
|
||||
caption_xml = XML.parse(caption_xml)
|
||||
|
||||
webvtt = String.build do |str|
|
||||
@ -131,15 +135,21 @@ module Invidious::Routes::API::V1::Videos
|
||||
END_CUE
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
# Some captions have "align:[start/end]" and "position:[num]%"
|
||||
# attributes. Those are causing issues with VideoJS, which is unable
|
||||
# to properly align the captions on the video, so we remove them.
|
||||
#
|
||||
# See: https://github.com/iv-org/invidious/issues/2391
|
||||
webvtt = YT_POOL.client &.get("#{url}&format=vtt").body
|
||||
if webvtt.starts_with?("<?xml")
|
||||
webvtt = caption.timedtext_to_vtt(webvtt)
|
||||
else
|
||||
webvtt = YT_POOL.client &.get("#{url}&format=vtt").body
|
||||
.gsub(/([0-9:.]{12} --> [0-9:.]{12}).+/, "\\1")
|
||||
end
|
||||
end
|
||||
|
||||
if title = env.params.query["title"]?
|
||||
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
||||
|
@ -31,6 +31,59 @@ module Invidious::Videos
|
||||
return captions_list
|
||||
end
|
||||
|
||||
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
|
||||
cues = [] of XML::Node
|
||||
tree = XML.parse(timedtext)
|
||||
tree = tree.children.first()
|
||||
|
||||
tree.children.each do |item|
|
||||
if item.name == "body"
|
||||
item.children.each do |cue|
|
||||
if cue.name == "p"
|
||||
cues << cue
|
||||
end
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
result = String.build do |result|
|
||||
result << <<-END_VTT
|
||||
WEBVTT
|
||||
Kind: captions
|
||||
Language: #{tlang || @language_code}
|
||||
|
||||
|
||||
END_VTT
|
||||
cues.each_with_index do |node,i|
|
||||
start_time = node["t"].to_f.milliseconds
|
||||
|
||||
duration = node["d"]?.try &.to_f.milliseconds
|
||||
|
||||
duration ||= start_time
|
||||
|
||||
if cues.size > i + 1
|
||||
end_time = cues[i + 1]["t"].to_f.milliseconds
|
||||
else
|
||||
end_time = start_time + duration
|
||||
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 = String.build do |text|
|
||||
node.children.each do |s|
|
||||
text << s.content
|
||||
end
|
||||
end
|
||||
result << start_time + " --> " + end_time + "\n"
|
||||
result << text + "\n"
|
||||
result << "\n"
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
# List of all caption languages available on Youtube.
|
||||
LANGUAGES = {
|
||||
"",
|
||||
@ -164,5 +217,6 @@ module Invidious::Videos
|
||||
"Yoruba",
|
||||
"Zulu",
|
||||
}
|
||||
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user