fix(library): recover Plex album format + bit-depth when codec is missing

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 <lossless> 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
This commit is contained in:
vicrodh committed 2026-05-13 22:22:57 -06:00
1 parent 0f5bff3acf
commit b5918e1cbb
1 file changed
+31 -2
+31 -2
View File
@@ -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