Skip to content

Commit 9bb6f74

Browse files
committed
fix: Calculate bitrate from actual file size instead of API value
The API returns theoretical max bitrate (e.g., 1411kbps for FLAC), but files may be compressed (e.g., 960kbps). Calculate real bitrate from downloaded file size and duration to ensure Telegram displays accurate values.
1 parent 571c68c commit 9bb6f74

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "music163bot-rust"
3-
version = "1.1.9"
3+
version = "1.1.10"
44
edition = "2024"
55
license = "WTFPL"
66

src/bot.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,24 @@ async fn download_and_send_music(
820820

821821
// Get file size for database (async to avoid blocking)
822822
let audio_file_size = audio_buffer.size().await as i64;
823+
let duration_sec = (song_detail.dt.unwrap_or(0) / 1000) as i64;
824+
825+
// Calculate actual bitrate from file size and duration
826+
// API's song_url.br is often theoretical (e.g., 1411kbps for FLAC) but
827+
// actual file may be compressed (e.g., 960kbps). Use real calculated value.
828+
let actual_bitrate_bps = if duration_sec > 0 {
829+
(8 * audio_file_size) / duration_sec
830+
} else {
831+
// Fallback to API value if duration is missing
832+
song_url.br as i64
833+
};
834+
835+
tracing::info!(
836+
"Bitrate - API: {} bps, Calculated from file: {} bps (duration: {}s)",
837+
song_url.br,
838+
actual_bitrate_bps,
839+
duration_sec
840+
);
823841

824842
// Create song info for database
825843
let mut song_info = SongInfo {
@@ -834,8 +852,8 @@ async fn download_and_send_music(
834852
music_size: audio_file_size,
835853
pic_size: 0,
836854
emb_pic_size: 0,
837-
bit_rate: song_url.br as i64,
838-
duration: (song_detail.dt.unwrap_or(0) / 1000) as i64,
855+
bit_rate: actual_bitrate_bps,
856+
duration: duration_sec,
839857
file_id: None,
840858
thumb_file_id: None,
841859
from_user_id: msg.from.as_ref().map_or(0, |u| u.id.0 as i64),

0 commit comments

Comments
 (0)