Support injectable charset detection for standalone SubRip subtitles - #3368
Open
hoyahozz wants to merge 1 commit into
Open
Support injectable charset detection for standalone SubRip subtitles#3368hoyahozz wants to merge 1 commit into
hoyahozz wants to merge 1 commit into
Conversation
Standalone SubRip subtitles without a byte order mark are decoded as UTF-8, which corrupts files that use legacy character encodings. Add an injectable CharsetDetector to DefaultSubtitleParserFactory and use it when parsing standalone SubRip files. Keep BOM precedence, UTF-8 fallback, and embedded Matroska/WebM subtitle behavior unchanged. Issue: androidx#2247
Collaborator
|
Thanks for the contribution. @icbaker I think that matches the idea in google/ExoPlayer#3644 (comment) and could probably be merged. |
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.
Summary
External standalone and raw-text SubRip inputs without a supported UTF byte order mark currently fall back to UTF-8. As a result, subtitles encoded with legacy character encodings such as GB18030, EUC-KR, or Shift_JIS may be decoded incorrectly.
This change introduces an injectable
CharsetDetectorcontract in the extractor text package and passes it throughDefaultSubtitleParserFactoryto applicableSubripParserinstances. Applications can provide their own charset detection implementation without requiring Media3 to depend on a specific detection library.This follows the extension-point approach proposed in google/ExoPlayer#3644.
Behavior
null: a UTF-8 or UTF-16 byte order mark still wins, and everything else falls back to UTF-8.Formats whosecontainerMimeTypeis null or a text MIME type, i.e. standalone.srtinput.CharsetDetector.detect()receives exactly theoffsetandlengthpassed toSubtitleParser.parse().DefaultSubtitleParserFactory.SubripParseritself always uses whatever detector it is constructed with.This PR intentionally does not provide a charset detection implementation. Applications remain responsible for choosing and configuring one.
Example - injecting a detector for an external SRT
The following example assumes that
appCharsetDetectorwraps a detection library selected by the application.When the external SRT is opened,
DefaultMediaSourceFactorycreates a standalone subtitleFormat.DefaultSubtitleParserFactorythen creates aSubripParserwith the configured detector. If the input has no supported UTF BOM, the parser asks the detector to inspect the exact requested byte range and decodes that range using the returned charset.The same detector is also used for raw-text SubRip inputs. It is not passed to SubRip samples embedded in Matroska or WebM containers.
Before / After
The same BOM-free subtitle files were played on the same physical device. Without a detector, the existing UTF-8 fallback produces replacement characters. Injecting a detector that returns the matching charset restores the original Korean, Chinese, and Japanese text.
EUC-KR)GB18030)Shift_JIS)Fixes #2247