Repository navigation
[codex] fix ASS subtitle font loading - #28
Conversation
📝 WalkthroughWalkthroughAdds backend extraction and API endpoints for embedded ASS/SSA subtitle fonts, advertises per-track font_bundle_url, provides frontend fallback/font-loading utilities, integrates font delivery into useASSSubtitles, and adds tests and runtime font packages. ChangesEmbedded ASS/SSA Subtitle Font Bundle Support
Sequence Diagram(s)sequenceDiagram
participant Client
participant PlaybackHandler
participant StreamHandler
participant ExtractAttachedSubtitleFonts
participant ffprobe
participant ffmpeg
Client->>PlaybackHandler: GET /playback/start
PlaybackHandler-->>Client: subtitle_urls (font_bundle_url)
Client->>StreamHandler: GET /stream/{session}/subtitles/{track}/fonts
StreamHandler->>ExtractAttachedSubtitleFonts: inputPath, ffmpegPath
ExtractAttachedSubtitleFonts->>ffprobe: probe attachment streams
ffprobe-->>ExtractAttachedSubtitleFonts: stream JSON
ExtractAttachedSubtitleFonts->>ffmpeg: extract attachment (stdout)
ffmpeg-->>ExtractAttachedSubtitleFonts: font bytes
ExtractAttachedSubtitleFonts-->>StreamHandler: []SubtitleFontAttachment
StreamHandler-->>Client: JSON [{name, data}]
Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/api/handlers/stream.go`:
- Around line 299-334: The handler currently calls
playback.ExtractAttachedSubtitleFonts directly and returns a 500 when the file
is missing; call playback.Preflight (or the project's equivalent preflight
function) on file.FilePath (using r.Context() and h.FFmpegPath) before calling
playback.ExtractAttachedSubtitleFonts so missing/unavailable-file errors follow
the same abort flow as other stream handlers; if the preflight returns an error,
map it to the existing not-found/missing-file response (use writeError the same
way as the earlier file-not-found handling) and only proceed to
ExtractAttachedSubtitleFonts when preflight succeeds.
In `@internal/playback/subtitle_fonts.go`:
- Around line 199-206: ffprobePathFromFFmpeg rewrites the last "ffmpeg"
occurrence in the entire path which breaks when a parent directory contains
"ffmpeg"; instead make the replacement against the basename only: use
filepath.Base(ffmpegPath) and filepath.Dir(ffmpegPath), find/replace "ffmpeg" in
the base (preserving any suffix like ".exe"), then join dir and the new base
with filepath.Join; keep the existing fallbacks (empty input -> "ffprobe" and
default "ffprobe" when replacement not possible).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f4a46fde-1323-41a4-bfd0-e326a553f707
⛔ Files ignored due to path filters (1)
web/pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (12)
DockerfileDockerfile.devinternal/api/handlers/playback.gointernal/api/handlers/stream.gointernal/api/router.gointernal/playback/subtitle_fonts.goweb/package.jsonweb/src/player/hooks/useASSSubtitles.test.tsxweb/src/player/hooks/useASSSubtitles.tsweb/src/player/hooks/usePlaybackSession.tsweb/src/player/types.tsweb/src/player/utils/subtitleFonts.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/proxy/server.go (1)
207-225: 💤 Low valueMissing ASS/SSA track validation in proxy handler.
The authenticated handler in
stream.govalidates that the requested track is ASS/SSA before extracting fonts (lines 315-318), but this proxy handler extracts fonts for any track type without validation. This inconsistency allows unnecessary font extraction for non-ASS tracks (e.g., VTT/SRT) where fonts are irrelevant.Consider adding track codec validation. The
claimsstruct would need to carry subtitle track metadata, or the handler could accept this as a known limitation since invalid requests waste resources but don't cause incorrect behavior.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/proxy/server.go` around lines 207 - 225, handleSubtitleFonts currently parses the subtitle track but does not validate the track codec before calling playback.ExtractAttachedSubtitleFonts; update this handler to only attempt font extraction for ASS/SSA tracks by reusing the same validation logic used in the authenticated handler in stream.go (i.e., after parsing track via playback.ParseSubtitleTrackParam, check the track codec/type and return 400 (or skip extraction) if it is not ASS/SSA), and if necessary extend the claims struct to carry subtitle track metadata used for validation or fetch the track codec from the same source stream.go uses so that ExtractAttachedSubtitleFonts is only called for ASS/SSA tracks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/proxy/server.go`:
- Around line 207-225: handleSubtitleFonts currently parses the subtitle track
but does not validate the track codec before calling
playback.ExtractAttachedSubtitleFonts; update this handler to only attempt font
extraction for ASS/SSA tracks by reusing the same validation logic used in the
authenticated handler in stream.go (i.e., after parsing track via
playback.ParseSubtitleTrackParam, check the track codec/type and return 400 (or
skip extraction) if it is not ASS/SSA), and if necessary extend the claims
struct to carry subtitle track metadata used for validation or fetch the track
codec from the same source stream.go uses so that ExtractAttachedSubtitleFonts
is only called for ASS/SSA tracks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: d25dfbbe-a4b1-49e4-936f-c8922498f5fa
📒 Files selected for processing (6)
internal/api/handlers/playback.gointernal/api/handlers/stream.gointernal/playback/subtitle_fonts.gointernal/playback/subtitle_fonts_test.gointernal/proxy/server.goweb/src/player/utils/subtitleFonts.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- internal/api/handlers/playback.go
- web/src/player/utils/subtitleFonts.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/playback/subtitle_fonts_test.go (1)
70-81: ⚡ Quick winConsider testing edge cases for
ffprobePathFromFFmpeg.The test currently covers the main scenarios (basename replacement and fallback), but could be more comprehensive by adding cases for:
- Just
"ffmpeg"with no directory component- Absolute paths (e.g.,
"/usr/bin/ffmpeg")- Empty string input
- Path already ending in
"ffprobe"These edge cases would help ensure the function handles all inputs correctly and prevent potential bugs.
🧪 Example additional test cases
got = ffprobePathFromFFmpeg(filepath.Join("tmp", "ffmpeg-tools", "custom")) if got != "ffprobe" { t.Fatalf("ffprobePathFromFFmpeg custom basename = %q, want ffprobe", got) } + + // Edge case: just the basename + got = ffprobePathFromFFmpeg("ffmpeg") + if got != "ffprobe" { + t.Fatalf("ffprobePathFromFFmpeg bare ffmpeg = %q, want ffprobe", got) + } + + // Edge case: absolute path + got = ffprobePathFromFFmpeg(filepath.Join(string(filepath.Separator), "usr", "bin", "ffmpeg")) + want = filepath.Join(string(filepath.Separator), "usr", "bin", "ffprobe") + if got != want { + t.Fatalf("ffprobePathFromFFmpeg absolute path = %q, want %q", got, want) + } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/playback/subtitle_fonts_test.go` around lines 70 - 81, Add unit tests for ffprobePathFromFFmpeg to cover edge cases: call ffprobePathFromFFmpeg with just "ffmpeg" (expect "ffprobe"), with an absolute path like "/usr/bin/ffmpeg" (expect "/usr/bin/ffprobe"), with an empty string (decide and assert expected behavior, e.g., ""), and with a path already ending in "ffprobe" (expect the same input back). Update TestFFprobePathFromFFmpeg or add a new table-driven test that includes these cases and asserts the returned value for each input using the existing test helper pattern.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/playback/subtitle_fonts_test.go`:
- Around line 70-81: Add unit tests for ffprobePathFromFFmpeg to cover edge
cases: call ffprobePathFromFFmpeg with just "ffmpeg" (expect "ffprobe"), with an
absolute path like "/usr/bin/ffmpeg" (expect "/usr/bin/ffprobe"), with an empty
string (decide and assert expected behavior, e.g., ""), and with a path already
ending in "ffprobe" (expect the same input back). Update
TestFFprobePathFromFFmpeg or add a new table-driven test that includes these
cases and asserts the returned value for each input using the existing test
helper pattern.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: d302c8a6-8ea9-48bc-a437-9fc370fa4402
📒 Files selected for processing (3)
internal/api/handlers/stream.gointernal/playback/subtitle_fonts.gointernal/playback/subtitle_fonts_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
- internal/api/handlers/stream.go
- internal/playback/subtitle_fonts.go
Summary
Fixes ASS/SSA subtitle rendering for non-Latin scripts in the web player by making fonts explicit instead of relying on browser/local font probing.
font_bundle_urlfor embedded ASS/SSA tracks and add an authenticated stream endpoint that extracts font attachments from the source container.\fnoverrides to the selected fallback, and disable JASSUB local font probing to reducefontselectconsole noise.Root Cause
JASSUB/libass only renders missing glyphs through its configured default font and logs fontselect misses while trying to resolve style fonts such as
Trebuchet MS. Loading random extra fonts is not enough unless libass can actually select them. Thai tracks rendered as boxes because the default font did not cover Thai, and Arabic could still box for glyphs outside the available fallback set.Validation
pnpm exec vitest run src/player/hooks/useASSSubtitles.test.tsxpnpm exec eslint src/player/hooks/useASSSubtitles.ts src/player/hooks/useASSSubtitles.test.tsx src/player/utils/subtitleFonts.ts src/player/types.ts src/player/hooks/usePlaybackSession.tspnpm exec tsc -bpnpm exec vite buildgo test ./...git diff --checkNotes
Real-media playback should still be checked against the Arabic/Thai samples that exposed the issue, especially embedded fonts in MKV releases.
Summary by CodeRabbit
New Features
Enhancements
Tests