Yokan / rebuild
Course 1 8624561

LOCAL_VIDEO_ARTWORK.md

docs/features/LOCAL_VIDEO_ARTWORK.md

documentation / design asset94 lines
1# Local video playback and frame-derived artwork23Covers diff row E10 (frame artwork) and the stage-5 local-video playback check for4local-library anime (`isLocalArchive = true`, source `local`).56## Playback path78Episode tap → `PlayerViewModel` (no source matches `local`, so `source = null`) →9`ProfileReaderContent.videos(null, chapter)` → `resolveLocalEpisodeVideo` (in10`LocalLibraryPaths.kt`): the chapter's virtual `archivePath` (`<folder name>/<rest>`)11is mapped through the *current* folder configuration, then the file itself (video12extension) or the first video in a folder is used → `LocalFileService.readableUrl`13(`file://<path>` natively, the OPFS media-worker URL `…/__yokan_files__/…` on web) →14`DbVideo(quality = "Downloaded")` → `MediaPlayerController.load`. A missing file yields15no stream (never the virtual path).1617Fixes made while verifying: libmpv refuses to create a client unless `LC_NUMERIC` is18"C", and the JVM adopts the user's locale (e.g. `LANG=en_US.UTF-8`), so desktop playback19and frame extraction failed whenever a locale was set. `vendor/kmp-mpv` now pins20`LC_NUMERIC` to "C" before `mpv_create` (macOS/Linux; Windows unaffected). Also, libmpv percent-decodes `file://` URLs, so a local file whose21name contains `%XX` (e.g. `Episode 2 100%20.mp4`) did not open. The mpv controller now22hands mpv the raw path for `file://` URLs without headers (Android/iOS/desktop).2324Verified (see the counts below):25- commonTest `LocalEpisodePlaybackTest`: real scanner over an in-memory tree → virtual26  path → playable `file://` URL; re-pointed folder follows; missing file → no stream;27  no extension runtime is created.28- desktopTest `LocalVideoDesktopTest`: real temp files (MKV and MP4 fixtures in29  `RedVideoFixtures`), real scanner and `JvmLocalFileService`, the player's resolution,30  then the real `MpvMediaPlayerController` (software render loop like the desktop31  surface, `vo=libmpv`): duration becomes known (~1 s) and the decoded red frame renders,32  including the `%20` file name. Skipped with an assumption when libmpv is neither bundled33  nor installed. Manual probe on this Mac: both the bundled media-kit libmpv and Homebrew34  libmpv load the fixtures and render the frame.35- wasmJsTest `BrowserLocalVideoTest`: real OPFS scan → media-worker URL from36  `ProfileReaderContent.videos` → a real `<video>` decode of a WebM fixture.3738Not verified: on-device Android/iOS playback (no instrumented/simulator tests here);39mpv's handling of SAF `content://` URLs is unchanged by this work.4041## Frame artwork4243After every successful `LocalLibraryController.rescan` (scan loop untouched), a44background job on the profile scope runs `LocalVideoArtwork.generate()`:45- **Cover**: for a local anime title with no `customCoverImage`, `imageUrl` or46  `customCoverFromTracker` (scanned `cover.*` files and `metadata.json`/tracker art47  populate those first), the first episode's frame becomes `customCoverImage`. A Seasons48  parent without episodes uses its first season's first episode. Yokan has no separate49  background image: the detail header uses the cover, so this also covers background art.50- **Thumbnails**: an episode whose `thumbnailUrl` is null (no `episodes.json` preview)51  gets the cached frame's readable URL. Scans keep it (`previewUrl ?: existing`); a52  sidecar preview added later replaces it, and a cover file added later replaces the53  frame cover.54- **Never overwrites**: every write re-reads the row inside `database.batch` while holding55  the library operation lock (the same lock scans and entry management hold), so a cover56  the user picks or a preview a scan writes while a frame decodes wins.57- **Bounds**: one decode at a time, at most 12 decodes per run (later scans continue),58  frames fit 512×512 px, JPEG output over 1 MiB is dropped. Only one run at a time;59  failures are remembered for the session and not retried until restart.60- **Cache**: `<downloadRoot>/.yokan-artwork/<sha256(virtual path|size|mtime)>.jpg`61  (OPFS on web). Written as `.part` then promoted; cancellation or failure deletes the62  partial file. Later runs, a cleared and rescanned library, or a new session reuse the63  file without decoding; a changed file (size/mtime) is a new key. When the provider64  exposes no size/mtime, the key falls back to the virtual path alone.65- **Lifetime**: the job is a child of the profile session scope; retirement cancels and66  joins it before the database and file service close.6768Per-platform extraction (`createPlatformVideoFrameExtractor`), frame at ~10% of duration:69- **Android**: `MediaMetadataRetriever` (`setDataSource(path)` for files, `(context, uri)`70  for SAF `content://`), `getScaledFrameAtTime` on API 27+, scaled bitmap on API 26.71  Blocking work runs on `blockingDispatcher`; a decode in progress finishes before cancel.72- **iOS**: `AVAssetImageGenerator` (`appliesPreferredTrackTransform`, `maximumSize`) inside73  `IosSecurityScopedBookmarks.withAccessAsync`, so bookmarked/open-in-place folders work.74  Cancellation calls `cancelAllCGImageGeneration`. Containers AVFoundation cannot read75  (e.g. MKV, AVI) produce no artwork.76- **Desktop**: a headless instance of the player's libmpv (`vo=libmpv`, software render77  API scales; Skia encodes JPEG). The media-kit libmpv bundled for macOS/Windows lacks78  lavfi `scale` and image encoders, so `screenshot-to-file` is not usable. Linux needs a79  system libmpv; without any libmpv, extraction returns null (no artwork).80- **Web**: a muted detached `<video>` on the same-origin media-worker URL, canvas81  `drawImage` + `toBlob('image/jpeg')`. Codecs/containers the browser cannot decode82  (MKV/HEVC in most browsers) produce no artwork. Cancellation abandons, but cannot abort,83  the browser decode; a 20 s timeout bounds it.8485No preference: the defaults only fill fields that were empty; stored data and backups86keep their existing shape (`customCoverImage` bytes, `thumbnailUrl` string).8788Verification passed 167 desktop checks, 84 Kotlin/Wasm browser tests and 17 Android bridge unit checks without failures/errors/skips, and all four targets compiled (shared stage 5/6 run). The three desktop libmpv checks ran89(no skips) after the locale fix. Tests: commonTest `LocalVideoArtworkTest` (cache reuse and90content keys, per-run bound, failed/empty/oversized frames, existing artwork and91non-local titles untouched, concurrent edit and running-scan win, cancellation during92decode and during write leaves no partial file, Seasons parent); desktopTest93`LocalVideoDesktopTest` (real libmpv frames from MKV/MP4 within bounds, end-to-end run);94wasmJsTest `BrowserLocalVideoTest`.95

Search chapters, reference and source filenames.