|
|
@ -90,47 +90,52 @@ module Invidious::Routes::API::V1::Videos
|
|
|
|
# as well as some other markup that makes it cumbersome, so we try to fix that here
|
|
|
|
# as well as some other markup that makes it cumbersome, so we try to fix that here
|
|
|
|
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
|
|
|
|
caption_xml = XML.parse(caption_xml)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
webvtt = String.build do |str|
|
|
|
|
if caption_xml.starts_with?("<?xml")
|
|
|
|
str << <<-END_VTT
|
|
|
|
webvtt =caption.timedtext_to_vtt(caption_xml,tlang)
|
|
|
|
WEBVTT
|
|
|
|
else
|
|
|
|
Kind: captions
|
|
|
|
caption_xml = XML.parse(caption_xml)
|
|
|
|
Language: #{tlang || caption.language_code}
|
|
|
|
|
|
|
|
|
|
|
|
webvtt = String.build do |str|
|
|
|
|
|
|
|
|
str << <<-END_VTT
|
|
|
|
END_VTT
|
|
|
|
WEBVTT
|
|
|
|
|
|
|
|
Kind: captions
|
|
|
|
caption_nodes = caption_xml.xpath_nodes("//transcript/text")
|
|
|
|
Language: #{tlang || caption.language_code}
|
|
|
|
caption_nodes.each_with_index do |node, i|
|
|
|
|
|
|
|
|
start_time = node["start"].to_f.seconds
|
|
|
|
|
|
|
|
duration = node["dur"]?.try &.to_f.seconds
|
|
|
|
END_VTT
|
|
|
|
duration ||= start_time
|
|
|
|
|
|
|
|
|
|
|
|
caption_nodes = caption_xml.xpath_nodes("//transcript/text")
|
|
|
|
if caption_nodes.size > i + 1
|
|
|
|
caption_nodes.each_with_index do |node, i|
|
|
|
|
end_time = caption_nodes[i + 1]["start"].to_f.seconds
|
|
|
|
start_time = node["start"].to_f.seconds
|
|
|
|
else
|
|
|
|
duration = node["dur"]?.try &.to_f.seconds
|
|
|
|
end_time = start_time + duration
|
|
|
|
duration ||= start_time
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if caption_nodes.size > i + 1
|
|
|
|
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 = caption_nodes[i + 1]["start"].to_f.seconds
|
|
|
|
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')}"
|
|
|
|
else
|
|
|
|
|
|
|
|
end_time = start_time + duration
|
|
|
|
text = HTML.unescape(node.content)
|
|
|
|
end
|
|
|
|
text = text.gsub(/<font color="#[a-fA-F0-9]{6}">/, "")
|
|
|
|
|
|
|
|
text = text.gsub(/<\/font>/, "")
|
|
|
|
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')}"
|
|
|
|
if md = text.match(/(?<name>.*) : (?<text>.*)/)
|
|
|
|
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 = "<v #{md["name"]}>#{md["text"]}</v>"
|
|
|
|
|
|
|
|
end
|
|
|
|
text = HTML.unescape(node.content)
|
|
|
|
|
|
|
|
text = text.gsub(/<font color="#[a-fA-F0-9]{6}">/, "")
|
|
|
|
str << <<-END_CUE
|
|
|
|
text = text.gsub(/<\/font>/, "")
|
|
|
|
#{start_time} --> #{end_time}
|
|
|
|
if md = text.match(/(?<name>.*) : (?<text>.*)/)
|
|
|
|
#{text}
|
|
|
|
text = "<v #{md["name"]}>#{md["text"]}</v>"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
END_CUE
|
|
|
|
str << <<-END_CUE
|
|
|
|
end
|
|
|
|
#{start_time} --> #{end_time}
|
|
|
|
end
|
|
|
|
#{text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
END_CUE
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
else
|
|
|
|
else
|
|
|
|
# Some captions have "align:[start/end]" and "position:[num]%"
|
|
|
|
# Some captions have "align:[start/end]" and "position:[num]%"
|
|
|
|
# attributes. Those are causing issues with VideoJS, which is unable
|
|
|
|
# attributes. Those are causing issues with VideoJS, which is unable
|
|
|
@ -138,7 +143,12 @@ module Invidious::Routes::API::V1::Videos
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# See: https://github.com/iv-org/invidious/issues/2391
|
|
|
|
# See: https://github.com/iv-org/invidious/issues/2391
|
|
|
|
webvtt = YT_POOL.client &.get("#{url}&format=vtt").body
|
|
|
|
webvtt = YT_POOL.client &.get("#{url}&format=vtt").body
|
|
|
|
.gsub(/([0-9:.]{12} --> [0-9:.]{12}).+/, "\\1")
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if title = env.params.query["title"]?
|
|
|
|
if title = env.params.query["title"]?
|
|
|
|