Skip to content

feat(subtitles): Blu-ray picture subtitles (.sup) to SRT and ASS with OCR - #1995

Open
YusufMntm wants to merge 1 commit into
KnifeLemon:mainfrom
YusufMntm:feat/sup-ocr-1872
Open

YusufMntm wants to merge 1 commit into
KnifeLemon:mainfrom
YusufMntm:feat/sup-ocr-1872

Conversation

@YusufMntm

Copy link
Copy Markdown
Contributor

Fixes #1872

What and why

.sup files (Blu-ray PGS picture subtitles) weren't opened at all, and pictures need text recognition before they
can become SRT or ASS. As you suggested, this uses the OCR Filee already has: Tesseract, and Apple Vision on a Mac.
Scope is .sup files. VobSub (.idx/.sub) and picture subtitle tracks inside videos are left for follow-ups;
MediaEncoding's refusal for those is unchanged.

Design

  • PgsReader (new, Subtitles/): reads the segments, written from the format in Filee's own words.
    • Segments: compositions (with cropping), windows, palettes (BT.709 limited-range YCbCr → RGB, alpha kept), and
      objects split over several data segments, with every run-length code form.
    • Objects and palettes last for an epoch. A display set without objects ends the caption, and so does the next
      caption. A palette-only update (a fade) stays one caption. A last caption that never ends gets 5 s.
    • It yields each caption as soon as it ends, so a film's bitmaps are never all in memory: each one is written as a
      PNG and dropped.
    • Every read is bounds-checked, with caps per object (4096×4096), per caption and per file (100,000 captions). A
      file with nothing readable refuses with the new error.sup_no_captions.
  • SubtitleOcr (new, Ocr/): objects are read top to bottom, then left to right, and joined by line.
    • Each object becomes dark text on white (255 − luma·alpha/255, so the light fill turns dark and the dark outline
      light), padded 16 px and doubled when under 100 px high, then written with SkiaSharp.
    • Tesseract reads them in batches through its list-file mode (--psm 6, pages split on form feeds), with the
      existing parallel runs. If a batch doesn't split into the right number of pages, it falls back to one picture per
      call.
    • Lines are trimmed and spaces collapsed. Spaces between CJK characters are removed as in [Bug]: Converting PDF to Word may result in formatting errors. #691
      (PdfDocumentReader.IsCjk is now internal); Korean keeps its spaces.
    • Output goes through the existing SubtitleWriter, with temp file + move.
  • Edges: TesseractConverter and VisionConverter get sup → srt and sup → txt (DirectOnly). These are plain
    edges, not Ocr: true, since .sup always needs OCR whatever the PDF switch says. ASS, WebVTT, TTML, SAMI, LRC and
    MicroDVD come through SRT in two steps.
    • Tesseract's language-data copy and process call are shared helpers now; ToPdfAsync behaves as before.
    • The Mac path uses ITextRecognizer and the same flow, and Vision stays first in EngineRegistry.
  • OCR language: the preset's Pdf.OcrLanguage. To make it choosable, subtitle-target presets show the language picker
    without the "Recognize text" switch. The OCR rows moved out of the PDF card into an OCR card of their own (shown for
    the existing OCR targets as before, plus subtitle targets), with one new caption line,
    presets.ocr_language_subtitles. That's the most visible UI change here: if you'd rather keep the rows inside the
    PDF card, or show the picker only for presets that can take .sup, I'll change it.
  • Format: sup is a read-only subtitle format. FormatFields: Specialist in video-pro, with ASS/TTML/SCC. Schema 112
    teaches the subtitles donut .sup (if another PR takes 112 first I'll renumber). SharedPresetVersions: 2.6.0.
  • i18n: error.sup_no_captions and presets.ocr_language_subtitles in all 23 files. Docs: README subtitles row and
    docs/ENGINES.md (Subtitles, Tesseract, Vision). CHANGELOG.md is left alone; the lines are in a comment below.

Tests

  • PgsTests (with a PgsBuilder that draws text, quantizes it and RLE-encodes it into segments):
    • timings (cleared, replaced, open end), fades, every RLE form and short rows;
    • fragmented objects with cropping, and the palette conversion;
    • malformed, truncated, overflowing and absurd-size input is refused or stopped;
    • the first caption is yielded before the rest of the stream is read;
    • the OCR preparation, line cleanup and form-feed splitting;
    • two objects come out top to bottom (through the Vision flow with a stand-in recognizer);
    • with the real Tesseract (skips without it): a two-caption .sup → SRT, ASS and TXT with both texts and the
      right times.
  • OcrRouteTests: sup → srt, the two-step targets, direct sup → txt, no sup → docx.
  • SupFormatTests: detection, donut, grade and field, v111 → v112 migration. FormatFieldsTests: Specialist count
    146 → 147.
  • RenderTests.Preset_editor_shows_and_saves_text_recognition: SRT and ASS presets show and save the language.

Ran locally (Windows, Tesseract 5.4 from fetch-engines.ps1): Filee.Engines.Tests (Pgs, Subtitle, Ocr: 188 pass,
1 unrelated skip), Filee.Core.Tests (FormatFields, Sup, OcrRoute, SharedPreset, PresetGallery, UserDataStore: 414
pass, 1 skip), Filee.App.Tests (Localization, RenderTests: 737 pass), dotnet format --verify-no-changes on the
changed files.

Not tried: a .sup ripped from a real Blu-ray (only files built in the tests), and Apple Vision on a real Mac. Real
discs have coloured, outlined or italic captions, which may read less cleanly than the test captions.

🤖 Generated with Claude Code

… OCR

Filee didn't open .sup files, and picture subtitles can only become text
through OCR (KnifeLemon#1872). Filee already has Tesseract (and Apple Vision on macOS).

PgsReader reads the PGS segments (compositions, windows, palettes, run-length
objects split over several segments, epochs, palette-only updates) and yields
each caption with its times as soon as it ends, so a film's worth of bitmaps
never sits in memory at once. SubtitleOcr turns each object into dark text on
white (padded, doubled when small), reads them in batches through Tesseract's
list-file mode (one picture per call if a batch can't be split), cleans the
lines (CJK spaces as in KnifeLemon#691) and writes SRT or text with SubtitleWriter;
ASS, WebVTT and the rest follow through SRT. Vision gets the same edges.

.sup is a read-only Specialist subtitle format in the video-pro field, learned
by the subtitles donut in schema 112. Subtitle presets show the OCR language
picker (without the OCR switch) in an OCR card of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@YusufMntm
YusufMntm requested a review from KnifeLemon as a code owner December 2, 2026 16:13
@YusufMntm

Copy link
Copy Markdown
Contributor Author

The CHANGELOG.md lines for this one, for the CHANGELOG PR:

ko: Blu-ray 그림 자막(.sup)을 OCR로 읽어 SRT, ASS 같은 텍스트 자막으로 바꿀 수 있어요
en: Blu-ray picture subtitles (.sup) convert to SRT, ASS and the other text subtitle formats with OCR
- **Blu-ray picture subtitles to text** (#1872). `.sup` (Blu-ray PGS) files are read by Filee and their pictures
  recognized with Tesseract (or Apple Vision on a Mac) into SRT, ASS, WebVTT, TTML, SAMI, LRC, MicroDVD or text, in
  the preset's OCR language. Subtitle presets show the OCR language picker.

@github-actions github-actions Bot added the feature New features label Dec 2, 2026

@KnifeLemon KnifeLemon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, @YusufMntm! The PGS reader is careful: segments, RLE, crops and palette indexes are all bounds-checked. The Tesseract list-file batching with the per-picture fallback is a nice touch. A few things before we can merge:

1. Decoded objects can use unbounded memory (blocking)

PgsReader.ParseOds decodes every complete object right away and keeps it in objects[objectId] until an epoch-start PCS.

  • DecodeRle allocates width × height bytes, up to 16 MiB at the pixel cap, even when the RLE data is empty.
  • Nothing limits how many object IDs are kept, and ODS segments are parsed even without a PCS.

So a .sup made only of ~24-byte ODS segments with distinct IDs (first+last flags, 4096×4096, no data) holds about 16 GiB after 1,000 segments. That's a 24 KB file. With all 65,536 IDs it's far more. Any of these would fix it:

  • cap the objects per epoch (PGS allows 64);
  • cap their total decoded bytes;
  • keep the RLE and decode an object only when a composition shows it.

2. Per-object size cap

MaxObjectPixels limits the pixel count, so a 65535×256 object passes. With a height under 100, PrepareBitmap then doubles it to about 131k px wide. Could the cap be per side (4096 each, as the description says), or checked against the composition's video size?

3. Palette-only updates

Each palette-only display set re-renders the whole active caption and sums its alpha twice. A small file with many of them makes the conversion crawl. Either of these would avoid it:

  • a limit on re-renders per caption;
  • comparing the palettes' alpha instead of rendering.

4. OCR language for .sup → TXT

sup → txt uses Pdf.OcrLanguage, but TXT isn't a subtitle target, so the picker only shows when "Recognize text" is on. That switch also changes how PDFs and pictures are handled. Could TXT presets show the picker too, the way subtitle targets do?

5. Schema number

#1994 (.msi) also takes schema 112. Whichever of the two merges second has to move to 113: CurrentSchemaVersion, the from < 113 migration and its test. We'll ping you on the one that needs it.

6. Small wording things

  • ko.json: the two new strings use 습니다/입니다, and the rest of the file uses 해요체. Suggested: "이 파일에는 Filee가 읽을 수 있는 Blu-ray 자막(.sup)이 없어요" and "그림 자막(.sup)을 읽을 때 쓸 자막 언어예요".
  • en.json: most error.* strings have no trailing period, so error.sup_no_captions could drop it.

Thanks for the CHANGELOG lines in the comment. We'll add them in the CHANGELOG PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Format]: .sup

2 participants