Skip to content

Repair ID3 text tags written in a legacy encoding - #45

Open
milovidov983 wants to merge 1 commit into
M1sterMo:mainfrom
milovidov983:fix/legacy-tag-encodings
Open

milovidov983 wants to merge 1 commit into
M1sterMo:mainfrom
milovidov983:fix/legacy-tag-encodings

Conversation

@milovidov983

@milovidov983 milovidov983 commented Nov 25, 2026 •

Copy link
Copy Markdown

Audiobooks whose tags were written by a tool running under a Cyrillic locale show every chapter title as Âàëèñ, while the file names, the folder name and the book name beside them read fine. Titles, artists, narrators and chapter names are all affected, so the player looks like it cannot handle Cyrillic at all.

Screenshots

Before — the chapter list, next to a book name that came from the folder name:

Screenshot_2026-11-25-15-28-51-828_com github mistermo_vibecode voiceplus

Before — the same string in the player, under the title:

Screenshot_2026-11-25-15-28-56-528_com github mistermo_vibecode voiceplus

After — the same book with the fix applied:

Screenshot_2026-11-25-16-34-33-012_com github mistermo_vibecode voiceplus debug Screenshot_2026-11-25-16-34-30-485_com github mistermo_vibecode voiceplus debug

Why it happens

The files declare ID3v2.3 text frames with encoding byte 0 (ISO-8859-1) and then store Windows-1251 bytes:

TIT2  enc=0x00  bytes: 00 C2 E0 EB E8 F1     ISO-8859-1: "Âàëèñ"    Windows-1251: "Валис"
TPE1  enc=0x00  bytes: 00 D4 2E 20 C4 E8 EA  ISO-8859-1: "Ô. Äèê"   Windows-1251: "Ф. Дик"

Media3 decodes the frame as declared, which is exactly what the spec asks for. But a large share of older rips and audiobook packs were tagged like this by tools running under a Cyrillic locale, and the mainstream players cope with them — foobar2000, VLC and mp3tag all re-decode such tags.

What this does

repairLegacyTagText re-decodes the text before it reaches Metadata, and MediaAnalyzer applies it to text frames (TIT2, TPE1, TALB, TCOM, TXXX), chapter frames and comments. The file itself is never touched, and nothing can be lost: every character of an ISO-8859-1 decode fits into one byte, so re-encoding recovers exactly the bytes the tagger wrote.

Only repairs the bytes can prove are attempted:

  1. UTF-8 written into a frame that declares ISO-8859-1 — the bytes either parse as UTF-8 or they don't. Independent of the script, so it helps every language, including those with no single-byte legacy encoding.
  2. Windows-1251 — accepted only when the decode turns a run of accented Latin letters into Cyrillic and leaves ASCII letters a minority. Café, Müller, Ação, Tôi and ÉTÉ come back unchanged.
  3. Windows-1252 punctuation that arrived as C1 control characters (U+0080–U+009F) — no title legitimately contains those.

What it deliberately leaves alone

Latin-alphabet legacy charsets (Windows-1250, -1254, -1257) and the double-byte Asian ones are not guessed at. In the few dozen bytes of a title they are indistinguishable from genuine Latin text, so a wrong guess would corrupt names that are correct today. Those are better served by letting the user name the charset when re-reading tags — a separate feature, and I am happy to take it on if you want it.

Greek (windows-1253) and Hebrew (windows-1255) sit in the same 0xC0–0xFF band as Cyrillic and decode just as cleanly from the same bytes, so adding them needs a discriminator beyond byte ranges. The reasoning is written down next to the charset table.

Tests

  • LegacyTagTextTest — repairs for Windows-1251, UTF-8 and Windows-1252, plus a "leaves alone" table: correct Latin-1 names, UTF-16 text in five scripts, short accented words, plain ASCII, and idempotence.
  • MediaAnalyzerTest.mp3WithLegacyCyrillicTags — a 3-second fixture tagged exactly like the files in the report, asserting title, artist, album and narrator end to end. create_legacy_cp1251_mp3.sh regenerates it.
  • I also ran the function over the 122 tag frames of a real Cyrillic audiobook: all 122 repair to the exact expected text.
  • ./gradlew voiceUnitTest lintKotlin passes locally.

CHANGELOG.md has an entry under Unreleased.

Note for books that were scanned before the update

Chapter names are cached per file, so an existing book keeps the mojibake until its tags are read again. Toggling "Use folder names instead of file tags" forces that re-scan today. A dedicated "re-read tags" action, or bumping a tag-parser version to re-read once after the update, would be a good follow-up — tell me which you would prefer and I will send it separately.

Not changed here

The book name on the player screen still comes from the folder name when the files carry no TALB frame, which is why that line was already readable before this change.

ID3v2 text frames declare ISO-8859-1 in their encoding byte, and taggers with a
Cyrillic locale set that byte correctly while writing Windows-1251 into the
frame. Media3 decodes such a frame as the spec says, so "Валис" reaches the
library as "Âàëèñ" and stays broken: book titles, authors, narrators and
chapter names all show mojibake while the file names beside them read fine.

Repairing the decoded string instead of the file keeps the audio untouched and
is lossless, because every character of an ISO-8859-1 decode fits into one
byte. Only repairs the bytes can prove are attempted:

- UTF-8 written into a frame declaring ISO-8859-1, for any script;
- Windows-1251, accepted only when the decode turns accented Latin letters
  into a run of Cyrillic ones, which leaves "Café" and "Müller" alone;
- Windows-1252 punctuation that arrived as C1 control characters.

Latin-alphabet legacy encodings and the double-byte Asian ones are left out on
purpose: in the few dozen bytes of a title they cannot be told apart from
genuine Latin text, and a wrong guess would corrupt correct names.

Books scanned before this need their tags read again; the setting that toggles
folder names forces that re-scan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant