diff --git a/CLAUDE.md b/CLAUDE.md index cffd4ff..8f034fe 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,7 +26,7 @@ xcodebuild -project Subler.xcodeproj -scheme Subler -configuration Debug build No deployment-target override is needed: the projects moved from upstream's 10.13 to `$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)` (recent toolchains refuse 10.13). -There is **no test target and no CI** — changes are verified by building and running the app manually. Of the `Utilities/` tools, MP4Dump (legacy ObjC debugging) is not part of the app build, but `ParseFilename/` **is** — it's copied into the app bundle and executed at runtime by `MetadataHelper.parseFilename` via `Process`. `extractsubs` is also copied into the bundle (Resources phase → `Contents/Resources/extractsubs`, exec bit preserved by the copy) but **nothing in the app invokes it** — it's a user-facing CLI helper shipped with the app: it extracts subtitle tracks from mkv/mp4 files via ffmpeg stream copy (named `.[.]..` — the shape `getFilenameLanguage`/`getFilenameSubtitlesTrackName` probe, so results drag straight back in) and runs `sup2vob --full` on extracted PGS `.sup` files. It needs ffprobe/ffmpeg/sup2vob on PATH (sup2vob builds from `MP42Foundation/sup2vob/` with `make && sudo make install`), plus mkvtoolnix and python3 only for its `S_TEXT/WEBVTT` rescue path, and it skips extraction with a clear message rather than failing per-track mid-pipeline. +There is **no test target and no CI** — changes are verified by building and running the app manually. Of the `Utilities/` tools, MP4Dump (legacy ObjC debugging) is not part of the app build, but `ParseFilename/` **is** — it's copied into the app bundle and executed at runtime by `MetadataHelper.parseFilename` via `Process`. `extractsubs` is also copied into the bundle (Resources phase → `Contents/Resources/extractsubs`, exec bit preserved by the copy) but **nothing in the app invokes it** — it's a user-facing CLI helper shipped with the app: it extracts subtitle tracks from mkv/mp4 files via ffmpeg stream copy (named `.[.]..` — the shape `getFilenameLanguage`/`getFilenameSubtitlesTrackName` probe, so results drag straight back in) and runs `sup2vob --full` on extracted PGS `.sup` files. It needs ffprobe/ffmpeg on PATH; sup2vob comes from PATH **or from the bundle next to the script** (`Contents/MacOS/sup2vob`, built by the app — see the submodule section), so the bundled pair needs no install. mkvtoolnix and python3 are only needed for its `S_TEXT/WEBVTT` rescue path, and it skips extraction with a clear message rather than failing per-track mid-pipeline. ## Architecture @@ -50,7 +50,7 @@ Referenced by the main project; `MP42Foundation.framework` is linked and copied The app never parses MP4/atom data directly — codec, container, and subtitle-conversion work belongs in the submodule, not in `Classes/`. `Classes/MP42FileAdditions.swift` extends framework types with app-level conveniences. -PGS `.sup` subtitle import: `MP42VobSubImporter` claims both `idx` and `sup`. A `.sup` is converted up-front to a private temp `.idx/.sub` pair via `MP42SupConverter` (the `@objc` façade over the sup2vob Swift sources vendored in `MP42/Sup2Vob/` from https://git.technomancer.com/Scott-Files/sup2vob — upstream logic is kept identical so upstream fixes re-merge cleanly; upstream's CLI-only globals and `main.swift` are the framework's only adaptations), then the existing VobSub machinery handles everything: the track comes out as `kMP42SubtitleCodecType_VobSub` (muxable straight into MP4 as `subp` via `MP4AddSubpicTrack`, or tx3g-convertible via `MP42BitmapSubConverter`), the frame size rides the idx `size:` line into `track.width/height`, language comes from the source filename (`getFilenameLanguage`, since PGS carries none — the generated idx `id:` is just "en"), and `track.URL` stays the user's `.sup`. The façade always runs upstream's `--full` mode (`fullFramePassthrough = true`, set in the adaptation layer `MP42SupConverter.swift`): frames keep the geometry the PGS itself defines — the idx size line is the **PCS canvas** (e.g. 1920x1080) and nothing is downscaled to the 720x480/576 DVD frame, which now only survives as fallback for a stream with no usable canvas (the framework's output is byte-identical to `sup2vob --full`). The temp pair is deleted in the importer's `dealloc` — safe because tracks hold the importer strongly through export. The ObjC→Swift hop uses the existing `MP42-Shared-Swift.h` shim (generated header), new Swift files must join both targets' Sources phases like `SampleDescription.swift`. The **same vendored sources** also build the CLI: `MP42Foundation/sup2vob/` holds only upstream's `main.swift` (adapted once: the two CLI globals `debugFrames`/`fullFramePassthrough` belong to the vendored `Convert.swift`, so main.swift drops its re-declaration) plus a `Makefile` compiling against `../MP42/Sup2Vob/` — one upstream merge updates app and CLI together, nothing can drift, and the CLI's `--full` output doubles as the byte-identical oracle for the framework converter (`make && sudo make install` puts `sup2vob` on PATH for the app-bundled `extractsubs`). +PGS `.sup` subtitle import: `MP42VobSubImporter` claims both `idx` and `sup`. A `.sup` is converted up-front to a private temp `.idx/.sub` pair via `MP42SupConverter` (the `@objc` façade over the sup2vob Swift sources vendored in `MP42/Sup2Vob/` from https://git.technomancer.com/Scott-Files/sup2vob — upstream logic is kept identical so upstream fixes re-merge cleanly; upstream's CLI-only globals and `main.swift` are the framework's only adaptations), then the existing VobSub machinery handles everything: the track comes out as `kMP42SubtitleCodecType_VobSub` (muxable straight into MP4 as `subp` via `MP4AddSubpicTrack`, or tx3g-convertible via `MP42BitmapSubConverter`), the frame size rides the idx `size:` line into `track.width/height`, language comes from the source filename (`getFilenameLanguage`, since PGS carries none — the generated idx `id:` is just "en"), and `track.URL` stays the user's `.sup`. The façade always runs upstream's `--full` mode (`fullFramePassthrough = true`, set in the adaptation layer `MP42SupConverter.swift`): frames keep the geometry the PGS itself defines — the idx size line is the **PCS canvas** (e.g. 1920x1080) and nothing is downscaled to the 720x480/576 DVD frame, which now only survives as fallback for a stream with no usable canvas (the framework's output is byte-identical to `sup2vob --full`). The temp pair is deleted in the importer's `dealloc` — safe because tracks hold the importer strongly through export. The ObjC→Swift hop uses the existing `MP42-Shared-Swift.h` shim (generated header), new Swift files must join both targets' Sources phases like `SampleDescription.swift`. The **same vendored sources** also build the CLI: `MP42Foundation/sup2vob/` holds only upstream's `main.swift` (adapted once: the two CLI globals `debugFrames`/`fullFramePassthrough` belong to the vendored `Convert.swift`, so main.swift drops its re-declaration) plus a `Makefile` compiling against `../MP42/Sup2Vob/` — one upstream merge updates app and CLI together, nothing can drift, and the CLI's `--full` output doubles as the byte-identical oracle for the framework converter. The Subler app build **invokes this same Makefile** (the "Build sup2vob CLI" run-script phase, `make -C MP42Foundation/sup2vob` — incremental, a no-op when unchanged) and installs the product into `Subler.app/Contents/MacOS/sup2vob`, so building Subler refreshes the CLI; a terminal `sup2vob` can just be a symlink to that bundle binary. The phase is why the Subler target sets `ENABLE_USER_SCRIPT_SANDBOXING = NO` (build-script file access within the source tree only — unrelated to the app's runtime security); the CLI itself also stays buildable standalone (`make && sudo make install`). PGS-from-MKV → VobSub: a PGS track inside an MKV imports as a **real subp track**, not just OCR'd tx3g text. `FileImportController`'s subtitle action sheet gains a `VobSub` entry (tag 2, default follows `Prefs.subtitleConvertBitmap` like VobSub sources) for PGS-format tracks; selecting it sets `MP42ConversionSettings.subtitlesVobSubConversion`, and the muxer's bitmap-converter branch recognizes PGS-source + VobSub-target and uses `MP42PgsToVobSubConverter` instead of `MP42BitmapSubConverter`. That converter runs the whole sup2vob conversion synchronously in `initWithTrack:error:` (sup2vob derives one global palette/geometry for the track — the PGS canvas size — and the muxer needs `magicCookie` + `frameWidth/Height` before creating the subpic track); the live muxing stream is discarded and `copyEncodedSample` replays prepass-collected samples, ending on exactly one EOF-flagged buffer. **Matroska traps found empirically here:** mkvmerge stores each PGS segment with its 13-byte "PG" envelope removed (blocks are `type` + 2-byte-be `len` + payload repeated, blank frames are real blank presentations, everything zlib-compressed) — a plain concatenation of demuxed blocks is NOT a `.sup`; phase 1 must walk each block's stripped segments and rebuild `PG + pts:4(90kHz from the block's ns timestamp ×9/100000) + 4 zero bytes + type + len` (mkvextract reconstructs exactly this — the rebuilt stream is byte-identical to it; blocks that already carry envelopes pass through verbatim for other muxers). The rebuilt temp `.sup` is fed to a second `MP42VobSubImporter` (the same machinery as `.sup` drag-import) whose demux supplies the subpic samples/palette. The converter header isn't exported (matches `MP42BitmapSubConverter`'s non-Public Headers-phase convention), and it can't be exercised from outside the framework via `copyNextSample`/`MP42FileImporter+Private` — those are `objc_direct`/internal, so harnesses must stay in public API (see the `@selector`-on-direct-method constraint noted in the converter source). diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index f508e17..494b636 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -809,6 +809,7 @@ 8D15AC330486D014006FF6A4 /* Frameworks */, A984DB5E0F52D68E001A20B2 /* Copy Files */, A922E0272233C9AC0072FD1B /* Copy Files */, + A9E77B152E5C0FF4E0000004 /* Build sup2vob CLI */, ); buildRules = ( ); @@ -945,6 +946,28 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + A9E77B152E5C0FF4E0000004 /* Build sup2vob CLI */ = { + isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Build sup2vob CLI"; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/zsh; + shellScript = "# The sup2vob CLI ships inside the app bundle (Contents/MacOS) so it\n# updates with every Subler build; make is incremental, so this is a\n# no-op unless the conversion sources or main.swift changed. The recipe\n# lives in the submodule's Makefile — do not duplicate it here.\nset -eu\nmake -C \"${SRCROOT}/MP42Foundation/sup2vob\"\ninstall -m 755 \"${SRCROOT}/MP42Foundation/sup2vob/sup2vob\" \"${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/MacOS/sup2vob\"\n"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ 8D15AC300486D014006FF6A4 /* Sources */ = { isa = PBXSourcesBuildPhase; @@ -1522,6 +1545,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + ENABLE_USER_SCRIPT_SANDBOXING = NO; /* the "Build sup2vob CLI" phase runs the submodule Makefile over MP42/Sup2Vob */ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; ASSETCATALOG_COMPILER_OPTIMIZATION = space; @@ -1570,6 +1594,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + ENABLE_USER_SCRIPT_SANDBOXING = NO; /* the "Build sup2vob CLI" phase runs the submodule Makefile over MP42/Sup2Vob */ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; ASSETCATALOG_COMPILER_OPTIMIZATION = space; diff --git a/Utilities/extractsubs b/Utilities/extractsubs index 534a94f..969a771 100755 --- a/Utilities/extractsubs +++ b/Utilities/extractsubs @@ -37,7 +37,8 @@ then fi # --- dependency checks ------------------------------------------------------ -# ffprobe, ffmpeg and sup2vob must all be on PATH, and the ffmpeg build must +# ffprobe and ffmpeg must be on PATH (sup2vob too, or it is taken from the +# Subler.app bundle beside this script), and the ffmpeg build must # include muxers for every extension stage 2 can write ("sup" is the one # minimal builds strip). Check up front so a missing or broken tool prints a # clear message and exits 1 instead of failing per track mid-pipeline. @@ -50,10 +51,26 @@ die() } missing=() -for tool in ffprobe ffmpeg sup2vob +for tool in ffprobe ffmpeg do command -v "${tool}" >/dev/null 2>&1 || missing+=("${tool}") done + +# sup2vob is looked up on PATH first (a `make install` copy, or a symlink to +# the one bundled with Subler.app), then as this script's bundle sibling: +# the app build compiles sup2vob into Contents/MacOS beside this script's +# Contents/Resources home, so running the bundled script needs no install. + +sup2vob_bin="" +if command -v sup2vob >/dev/null 2>&1 +then + sup2vob_bin="sup2vob" +else + sibling="$(dirname "$0")/../MacOS/sup2vob" + [ -x "${sibling}" ] && sup2vob_bin="${sibling}" +fi +[ -n "${sup2vob_bin}" ] || missing+=("sup2vob") + if [ "${#missing[@]}" -gt 0 ] then printf 'extractsubs: missing dependency on PATH: %s\n' "${missing[*]}" >&2 @@ -67,19 +84,21 @@ then printf ' e.g. brew install ffmpeg\n' >&2 ;; sup2vob) - printf ' build and install it from sup2vob/: make && sudo make install\n' >&2 + printf ' build and install it: cd MP42Foundation/sup2vob && make && sudo make install\n' >&2 + printf ' (or run the copy bundled in Subler.app/Contents/Resources — the app\n' >&2 + printf ' carries sup2vob at Contents/MacOS/sup2vob and needs no install)\n' >&2 ;; esac done exit 1 fi -# Found on PATH is not enough — a broken install (bad arch, half-updated -# Homebrew) must fail here too, so give each tool a cheap smoke run. +# Found is not enough — a broken install (bad arch, half-updated Homebrew) +# must fail here too, so give each tool a cheap smoke run. ffprobe -version >/dev/null 2>&1 || die "ffprobe is on PATH but does not run (ffprobe -version failed)" ffmpeg -version >/dev/null 2>&1 || die "ffmpeg is on PATH but does not run (ffmpeg -version failed)" -sup2vob --help >/dev/null 2>&1 || die "sup2vob is on PATH but does not run (sup2vob --help failed)" +"${sup2vob_bin}" --help >/dev/null 2>&1 || die "sup2vob (${sup2vob_bin}) does not run (${sup2vob_bin} --help failed)" # Feature check: ffmpeg must be able to mux every format stage 2 can write — # "sup" for pgs, "srt", "ass" and "webvtt" for text. There is no "ssa" muxer @@ -354,5 +373,5 @@ fi cat /tmp/extractsubs.$$ | egrep "\.sup$" | cut -f 3- -d ':' | while IFS= read -r supfile do [ -z "${supfile}" ] && continue - sup2vob --full "${supfile}" + "${sup2vob_bin}" --full "${supfile}" done