Skip to content

Split any YouTube song into stems

TypeScript

fix: arm64 ffmpeg for mac — macOS 27 ships without Rosetta

Daniel Ravina1d797a7beda2
diff --git a/README.md b/README.md
index 607973e..90b17ad 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,7 @@ Wrong Node version? Scripts auto-relaunch with a suitable one (nvm / nvm-windows
## Build & release
```bash
-bash scripts/fetch-ffmpeg.sh # mac/linux (one time)
+bash scripts/fetch-ffmpeg.sh # mac (builds arm64 ffmpeg + libsoxr, needs cmake) / linux (one time)
powershell scripts/fetch-ffmpeg.ps1 # windows (one time)
npm run dist # mac dmg -> release/
diff --git a/scripts/fetch-ffmpeg.sh b/scripts/fetch-ffmpeg.sh
index 431ad04..6fbe106 100755
--- a/scripts/fetch-ffmpeg.sh
+++ b/scripts/fetch-ffmpeg.sh
@@ -5,24 +5,107 @@ ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OS="$(uname -s)"
mkdir -p "$ROOT/extras"
+# the pipeline resamples with soxr, so every bundled ffmpeg must be built
+# with libsoxr. There is no public static macOS arm64 build with libsoxr
+# (evermeet.cx is x86_64-only, osxexperts.net ships arm64 without soxr), so
+# the mac path compiles ffmpeg + soxr from source — Apple Silicon only,
+# matching the arm64-only electron build
+SOXR_VERSION="0.1.3"
+FFMPEG_VERSION="9.0.2"
+
+function mac_ffmpeg_is_usable() {
+ local bin="$1"
+ [[ -x "$bin" ]] || return 1
+ file "$bin" 2>/dev/null | grep -q arm64 || return 1
+ "$bin" -hide_banner -buildconf 2>/dev/null | grep -q -- --enable-libsoxr
+}
+
+function build_mac_ffmpeg() {
+ local out="$1"
+ # hw.optional.arm64 is kernel truth — a shell/binary running under Rosetta
+ # makes uname -m report x86_64
+ if [[ "$(sysctl -n hw.optional.arm64 2>/dev/null)" != "1" ]]; then
+ echo "the macOS bundle targets Apple Silicon — run this on an arm64 Mac"
+ exit 1
+ fi
+ if [[ "$(uname -m)" != "arm64" ]]; then
+ # this script itself is running translated (e.g. an x86_64 anaconda or
+ # Homebrew bash resolved from PATH) — re-exec natively so clang builds
+ # the arm64 slice configure would otherwise pick
+ exec arch -arm64 /bin/bash "$0" "$@"
+ fi
+ if ! command -v cmake >/dev/null 2>&1; then
+ echo "cmake is required to build the bundled ffmpeg — install it first (e.g. brew install cmake)"
+ exit 1
+ fi
+
+ TMP="$(mktemp -d)"
+ trap 'rm -rf "$TMP"' EXIT
+ PREFIX="$TMP/prefix"
+ JOBS="$(sysctl -n hw.ncpu)"
+
+ echo "building libsoxr $SOXR_VERSION (arm64)..."
+ curl -fsSL -o "$TMP/soxr.tar.gz" "https://github.com/chirlu/soxr/archive/refs/tags/$SOXR_VERSION.tar.gz"
+ tar -xzf "$TMP/soxr.tar.gz" -C "$TMP"
+ cmake -S "$TMP/soxr-$SOXR_VERSION" -B "$TMP/soxr-build" \
+ -DBUILD_SHARED_LIBS=OFF \
+ -DBUILD_TESTS=OFF \
+ -DCMAKE_BUILD_TYPE=Release \
+ -DCMAKE_OSX_ARCHITECTURES=arm64 \
+ -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0 \
+ -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
+ -DCMAKE_INSTALL_PREFIX="$PREFIX" >/dev/null
+ cmake --build "$TMP/soxr-build" --target install --parallel "$JOBS" >/dev/null
+
+ echo "building ffmpeg $FFMPEG_VERSION (arm64, static libsoxr) — this takes a few minutes..."
+ curl -fsSL -o "$TMP/ffmpeg.tar.xz" "https://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.xz"
+ tar -xf "$TMP/ffmpeg.tar.xz" -C "$TMP"
+ (
+ cd "$TMP/ffmpeg-$FFMPEG_VERSION"
+ PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig" ./configure \
+ --enable-libsoxr \
+ --disable-autodetect \
+ --enable-zlib \
+ --enable-bzlib \
+ --disable-ffplay \
+ --disable-ffprobe \
+ --disable-doc \
+ --disable-debug \
+ --pkg-config-flags=--static \
+ --extra-cflags="-I$PREFIX/include -mmacosx-version-min=11.0" \
+ --extra-ldflags="-L$PREFIX/lib -mmacosx-version-min=11.0" >/dev/null
+ make --silent --jobs "$JOBS" >/dev/null
+ )
+
+ mkdir -p "$OUT"
+ cp -f "$TMP/ffmpeg-$FFMPEG_VERSION/ffmpeg" "$out"
+ chmod +x "$out"
+ xattr -dr com.apple.quarantine "$out" 2>/dev/null || true
+
+ if ! mac_ffmpeg_is_usable "$out"; then
+ echo "built ffmpeg is not usable (wrong arch or missing libsoxr)" >&2
+ rm -f "$out"
+ exit 1
+ fi
+}
+
if [[ "$OS" == "Darwin" ]]; then
OUT="$ROOT/extras/ffmpeg-mac"
- if [[ -x "$OUT/ffmpeg" ]]; then
+ if mac_ffmpeg_is_usable "$OUT/ffmpeg"; then
echo "ffmpeg already present: $OUT/ffmpeg"
"$OUT/ffmpeg" -version | head -1
exit 0
fi
- mkdir -p "$OUT"
- echo "downloading static ffmpeg for macOS..."
- curl -fsSL -o "$ROOT/extras/ffmpeg-mac.zip" "https://evermeet.cx/ffmpeg/get/ffmpeg/zip"
- unzip -oq "$ROOT/extras/ffmpeg-mac.zip" -d "$OUT"
- rm -f "$ROOT/extras/ffmpeg-mac.zip"
- chmod +x "$OUT/ffmpeg"
+ if [[ -e "$OUT/ffmpeg" ]]; then
+ echo "existing ffmpeg is unusable (wrong CPU arch or missing libsoxr) — replacing it..."
+ rm -f "$OUT/ffmpeg"
+ fi
+ build_mac_ffmpeg "$OUT/ffmpeg"
"$OUT/ffmpeg" -version | head -1
echo "saved to $OUT/ffmpeg"
elif [[ "$OS" == "Linux" ]]; then
OUT="$ROOT/extras/ffmpeg-linux"
- if [[ -x "$OUT/ffmpeg" ]]; then
+ if [[ -x "$OUT/ffmpeg" ]] && "$OUT/ffmpeg" -hide_banner -buildconf 2>/dev/null | grep -q -- --enable-libsoxr; then
echo "ffmpeg already present: $OUT/ffmpeg"
"$OUT/ffmpeg" -version | head -1
exit 0
@@ -52,6 +135,11 @@ elif [[ "$OS" == "Linux" ]]; then
cp -f "$BIN" "$OUT/ffmpeg"
rm -rf "$TMP_DIR" "$TMP_TXZ"
chmod +x "$OUT/ffmpeg"
+ if ! "$OUT/ffmpeg" -hide_banner -buildconf 2>/dev/null | grep -q -- --enable-libsoxr; then
+ echo "downloaded ffmpeg does not include libsoxr support" >&2
+ rm -f "$OUT/ffmpeg"
+ exit 1
+ fi
"$OUT/ffmpeg" -version | head -1
echo "saved to $OUT/ffmpeg"
elif [[ "$OS" == "MINGW"* || "$OS" == "MSYS"* || "$OS" == "CYGWIN"* ]]; then
diff --git a/src/main/env.ts b/src/main/env.ts
index 16b6677..fab1b7d 100644
--- a/src/main/env.ts
+++ b/src/main/env.ts
@@ -454,19 +454,22 @@ export async function detectTools(): Promise<void> {
state.python = { found: true, path: best.path, version: best.version }
}
+ // probe the bundled ffmpeg just like system candidates: a bundled binary
+ // that can't execute on this machine (e.g. a stale x86_64 build on an
+ // Apple Silicon Mac without Rosetta — exec fails with errno -86) must
+ // fall through to a system install instead of blowing up mid-job
+ const ffProbes: string[] = []
const bundled = bundledFfmpeg()
- if (bundled) {
- state.ffmpeg = { found: true, path: bundled }
- } else {
- for (const candidate of ffCandidates()) {
- if (!existsSync(candidate)) continue
- try {
- await runCapture(candidate, ['-version'])
- state.ffmpeg = { found: true, path: candidate }
- break
- } catch {
- continue
- }
+ if (bundled) ffProbes.push(bundled)
+ ffProbes.push(...ffCandidates())
+ for (const candidate of ffProbes) {
+ if (!existsSync(candidate)) continue
+ try {
+ await runCapture(candidate, ['-version'])
+ state.ffmpeg = { found: true, path: candidate }
+ break
+ } catch {
+ continue
}
}
diff --git a/src/main/pipeline.ts b/src/main/pipeline.ts
index 027bf7f..8ee8328 100644
--- a/src/main/pipeline.ts
+++ b/src/main/pipeline.ts
@@ -597,6 +597,19 @@ async function runSeparation(
}
}
+/* macOS errno -86 (EBADARCH, "Bad CPU type in executable"): a spawned tool
+ cannot run on this CPU — historically the x86_64 ffmpeg that older builds
+ bundled for Apple Silicon Macs without Rosetta. Say that, not "Unknown
+ system error -86" */
+function friendlySpawnError(err: NodeJS.ErrnoException): Error {
+ if (err.errno === -86 || /Unknown system error -86/.test(err.message ?? '')) {
+ return new Error(
+ "StemKit's built-in audio tools don't work on this Mac — update StemKit to the latest version"
+ )
+ }
+ return err
+}
+
function runProcess(
job: ActiveJob,
cmd: string,
@@ -625,7 +638,7 @@ function runProcess(
rl.on('line', (line) => opts.onLine?.(line))
}
- child.on('error', reject)
+ child.on('error', (err) => reject(friendlySpawnError(err)))
child.on('close', (code) => {
if (job.cancelled || !jobs.has(job.videoId)) return reject(new Error('cancelled'))
if (code === 0) return resolve()