fix: show readable audio quality labels

Convert audio stream bitrates from bits per second to kilobit labels in listen mode so the quality selector shows values like 128k instead of 128000k.

Closes iv-org/invidious#2513
This commit is contained in:
JasonDavis666 2026-04-26 22:18:42 +08:00
parent 9eda6e5bc4
commit ea0f99b741
3 changed files with 14 additions and 2 deletions

View File

@ -31,6 +31,13 @@ Spectator.describe "Helper" do
end end
end end
describe "#format_audio_quality_label" do
it "formats audio bitrates as readable kilobit labels" do
expect(Helpers.format_audio_quality_label(128000)).to eq("128k")
expect(Helpers.format_audio_quality_label(50000)).to eq("50k")
end
end
describe "#sign_token" do describe "#sign_token" do
it "correctly signs a given hash" do it "correctly signs a given hash" do
token = { token = {

View File

@ -40,6 +40,10 @@ module Helpers
return description return description
end end
def format_audio_quality_label(bitrate : Int) : String
"#{(bitrate / 1000).round.to_i}k"
end
def cache_annotation(id, annotations) def cache_annotation(id, annotations)
if !CONFIG.cache_annotations if !CONFIG.cache_annotations
return return

View File

@ -28,12 +28,13 @@
src_url = invidious_companion.public_url.to_s + src_url + src_url = invidious_companion.public_url.to_s + src_url +
"&check=#{invidious_companion_check_id}" if (invidious_companion) "&check=#{invidious_companion_check_id}" if (invidious_companion)
bitrate = fmt["bitrate"] bitrate = fmt["bitrate"].as_i
quality_label = Helpers.format_audio_quality_label(bitrate)
mimetype = HTML.escape(fmt["mimeType"].as_s) mimetype = HTML.escape(fmt["mimeType"].as_s)
selected = (i == best_m4a_stream_index) selected = (i == best_m4a_stream_index)
%> %>
<source src="<%= src_url %>" type='<%= mimetype %>' label="<%= bitrate %>k" selected="<%= selected %>"> <source src="<%= src_url %>" type='<%= mimetype %>' label="<%= quality_label %>" selected="<%= selected %>">
<% if !params.local && !CONFIG.disabled?("local") %> <% if !params.local && !CONFIG.disabled?("local") %>
<source src="<%= src_url %>&local=true" type='<%= mimetype %>' hidequalityoption="true"> <source src="<%= src_url %>&local=true" type='<%= mimetype %>' hidequalityoption="true">
<% end %> <% end %>