Add message for when chapters are auto generated

This commit is contained in:
syeopite 2023-08-21 22:14:40 -07:00
parent 00d16dff1f
commit a569b8f3d9
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
3 changed files with 24 additions and 2 deletions

View File

@ -238,6 +238,10 @@ struct Video
return @chapters return @chapters
end end
def automatically_generated_chapters? : Bool?
return @info["AutoGeneratedChapters"]?.try &.as_bool
end
def hls_manifest_url : String? def hls_manifest_url : String?
info.dig?("streamingData", "hlsManifestUrl").try &.as_s info.dig?("streamingData", "hlsManifestUrl").try &.as_s
end end

View File

@ -392,13 +392,25 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
# Chapters # Chapters
chapters_array = [] of JSON::Any chapters_array = [] of JSON::Any
chapters_auto_generated = nil
# Yes, `decoratedPlayerBarRenderer` is repeated twice. # Yes,`decoratedPlayerBarRenderer` is repeated twice.
if player_bar = player_overlays.try &.dig?("decoratedPlayerBarRenderer", "decoratedPlayerBarRenderer", "playerBar") if player_bar = player_overlays.try &.dig?("decoratedPlayerBarRenderer", "decoratedPlayerBarRenderer", "playerBar")
if markers = player_bar.dig?("multiMarkersPlayerBarRenderer", "markersMap") if markers = player_bar.dig?("multiMarkersPlayerBarRenderer", "markersMap")
potential_chapters_array = markers.as_a.find { |m| m["key"]? == "DESCRIPTION_CHAPTERS" } potential_chapters_array = markers.as_a.find { |m| m["key"]? == "DESCRIPTION_CHAPTERS" }
# Let manual chapters have higher precedence than automatically generated ones.
if !potential_chapters_array
potential_chapters_array = markers.as_a.find { |m| m["key"]? == "AUTO_CHAPTERS" }
end
if potential_chapters_array if potential_chapters_array
if potential_chapters_array["key"] == "AUTO_CHAPTERS"
chapters_auto_generated = true
else
chapters_auto_generated = false
end
chapters_array = potential_chapters_array["value"]["chapters"].as_a chapters_array = potential_chapters_array["value"]["chapters"].as_a
end end
end end
@ -450,6 +462,7 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
"authorVerified" => JSON::Any.new(author_verified || false), "authorVerified" => JSON::Any.new(author_verified || false),
"subCountText" => JSON::Any.new(subs_text || "-"), "subCountText" => JSON::Any.new(subs_text || "-"),
"AutoGeneratedChapters" => JSON::Any.new(chapters_auto_generated),
"chapters" => JSON::Any.new(chapters_array), "chapters" => JSON::Any.new(chapters_array),
} }

View File

@ -2,6 +2,11 @@
<div class="description-chapters-section"> <div class="description-chapters-section">
<hr class="description-content-separator"/> <hr class="description-content-separator"/>
<h4>Chapters</h4> <h4>Chapters</h4>
<% if video.automatically_generated_chapters? %>
<h5>Chapters are automatically generated </h5>
<% end %>
<div class="description-chapters-content-container"> <div class="description-chapters-content-container">
<% chapters.each do | chapter | %> <% chapters.each do | chapter | %>
<%- start_in_seconds = chapter.start_ms.milliseconds.total_seconds.to_i %> <%- start_in_seconds = chapter.start_ms.milliseconds.total_seconds.to_i %>