Skip to content

The swiss army knife of lossless video/audio editing

Replace download-ffmpeg shell scripts with one TypeScript script

Claude8c5a2bad1eae
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index be5607f1..38abfb06 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -166,7 +166,7 @@ Use the newest stable release branch that all platforms can get (BtbN may lag be
1. macOS: make a new [ffmpeg-build-script](https://github.com/mifi/ffmpeg-build-script) release (see its README).
2. Windows and Linux: make a new [ffmpeg-builds](https://github.com/mifi/ffmpeg-builds) release (see its README).
-3. Update the `download-ffmpeg-*` scripts in [package.json](./package.json).
+3. Update the versions in [script/downloadFfmpeg.ts](./script/downloadFfmpeg.ts).
4. Read FFmpeg's `Changelog` and `doc/APIchanges` (and the release notes) since the previous version for changes that may affect LosslessCut, e.g. removed or renamed CLI options, changed muxer/demuxer defaults, seeking behaviour (`-ss`, `-avoid_negative_ts`), `ffprobe` JSON output, and codec/format support.
5. Test a CI build on each platform: cutting (keyframe and normal), merging, smart cut, capture frame, waveform/thumbnails and FFmpeg-assisted playback.
diff --git a/package.json b/package.json
index 002eae3d..cf736e2e 100644
--- a/package.json
+++ b/package.json
@@ -11,11 +11,11 @@
"clean": "rimraf dist out common-ts-dist main-ts-dist build-resources icon-build",
"start": "electron-vite preview",
"dev": "electron-vite dev -w",
- "download-ffmpeg-darwin-x64": "mkdirp ffmpeg/darwin-x64 && cd ffmpeg/darwin-x64 && wget https://github.com/mifi/ffmpeg-build-script/releases/download/8.1.3-1/ffmpeg-macos-X64 -O ffmpeg && wget https://github.com/mifi/ffmpeg-build-script/releases/download/8.1.3-1/ffprobe-macos-X64 -O ffprobe && chmod +x ffmpeg && chmod +x ffprobe",
- "download-ffmpeg-darwin-arm64": "mkdirp ffmpeg/darwin-arm64 && cd ffmpeg/darwin-arm64 && wget https://github.com/mifi/ffmpeg-build-script/releases/download/8.1.3-1/ffmpeg-macos-ARM64 -O ffmpeg && wget https://github.com/mifi/ffmpeg-build-script/releases/download/8.1.3-1/ffprobe-macos-ARM64 -O ffprobe && chmod +x ffmpeg && chmod +x ffprobe",
- "download-ffmpeg-linux-x64": "mkdirp ffmpeg/linux-x64 && cd ffmpeg/linux-x64 && wget https://github.com/mifi/ffmpeg-builds/releases/download/8.1-1/ffmpeg-n8.1-latest-linux64-gpl-shared-8.1.tar.xz -O ffmpeg-ffprobe.xz && tar -xv -f ffmpeg-ffprobe.xz && mv ffmpeg-n8.1-latest-linux64-gpl-shared-8.1 extracted && mkdirp lib && mv extracted/bin/ffmpeg extracted/bin/ffprobe extracted/lib/lib*.so* lib",
- "download-ffmpeg-win32-x64": "mkdirp ffmpeg/win32-x64 && cd ffmpeg/win32-x64 && npx download-cli https://github.com/mifi/ffmpeg-builds/releases/download/8.1-1/ffmpeg-n8.1-latest-win64-gpl-shared-8.1.zip --out . --filename ffmpeg-ffprobe.zip && 7z x ffmpeg-ffprobe.zip && mkdirp lib && cd ffmpeg-n8.1-latest-win64-gpl-shared-8.1/bin && npx shx mv ffmpeg.exe ffprobe.exe *.dll ../../lib",
- "download-ffmpeg-win32-arm64": "mkdirp ffmpeg/win32-arm64 && cd ffmpeg/win32-arm64 && npx download-cli https://github.com/mifi/ffmpeg-builds/releases/download/8.1-1/ffmpeg-n8.1-latest-winarm64-gpl-shared-8.1.zip --out . --filename ffmpeg-ffprobe.zip && 7z x ffmpeg-ffprobe.zip && mkdirp lib && cd ffmpeg-n8.1-latest-winarm64-gpl-shared-8.1/bin && npx shx mv ffmpeg.exe ffprobe.exe *.dll ../../lib",
+ "download-ffmpeg-darwin-x64": "node script/downloadFfmpeg.ts darwin-x64",
+ "download-ffmpeg-darwin-arm64": "node script/downloadFfmpeg.ts darwin-arm64",
+ "download-ffmpeg-linux-x64": "node script/downloadFfmpeg.ts linux-x64",
+ "download-ffmpeg-win32-x64": "node script/downloadFfmpeg.ts win32-x64",
+ "download-ffmpeg-win32-arm64": "node script/downloadFfmpeg.ts win32-arm64",
"build": "yarn generate-icon && electron-vite build",
"tsc": "tsc --build",
"test": "vitest",
diff --git a/script/downloadFfmpeg.ts b/script/downloadFfmpeg.ts
new file mode 100644
index 00000000..f66ba4f2
--- /dev/null
+++ b/script/downloadFfmpeg.ts
@@ -0,0 +1,71 @@
+// Downloads the bundled ffmpeg/ffprobe for a platform into ffmpeg/<platform>-<arch>, see CONTRIBUTING.md
+// Usage: node script/downloadFfmpeg.ts darwin-x64|darwin-arm64|linux-x64|win32-x64|win32-arm64
+import assert from 'node:assert';
+import { createWriteStream } from 'node:fs';
+import { chmod, mkdir, readdir, rename, rm } from 'node:fs/promises';
+import { basename, join } from 'node:path';
+import { Readable } from 'node:stream';
+import { pipeline } from 'node:stream/promises';
+import { execa } from 'execa';
+
+// Bump these when upgrading FFmpeg
+const macRelease = '8.1.3-1'; // https://github.com/mifi/ffmpeg-build-script/releases
+const windowsLinuxRelease = '8.1-1'; // https://github.com/mifi/ffmpeg-builds/releases (mirrors BtbN's n<branch>-latest builds)
+
+const target = process.argv[2];
+const btbnTargets: Record<string, string> = { 'linux-x64': 'linux64', 'win32-x64': 'win64', 'win32-arm64': 'winarm64' };
+const macArchs: Record<string, string> = { 'darwin-x64': 'X64', 'darwin-arm64': 'ARM64' };
+assert(target != null && (target in btbnTargets || target in macArchs), `Usage: downloadFfmpeg.ts ${[...Object.keys(macArchs), ...Object.keys(btbnTargets)].join('|')}`);
+
+const dir = join('ffmpeg', target);
+
+async function download(url: string, path: string) {
+ console.log('Downloading', url);
+ const response = await fetch(url);
+ assert(response.ok && response.body != null, `Failed to download ${url}: ${response.status}`);
+ await pipeline(Readable.fromWeb(response.body), createWriteStream(path));
+}
+
+await rm(dir, { recursive: true, force: true });
+await mkdir(dir, { recursive: true });
+
+let binDir: string;
+const macArch = macArchs[target];
+if (macArch != null) {
+ binDir = dir;
+ for (const name of ['ffmpeg', 'ffprobe']) {
+ const path = join(dir, name);
+ await download(`https://github.com/mifi/ffmpeg-build-script/releases/download/${macRelease}/${name}-macos-${macArch}`, path);
+ await chmod(path, 0o755);
+ }
+} else {
+ // the electron-builder config bundles everything in `lib`
+ binDir = join(dir, 'lib');
+ const branch = windowsLinuxRelease.split('-')[0];
+ const isWindows = target.startsWith('win32');
+ const name = `ffmpeg-n${branch}-latest-${btbnTargets[target]}-gpl-shared-${branch}`;
+ const archive = join(dir, `${name}${isWindows ? '.zip' : '.tar.xz'}`);
+ await download(`https://github.com/mifi/ffmpeg-builds/releases/download/${windowsLinuxRelease}/${name}${isWindows ? '.zip' : '.tar.xz'}`, archive);
+
+ // 7z, because tar on Windows may be GNU tar (from Git), which doesn't support zip
+ await (isWindows ? execa('7z', ['x', archive, `-o${dir}`], { stdout: 'ignore' }) : execa('tar', ['-xf', archive, '-C', dir]));
+
+ await mkdir(binDir);
+ const extractedBinDir = join(dir, name, 'bin');
+ const extractedLibDir = join(dir, name, 'lib');
+ // Windows has the dlls next to the exes, Linux has the .so files in lib
+ const files = isWindows
+ ? (await readdir(extractedBinDir)).filter((file) => /^(ffmpeg\.exe|ffprobe\.exe|.*\.dll)$/.test(file)).map((file) => join(extractedBinDir, file))
+ : [join(extractedBinDir, 'ffmpeg'), join(extractedBinDir, 'ffprobe'), ...(await readdir(extractedLibDir)).filter((file) => /^lib.*\.so/.test(file)).map((file) => join(extractedLibDir, file))];
+ for (const file of files) await rename(file, join(binDir, basename(file)));
+ await rm(join(dir, name), { recursive: true });
+ await rm(archive);
+}
+
+// Smoke test, if the binaries can run on this machine
+if (target === `${process.platform}-${process.arch}`) {
+ const exe = join(binDir, process.platform === 'win32' ? 'ffmpeg.exe' : 'ffmpeg');
+ const { stdout } = await execa(exe, ['-hide_banner', '-version'], { env: { LD_LIBRARY_PATH: binDir } });
+ console.log(stdout.split('\n')[0]);
+}
+console.log('Done', binDir);