From 8df1c3bb57154dda021add8d11da9e7f4ae88bf1 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 10:17:47 -0500 Subject: [PATCH 01/10] Add support for timedtext captions --- src/invidious/routes/api/v1/videos.cr | 92 +++++++++++++++------------ src/invidious/videos/caption.cr | 56 +++++++++++++++- 2 files changed, 106 insertions(+), 42 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index a6b2eb4e..918fb421 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -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 if caption.name.includes? "auto-generated" caption_xml = YT_POOL.client &.get(url).body - caption_xml = XML.parse(caption_xml) - webvtt = String.build do |str| - str << <<-END_VTT - WEBVTT - Kind: captions - Language: #{tlang || caption.language_code} - - - END_VTT - - caption_nodes = caption_xml.xpath_nodes("//transcript/text") - caption_nodes.each_with_index do |node, i| - start_time = node["start"].to_f.seconds - duration = node["dur"]?.try &.to_f.seconds - duration ||= start_time - - if caption_nodes.size > i + 1 - end_time = caption_nodes[i + 1]["start"].to_f.seconds - 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 = HTML.unescape(node.content) - text = text.gsub(//, "") - text = text.gsub(/<\/font>/, "") - if md = text.match(/(?.*) : (?.*)/) - text = "#{md["text"]}" - end - - str << <<-END_CUE - #{start_time} --> #{end_time} - #{text} - - - END_CUE - end - end + if caption_xml.starts_with?(" i + 1 + end_time = caption_nodes[i + 1]["start"].to_f.seconds + 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 = HTML.unescape(node.content) + text = text.gsub(//, "") + text = text.gsub(/<\/font>/, "") + if md = text.match(/(?.*) : (?.*)/) + text = "#{md["text"]}" + end + + str << <<-END_CUE + #{start_time} --> #{end_time} + #{text} + + + 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 @@ -138,7 +143,12 @@ module Invidious::Routes::API::V1::Videos # # See: https://github.com/iv-org/invidious/issues/2391 webvtt = YT_POOL.client &.get("#{url}&format=vtt").body - .gsub(/([0-9:.]{12} --> [0-9:.]{12}).+/, "\\1") + if webvtt.starts_with?(" [0-9:.]{12}).+/, "\\1") + end end if title = env.params.query["title"]? diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 4642c1a7..941b9646 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -30,7 +30,60 @@ 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 From b49ed65a07d1e80eb5430b40dac47e7a2477cd39 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 10:21:16 -0500 Subject: [PATCH 02/10] Linting --- src/invidious/videos/caption.cr | 97 ++++++++++++++++----------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 941b9646..4049c5d0 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -30,60 +30,60 @@ 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 + + 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 - + 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 = { "", @@ -217,6 +217,5 @@ module Invidious::Videos "Yoruba", "Zulu", } - end end From 45b8f6d0cd89541d93a479ec20f43ee5c029abf8 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 10:25:05 -0500 Subject: [PATCH 03/10] More linting --- src/invidious/routes/api/v1/videos.cr | 74 +++++++++++++-------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 918fb421..eb371241 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -92,50 +92,50 @@ module Invidious::Routes::API::V1::Videos caption_xml = YT_POOL.client &.get(url).body if caption_xml.starts_with?(" i + 1 - end_time = caption_nodes[i + 1]["start"].to_f.seconds - 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 = HTML.unescape(node.content) - text = text.gsub(//, "") - text = text.gsub(/<\/font>/, "") - if md = text.match(/(?.*) : (?.*)/) - text = "#{md["text"]}" - end - - str << <<-END_CUE + + caption_nodes = caption_xml.xpath_nodes("//transcript/text") + caption_nodes.each_with_index do |node, i| + start_time = node["start"].to_f.seconds + duration = node["dur"]?.try &.to_f.seconds + duration ||= start_time + + if caption_nodes.size > i + 1 + end_time = caption_nodes[i + 1]["start"].to_f.seconds + 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 = HTML.unescape(node.content) + text = text.gsub(//, "") + text = text.gsub(/<\/font>/, "") + if md = text.match(/(?.*) : (?.*)/) + text = "#{md["text"]}" + end + + str << <<-END_CUE #{start_time} --> #{end_time} #{text} END_CUE - end - end - end + end + end + end else # Some captions have "align:[start/end]" and "position:[num]%" # attributes. Those are causing issues with VideoJS, which is unable @@ -144,11 +144,11 @@ module Invidious::Routes::API::V1::Videos # See: https://github.com/iv-org/invidious/issues/2391 webvtt = YT_POOL.client &.get("#{url}&format=vtt").body if webvtt.starts_with?(" [0-9:.]{12}).+/, "\\1") - end + 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"]? @@ -371,4 +371,4 @@ module Invidious::Routes::API::V1::Videos end end end -end +end \ No newline at end of file From 9d83e2da4e5c1dffc994dc8acd3f2a74280ffcc4 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 10:29:17 -0500 Subject: [PATCH 04/10] Add newline --- src/invidious/routes/api/v1/videos.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index eb371241..51344508 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -371,4 +371,4 @@ module Invidious::Routes::API::V1::Videos end end end -end \ No newline at end of file +end From 76758baab83b303e43a41a11bad37058c696905a Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Tue, 3 Jan 2023 13:10:26 -0500 Subject: [PATCH 05/10] Removed unneccesary String::Builder and removed cues that was just a blank line --- src/invidious/videos/caption.cr | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 4049c5d0..83a4c82f 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -41,7 +41,9 @@ module Invidious::Videos if item.name == "body" item.children.each do |cue| if cue.name == "p" - cues << cue + if !(cue.children.size == 1 && cue.children[0].content == "\n") + cues << cue + end end end break @@ -71,13 +73,13 @@ module Invidious::Videos 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" + + node.children.each do |s| + result << s.content + end + result << "\n" result << "\n" end end From 32471382c48289bafd0234d5e339fdfefb328da0 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Sun, 8 Jan 2023 16:18:35 -0500 Subject: [PATCH 06/10] Different cosmetic fixes --- src/invidious/routes/api/v1/videos.cr | 6 ++--- src/invidious/videos/caption.cr | 34 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 51344508..54602112 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -128,11 +128,11 @@ module Invidious::Routes::API::V1::Videos end str << <<-END_CUE - #{start_time} --> #{end_time} - #{text} + #{start_time} --> #{end_time} + #{text} - END_CUE + END_CUE end end end diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 83a4c82f..377f30d6 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -40,8 +40,7 @@ module Invidious::Videos tree.children.each do |item| if item.name == "body" item.children.each do |cue| - if cue.name == "p" - if !(cue.children.size == 1 && cue.children[0].content == "\n") + if cue.name == "p" && !(cue.children.size == 1 && cue.children[0].content == "\n") cues << cue end end @@ -51,12 +50,15 @@ module Invidious::Videos end result = String.build do |result| result << <<-END_VTT - WEBVTT - Kind: captions - Language: #{tlang || @language_code} - - - END_VTT + WEBVTT + Kind: captions + Language: #{tlang || @language_code} + + + END_VTT + + result << "\n\n" + cues.each_with_index do |node, i| start_time = node["t"].to_f.milliseconds @@ -70,11 +72,21 @@ module Invidious::Videos 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')}" + # start_time + 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') - 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')}" + result << " --> " - result << start_time + " --> " + end_time + "\n" + # 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| result << s.content From 4fc1b8ae86ab3d32955e72c957514799e5e121dc Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Sun, 8 Jan 2023 16:20:23 -0500 Subject: [PATCH 07/10] Remove superfluous 'end' --- src/invidious/videos/caption.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 377f30d6..95b9d643 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -42,7 +42,6 @@ module Invidious::Videos item.children.each do |cue| if cue.name == "p" && !(cue.children.size == 1 && cue.children[0].content == "\n") cues << cue - end end end break From 456e91426aeeafe889e2ea8887cfc3aa3f92fcd3 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Sun, 8 Jan 2023 16:44:44 -0500 Subject: [PATCH 08/10] Formatting --- src/invidious/videos/caption.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 95b9d643..03bc3fd0 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -41,7 +41,7 @@ module Invidious::Videos if item.name == "body" item.children.each do |cue| if cue.name == "p" && !(cue.children.size == 1 && cue.children[0].content == "\n") - cues << cue + cues << cue end end break @@ -56,8 +56,8 @@ module Invidious::Videos END_VTT - result << "\n\n" - + result << "\n\n" + cues.each_with_index do |node, i| start_time = node["t"].to_f.milliseconds From 4b2d9420247ab83b2690a331c727e0227b5b7a19 Mon Sep 17 00:00:00 2001 From: DUOLabs333 Date: Wed, 11 Jan 2023 15:58:07 -0500 Subject: [PATCH 09/10] Convert tabs to spaces --- src/invidious/routes/api/v1/videos.cr | 22 +++++++++++----------- src/invidious/videos/caption.cr | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 54602112..b10a30ea 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -98,12 +98,12 @@ module Invidious::Routes::API::V1::Videos webvtt = String.build do |str| str << <<-END_VTT - WEBVTT - Kind: captions - Language: #{tlang || caption.language_code} - - - END_VTT + WEBVTT + Kind: captions + Language: #{tlang || caption.language_code} + + + END_VTT caption_nodes = caption_xml.xpath_nodes("//transcript/text") caption_nodes.each_with_index do |node, i| @@ -128,11 +128,11 @@ module Invidious::Routes::API::V1::Videos end str << <<-END_CUE - #{start_time} --> #{end_time} - #{text} - - - END_CUE + #{start_time} --> #{end_time} + #{text} + + + END_CUE end end end diff --git a/src/invidious/videos/caption.cr b/src/invidious/videos/caption.cr index 03bc3fd0..13f81a31 100644 --- a/src/invidious/videos/caption.cr +++ b/src/invidious/videos/caption.cr @@ -49,12 +49,12 @@ module Invidious::Videos end result = String.build do |result| result << <<-END_VTT - WEBVTT - Kind: captions - Language: #{tlang || @language_code} + WEBVTT + Kind: captions + Language: #{tlang || @language_code} - END_VTT + END_VTT result << "\n\n" From 67ace4fd9dd1a62ab004b05dc0403cc71ef5e206 Mon Sep 17 00:00:00 2001 From: DUO Labs Date: Mon, 16 Jan 2023 18:50:38 -0500 Subject: [PATCH 10/10] Some indention changes Co-authored-by: Samantaz Fox --- src/invidious/routes/api/v1/videos.cr | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index b10a30ea..4ef877e5 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -98,12 +98,12 @@ module Invidious::Routes::API::V1::Videos webvtt = String.build do |str| str << <<-END_VTT - WEBVTT - Kind: captions - Language: #{tlang || caption.language_code} - - - END_VTT + WEBVTT + Kind: captions + Language: #{tlang || caption.language_code} + + + END_VTT caption_nodes = caption_xml.xpath_nodes("//transcript/text") caption_nodes.each_with_index do |node, i| @@ -128,11 +128,11 @@ module Invidious::Routes::API::V1::Videos end str << <<-END_CUE - #{start_time} --> #{end_time} - #{text} - - - END_CUE + #{start_time} --> #{end_time} + #{text} + + + END_CUE end end end