Fix listen mode audio bitrate labels

This commit is contained in:
sangnguyen 2026-05-20 23:55:38 +07:00
parent e82ac674ae
commit d0b59861b5
4 changed files with 48 additions and 7 deletions

View File

@ -43,4 +43,27 @@ Spectator.describe "Utils" do
expect(decode_date("8 years ago")).to be_close(Time.utc - 8.years, 500.milliseconds)
end
end
describe "audio_bitrate_label" do
it "formats audio bitrates in kbps" do
expect(audio_bitrate_label(100_000)).to eq("100k")
expect(audio_bitrate_label(128_000)).to eq("128k")
expect(audio_bitrate_label(129_600)).to eq("130k")
end
end
describe "audio_quality_param_matches?" do
it "matches legacy raw-bitrate quality params" do
expect(audio_quality_param_matches?("128000k", 128_000)).to be_true
end
it "matches kbps quality params from the player label" do
expect(audio_quality_param_matches?("128k", 128_000)).to be_true
end
it "ignores non-audio quality params" do
expect(audio_quality_param_matches?("hd720", 128_000)).to be_false
expect(audio_quality_param_matches?("invalidk", 128_000)).to be_false
end
end
end

View File

@ -202,6 +202,23 @@ def number_to_short_text(number)
text
end
def audio_bitrate_kbps(bitrate)
((bitrate + 500) // 1000).to_i
end
def audio_bitrate_label(bitrate)
"#{audio_bitrate_kbps(bitrate)}k"
end
def audio_quality_param_matches?(quality : String, bitrate)
return false unless quality.ends_with?("k")
requested_bitrate = quality.rchop("k").to_i?
return false if requested_bitrate.nil?
bitrate == requested_bitrate || audio_bitrate_kbps(bitrate) == requested_bitrate
end
def arg_array(array, start = 1)
if array.size == 0
args = "NULL"

View File

@ -163,11 +163,11 @@ module Invidious::Routes::Watch
if params.listen
url = audio_streams[0]["url"].as_s
if params.quality.ends_with? "k"
audio_streams.each do |fmt|
if fmt["bitrate"].as_i == params.quality.rchop("k").to_i
url = fmt["url"].as_s
end
audio_streams.each do |fmt|
bitrate = fmt["bitrate"].as_i
if audio_quality_param_matches?(params.quality, bitrate)
url = fmt["url"].as_s
end
end
else

View File

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