diff options
| author | Omar Roth <omarroth@hotmail.com> | 2018-08-07 09:16:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-07 09:16:47 -0500 |
| commit | 187fa11c4afc9192575f822e1f127bbd0de57f4e (patch) | |
| tree | 233ceaed9c406c0fd3b4aa7bba806378f41e670f | |
| parent | 1da69d095c15223b28cacbdae4b8ea2819fb96e7 (diff) | |
| parent | 9a02b5278a42b8785ed761d95f2d98a21df8ae71 (diff) | |
| download | invidious-187fa11c4afc9192575f822e1f127bbd0de57f4e.tar.gz invidious-187fa11c4afc9192575f822e1f127bbd0de57f4e.tar.bz2 invidious-187fa11c4afc9192575f822e1f127bbd0de57f4e.zip | |
Merge pull request #100 from omarroth/fix-captions
Fix duration for auto-generated captions
| -rw-r--r-- | src/invidious.cr | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/invidious.cr b/src/invidious.cr index 986a0ab3..30a27058 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1646,16 +1646,24 @@ get "/api/v1/captions/:id" do |env| END_VTT - caption_xml.xpath_nodes("//transcript/text").each do |node| + 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 - end_time = start_time + duration + + 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(/<font color="#[a-fA-F0-9]{6}">/, "") + text = text.gsub(/<\/font>/, "") if md = text.match(/(?<name>.*) : (?<text>.*)/) text = "<v #{md["name"]}>#{md["text"]}</v>" end |
