Repository navigation
Repair ID3 text tags written in a legacy encoding - #45
Open
milovidov983 wants to merge 1 commit into
Open
milovidov983 wants to merge 1 commit into
milovidov983 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Before — the same string in the player, under the title:
After — the same book with the fix applied:
Why it happens
The files declare ID3v2.3 text frames with encoding byte
0(ISO-8859-1) and then store Windows-1251 bytes: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
repairLegacyTagTextre-decodes the text before it reachesMetadata, andMediaAnalyzerapplies 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:
Café,Müller,Ação,TôiandÉTÉcome back unchanged.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.shregenerates it../gradlew voiceUnitTest lintKotlinpasses locally.CHANGELOG.mdhas an entry underUnreleased.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
TALBframe, which is why that line was already readable before this change.