Add logic to swap languages in transcript widget

This commit is contained in:
syeopite 2024-06-13 09:33:34 -07:00
parent 5ba6baea19
commit b781036404
No known key found for this signature in database
GPG Key ID: A73C186DA3955A1A
4 changed files with 88 additions and 18 deletions

View File

@ -874,4 +874,18 @@ h1, h2, h3, h4, h5, p,
.transcript-line:hover, .selected.transcript-line, .transcript-title-line:hover, .selected.transcript-title-line { .transcript-line:hover, .selected.transcript-line, .transcript-title-line:hover, .selected.transcript-title-line {
background: #cacaca; background: #cacaca;
}
.video-transcript > footer {
padding-bottom: 14px;
border-top: 1px solid #a0a0a0
}
.video-transcript > footer > form {
display: flex;
justify-content: center;
}
.video-transcript > footer select {
width: 75%;
} }

View File

@ -506,5 +506,6 @@
"video_description_show_transcript_section_button": "Show transcript", "video_description_show_transcript_section_button": "Show transcript",
"video_description_show_transcript_section_button_hide": "Hide transcript", "video_description_show_transcript_section_button_hide": "Hide transcript",
"error_transcripts_none_available": "No transcripts are available", "error_transcripts_none_available": "No transcripts are available",
"transcript_widget_title": "Transcript" "transcript_widget_title": "Transcript",
"transcript_widget_no_js_change_transcript_btn": "Swap"
} }

View File

@ -43,6 +43,9 @@ module Invidious::Routes::Watch
show_transcripts ||= "0" show_transcripts ||= "0"
show_transcripts = show_transcripts == "1" show_transcripts = show_transcripts == "1"
# Equal to a `caption.name` when set
selected_transcript = env.params.query["use_this_transcript"]?
preferences = env.get("preferences").as(Preferences) preferences = env.get("preferences").as(Preferences)
user = env.get?("user").try &.as(User) user = env.get?("user").try &.as(User)
@ -162,26 +165,55 @@ module Invidious::Routes::Watch
captions = captions - preferred_captions captions = captions - preferred_captions
if show_transcripts if show_transcripts
# The transcripts available are the exact same as the amount of captions available. Thus: # Transcripts can be mapped 1:1 to a video's captions.
if !preferred_captions.empty? # As such the amount of transcripts available is the same as the amount of captions available.
chosen_transcript = preferred_captions[0] #
transcript_request_param = Invidious::Videos::Transcript.generate_param( # To request transcripts we have to give a language code, and a boolean dictating whether or not
id, chosen_transcript.language_code, chosen_transcript.auto_generated # it is auto-generated. These attributes can be retrieved from the video's caption metadata.
)
elsif !captions.empty? # First we check if a transcript has been explicitly selected.
chosen_transcript = captions[0] # The `use_this_transcript` url parameter provides the label of the transcript the user wants.
transcript_request_param = Invidious::Videos::Transcript.generate_param( if selected_transcript
id, chosen_transcript.language_code, chosen_transcript.auto_generated selected_transcript = URI.decode_www_form(selected_transcript)
) target_transcript = captions.select(&.name.== selected_transcript)
else else
return error_template(404, "error_transcripts_none_available") target_transcript = nil
end end
transcript = Invidious::Videos::Transcript.from_raw( # If the selected transcript has a match then we'll request that.
YoutubeAPI.get_transcript(transcript_request_param), #
chosen_transcript.language_code, # If it does not match we'll try and request a transcript based on the user's
chosen_transcript.auto_generated, # preferred transcript
#
# If that also does not match then we'll just select the first transcript
# out of everything that's available.
#
# Raises when no matches are found
if target_transcript.is_a?(Array) && !target_transcript.empty?
target_transcript = target_transcript[0]
else
if !preferred_captions.empty?
target_transcript = preferred_captions[0]
elsif !captions.empty?
target_transcript = captions[0]
else
return error_template(404, "error_transcripts_none_available")
end
end
transcript_request_param = Invidious::Videos::Transcript.generate_param(
id, target_transcript.language_code, target_transcript.auto_generated
) )
begin
transcript = Invidious::Videos::Transcript.from_raw(
YoutubeAPI.get_transcript(transcript_request_param),
target_transcript.language_code,
target_transcript.auto_generated,
)
rescue NotFoundException
return error_template(404, "error_transcripts_none_available")
end
else else
transcript = nil transcript = nil
end end

View File

@ -19,5 +19,28 @@
<% end %> <% end %>
<% end %> <% end %>
</div> </div>
<footer></footer> <footer>
<% transcript_select_args = env.params.query.dup %>
<% transcript_select_args.delete_all("use_this_transcript") %>
<form class="select-transcript" method="get" action="/watch?<%=transcript_select_args%>">
<% # Preserve query parameters %>
<% transcript_select_args.each do | k, v | %>
<input type="hidden" name="<%=k%>" value="<%=v%>">
<% end %>
<select name="use_this_transcript">
<% {preferred_captions, captions}.each do | transcript_list |%>
<% transcript_list.each do | transcript_option | %>
<% if target_transcript.not_nil!.name == transcript_option.name %>
<option value="<%=URI.encode_www_form(transcript_option.name)%>" selected><%=transcript_option.name%></option>
<% else%>
<option value="<%=URI.encode_www_form(transcript_option.name)%>"><%=transcript_option.name%></option>
<% end %>
<% end %>
<% end %>
</select>
<input type="submit" value="<%= translate(locale, "transcript_widget_no_js_change_transcript_btn") %>"/>
</form>
</footer>
</section> </section>