From b5918e1cbb2173f1b4a4ead8196a89ce9238e6ce Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Wed, 13 May 2026 22:22:57 -0600 Subject: [PATCH] fix(library): recover Plex album format + bit-depth when codec is missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Plex aggregation CTE used MAX(codec)/MAX(bit_depth) directly, which produced two bugs that surfaced in the Albums grid for many Plex-only libraries: - format = "Unknown". Plex frequently omits the stream-level codec when its analyzer hasn't seen the file yet. MAX(NULL) collapsed the row's format to NULL, which the frontend parser mapped to AudioFormat::Unknown — the Library grid then showed UNKNOWN even though the file's container was clearly "flac" / "mp3". - bit_depth = NULL. Plex commonly omits bitDepth on older releases entirely; the album row inherited the gap and the UI rendered "--/44.1" next to an otherwise valid quality string. Two fallbacks applied to the plex_aggregated CTE only — the local CTE is untouched because local indexing always writes a non-null format string and a real bit depth via Symphonia: - format: COALESCE(MAX(codec), MAX(container)). The `container` column is populated for the same media and typically carries the same value ("flac", "mp3"), so it's a safe fallback when codec is fully NULL across the album. - bit_depth: COALESCE(MAX(bit_depth), CASE WHEN AND sample_rate <= 48000 THEN 16 ELSE NULL END). Lossless formats at CD-range rates are virtually always 16-bit redbook, so we default to 16 in that narrow window. Higher rates stay NULL so we don't lie about 24 vs 32. The per-track view shows the real value when the user clicks in (lazy hydration via plex_get_track_metadata). cc --- crates/qbz-library/src/database.rs | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/qbz-library/src/database.rs b/crates/qbz-library/src/database.rs index 1ee153f6e..ac0442b5e 100644 --- a/crates/qbz-library/src/database.rs +++ b/crates/qbz-library/src/database.rs @@ -2030,8 +2030,37 @@ impl LibraryDatabase { MAX(CASE WHEN artwork_path IS NOT NULL THEN artwork_path END) AS artwork, COUNT(*) AS track_count, CAST(SUM(COALESCE(duration_ms, 0)) / 1000 AS INTEGER) AS total_duration, - MAX(codec) AS format, - MAX(bit_depth) AS bit_depth, + -- Plex stream-level `codec` is often missing when the + -- server hasn't fully analyzed a track. The `container` + -- field is populated for the same media and usually + -- carries the same value ("flac", "mp3", etc.), so it + -- works as a fallback. Without it, any album where + -- Plex didn't expose codec on every track ends up + -- labeled "Unknown" in the UI even though the file + -- format is known via container. Local CTE is not + -- affected — local indexing always writes a non-null + -- format string. + COALESCE(MAX(codec), MAX(container)) AS format, + -- Plex frequently omits bitDepth from its Media/Stream + -- XML for older releases; the aggregated row inherits + -- the gap as NULL. When the format ends up lossless + -- and sample rate sits at CD range (<= 48 kHz), + -- default to 16 — that's the universal CD-Audio / + -- redbook assumption that virtually every lossless + -- album at that rate matches. Higher rates leave the + -- field NULL (could be 24, could be 32) and the UI + -- falls back to its "--" placeholder; the per-track + -- view shows the real value when the user clicks in. + COALESCE( + MAX(bit_depth), + CASE + WHEN LOWER(COALESCE(MAX(codec), MAX(container))) IN + ('flac', 'alac', 'wav', 'aiff', 'ape') + AND MAX(sampling_rate_hz) <= 48000 + THEN 16 + ELSE NULL + END + ) AS bit_depth, CAST(MAX(sampling_rate_hz) AS REAL) AS sample_rate, CAST(NULL AS TEXT) AS source_folders, 'plex' AS source