Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

build(packaging): 从固定源码构建 Windows FFmpeg

longmeidao0a65b794749f
diff --git a/scripts/build_ffmpeg_windows.sh b/scripts/build_ffmpeg_windows.sh
new file mode 100644
index 000000000..b7df4773b
--- /dev/null
+++ b/scripts/build_ffmpeg_windows.sh
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+# 在 Linux 使用 MinGW-w64 构建 Windows x64 FFmpeg 与完整对应源码包。
+set -euo pipefail
+script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
+output="$(realpath -m -- "${1:?用法: build_ffmpeg_windows.sh <输出目录>}")"
+for tool in python3 make nasm pkg-config clang x86_64-w64-mingw32-gcc x86_64-w64-mingw32-strip; do
+ command -v "$tool" >/dev/null || { echo "缺少构建工具: $tool" >&2; exit 1; }
+done
+python3 "$script_dir/fetch_ffmpeg_sources.py" prepare "$output"
+work="$output/work"
+prefix="$work/prefix"
+{
+ x86_64-w64-mingw32-gcc --version
+ nasm -v
+ clang --version
+ make --version
+ python3 --version
+ uname -a
+} > "$output/build-environment.txt"
+export PKG_CONFIG_PATH=
+export PKG_CONFIG_LIBDIR="$prefix/lib/pkgconfig"
+mkdir -p "$prefix" "$output/ffmpeg/bin"
+cd "$work/x264"
+./configure --host=x86_64-w64-mingw32 --cross-prefix=x86_64-w64-mingw32- \
+ --prefix="$prefix" --enable-static --disable-cli --disable-opencl \
+ --disable-avs --disable-swscale --disable-lavf --disable-ffms --disable-gpac --disable-lsmash \
+ --extra-ldflags=-static
+make -j"$(nproc)"
+make install
+make -C "$work/nv-codec-headers" PREFIX="$prefix" install
+cd "$work/ffmpeg"
+./configure --prefix="$output/ffmpeg" --target-os=mingw32 --arch=x86_64 \
+ --cross-prefix=x86_64-w64-mingw32- --enable-cross-compile \
+ --disable-autodetect --disable-shared --enable-static --disable-debug \
+ --disable-doc --disable-ffplay --disable-pthreads --enable-w32threads \
+ --enable-gpl --enable-version3 --disable-nonfree --enable-libx264 \
+ --enable-cuda-llvm \
+ --enable-ffnvcodec --enable-cuda --enable-cuvid --enable-nvenc --enable-nvdec \
+ --enable-d3d11va --enable-dxva2 --enable-schannel \
+ --extra-cflags="-I$prefix/include" --extra-ldflags="-L$prefix/lib -static" \
+ --pkg-config=pkg-config --pkg-config-flags=--static
+make -j"$(nproc)"
+cp ffmpeg.exe ffprobe.exe "$output/ffmpeg/bin/"
+x86_64-w64-mingw32-strip "$output/ffmpeg/bin/ffmpeg.exe" "$output/ffmpeg/bin/ffprobe.exe"
+cp /usr/share/doc/mingw-w64-common/copyright "$output/ffmpeg/LICENSE-MinGW-w64.txt"
+cp /usr/share/doc/gcc-mingw-w64-base/copyright "$output/ffmpeg/LICENSE-GCC-runtime.txt"
+cp ffbuild/config.mak "$output/buildconf.mak"
+python3 "$script_dir/fetch_ffmpeg_sources.py" finish "$output"
diff --git a/scripts/fetch_ffmpeg_sources.py b/scripts/fetch_ffmpeg_sources.py
new file mode 100644
index 000000000..494979348
--- /dev/null
+++ b/scripts/fetch_ffmpeg_sources.py
@@ -0,0 +1,138 @@
+"""校验固定源码,生成 FFmpeg Windows 发行目录与完整对应源码包。"""
+from __future__ import annotations
+
+import argparse
+import hashlib
+import json
+from pathlib import Path
+import shutil
+import tarfile
+import urllib.request
+import zipfile
+
+SCRIPT_DIR = Path(__file__).resolve().parent
+LOCK_PATH = SCRIPT_DIR / "ffmpeg-sources.json"
+
+
+def digest(path: Path) -> str:
+ with path.open("rb") as stream:
+ return hashlib.file_digest(stream, "sha256").hexdigest()
+
+
+def load_sources(path: Path = LOCK_PATH) -> dict:
+ lock = json.loads(path.read_text(encoding="utf-8"))
+ if {source["name"] for source in lock["sources"]} != {"ffmpeg", "x264", "nv-codec-headers"}:
+ raise ValueError("源码清单必须包含全部三个依赖")
+ for source in lock["sources"]:
+ if len(source["sha256"]) != 64 or any(c not in "0123456789abcdef" for c in source["sha256"]):
+ raise ValueError(f"无效源码 SHA256: {source['name']}")
+ if not source["url"].startswith("https://") or Path(source["archive"]).name != source["archive"]:
+ raise ValueError("源码必须使用 HTTPS 与单个归档文件名")
+ return lock
+
+
+def prepare(output: Path, lock: dict) -> None:
+ sources = output / "sources"
+ work = output / "work"
+ sources.mkdir(parents=True, exist_ok=True)
+ work.mkdir(parents=True, exist_ok=True)
+ for source in lock["sources"]:
+ archive = sources / source["archive"]
+ if not archive.exists():
+ temporary = archive.with_suffix(archive.suffix + ".part")
+ with urllib.request.urlopen(source["url"], timeout=60) as response, temporary.open("wb") as stream:
+ shutil.copyfileobj(response, stream)
+ if digest(temporary) != source["sha256"]:
+ raise ValueError(f"源码 SHA256 不匹配: {source['name']}")
+ temporary.replace(archive)
+ if digest(archive) != source["sha256"]:
+ raise ValueError(f"源码 SHA256 不匹配: {source['name']}")
+ destination = work / source["name"]
+ if destination.exists():
+ raise FileExistsError(f"构建目录已存在,请使用新的输出目录: {destination}")
+ with tarfile.open(archive) as package:
+ members = package.getmembers()
+ roots = {m.name.split("/", 1)[0] for m in members}
+ if len(roots) != 1 or next(iter(roots)) in ("", ".", ".."):
+ raise ValueError(f"源码归档根目录无效: {source['name']}")
+ package.extractall(work, filter="data")
+ (work / next(iter(roots))).rename(destination)
+
+
+def finish(output: Path, lock: dict) -> dict:
+ bundle = output / "ffmpeg"
+ work = output / "work"
+ shutil.copyfile(work / "ffmpeg/COPYING.GPLv3", bundle / "LICENSE.txt")
+ for source, license_name in (("ffmpeg", "COPYING.LGPLv2.1"), ("ffmpeg", "COPYING.LGPLv3"),
+ ("x264", "COPYING")):
+ shutil.copyfile(work / source / license_name, bundle / f"LICENSE-{source}-{license_name}.txt")
+ header_notices = []
+ for header in sorted((work / "nv-codec-headers/include/ffnvcodec").glob("*.h")):
+ text = header.read_text(encoding="utf-8")
+ header_notices.append(f"{header.name}\n{text.split('*/', 1)[0]}*/\n")
+ if not header_notices:
+ raise ValueError("未取得 NVIDIA 头文件许可证")
+ (bundle / "LICENSE-nv-codec-headers.txt").write_text("\n".join(header_notices), encoding="utf-8")
+ notice = (
+ "# FFmpeg Windows x64\n\n"
+ "FFmpeg 9.0.2 与 libx264 按 GPL-3.0-or-later 分发。\n"
+ "版权与完整许可证见本目录 LICENSE 文件及对应源码归档。\n"
+ "编解码使用 FFmpeg 内置实现与静态 libx264;NVIDIA 接口使用 nv-codec-headers。\n"
+ "Windows 系统库由操作系统提供;NVIDIA 驱动由用户环境提供。\n"
+ "支持 NVENC、CUVID、NVDEC、D3D11VA、DXVA2 与 Schannel HTTPS。\n"
+ "包含 CUDA LLVM 与 scale_cuda;CUDA 解码和 NVENC 可用性取决于本机驱动。\n"
+ "完整对应源码、固定 revision、SHA256 与构建步骤见同版本 ffmpeg-corresponding-source.zip。\n"
+ "本目录的 manifest.json 记录二进制、许可证、构建配置及源码归档的 SHA256。\n"
+ )
+ (bundle / "NOTICE.md").write_text(notice, encoding="utf-8")
+ shutil.copyfile(output / "buildconf.mak", bundle / "buildconf.txt")
+ archive = output / "ffmpeg-corresponding-source.zip"
+ instructions = (
+ "# Windows x64 FFmpeg 对应源码\n\n"
+ "包含三份未经修改的上游源码归档和构建脚本。\n"
+ "Linux/Ubuntu 安装:sudo apt-get install build-essential gcc-mingw-w64-x86-64-posix "
+ "mingw-w64-x86-64-dev nasm pkg-config python3 clang ca-certificates xz-utils\n\n"
+ '在此目录执行:bash scripts/build_ffmpeg_windows.sh "$PWD/rebuilt"\n'
+ "离线重建:先建立 rebuilt/sources,再将 sources/ 内全部归档复制进去,再执行上述命令。\n"
+ "产物为 rebuilt/ffmpeg 与 rebuilt/ffmpeg-corresponding-source.zip。\n"
+ "工具链属于系统编译环境;各版本记录在 build-environment.txt。\n"
+ )
+ with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as package:
+ for source in lock["sources"]:
+ path = output / "sources" / source["archive"]
+ if digest(path) != source["sha256"]:
+ raise ValueError(f"源码 SHA256 不匹配: {source['name']}")
+ package.write(path, f"sources/{path.name}")
+ for name in ("build_ffmpeg_windows.sh", "fetch_ffmpeg_sources.py", "ffmpeg-sources.json"):
+ package.write(SCRIPT_DIR / name, f"scripts/{name}")
+ for license_file in sorted(bundle.glob("LICENSE*")):
+ package.write(license_file, f"licenses/{license_file.name}")
+ package.writestr("README.md", instructions)
+ package.write(output / "buildconf.mak", "buildconf.txt")
+ if (output / "build-environment.txt").exists():
+ package.write(output / "build-environment.txt", "build-environment.txt")
+ manifest = {
+ "version": lock["version"], "license": lock["license"],
+ "files": {p.relative_to(bundle).as_posix(): digest(p) for p in sorted(bundle.rglob("*"))
+ if p.is_file() and p.name != "manifest.json"},
+ "sources": lock["sources"],
+ "source_archive": {"name": archive.name, "sha256": digest(archive)},
+ }
+ (bundle / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
+ return manifest
+
+
+def main() -> None:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("action", choices=("prepare", "finish"))
+ parser.add_argument("output", type=Path)
+ args = parser.parse_args()
+ lock = load_sources()
+ if args.action == "prepare":
+ prepare(args.output.resolve(), lock)
+ else:
+ finish(args.output.resolve(), lock)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/ffmpeg-sources.json b/scripts/ffmpeg-sources.json
new file mode 100644
index 000000000..22c57de33
--- /dev/null
+++ b/scripts/ffmpeg-sources.json
@@ -0,0 +1,27 @@
+{
+ "version": "9.0.2",
+ "license": "GPL-3.0-or-later",
+ "sources": [
+ {
+ "name": "ffmpeg",
+ "revision": "n9.0.2",
+ "url": "https://ffmpeg.org/releases/ffmpeg-9.0.2.tar.xz",
+ "sha256": "8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e",
+ "archive": "ffmpeg-9.0.2.tar.xz"
+ },
+ {
+ "name": "x264",
+ "revision": "b35605ace3ddf7c1a5d67a2eb553f034aef41d55",
+ "url": "https://code.videolan.org/videolan/x264/-/archive/b35605ace3ddf7c1a5d67a2eb553f034aef41d55/x264-b35605ace3ddf7c1a5d67a2eb553f034aef41d55.tar.gz",
+ "sha256": "cd71a7515b0e9a012e1ac9b1f8415bebcaf6fc97d4db32286642ac4c0fbe24f9",
+ "archive": "x264-b35605ace3ddf7c1a5d67a2eb553f034aef41d55.tar.gz"
+ },
+ {
+ "name": "nv-codec-headers",
+ "revision": "n13.1.15.0",
+ "url": "https://github.com/FFmpeg/nv-codec-headers/archive/refs/tags/n13.1.15.0.tar.gz",
+ "sha256": "2255bc74d038b95aa4be30f5f66322c2176acbdb90ada1851db6993536fbeaf7",
+ "archive": "nv-codec-headers-n13.1.15.0.tar.gz"
+ }
+ ]
+}
diff --git a/tests/test_ffmpeg_build.py b/tests/test_ffmpeg_build.py
new file mode 100644
index 000000000..9746b7cae
--- /dev/null
+++ b/tests/test_ffmpeg_build.py
@@ -0,0 +1,92 @@
+"""FFmpeg 对应源码归档与发行清单的行为验证。"""
+from __future__ import annotations
+
+import importlib.util
+import io
+import json
+from pathlib import Path
+import tarfile
+import tempfile
+import unittest
+import zipfile
+
+
+SCRIPT = Path(__file__).resolve().parents[1] / "scripts/fetch_ffmpeg_sources.py"
+SPEC = importlib.util.spec_from_file_location("ffmpeg_source_build", SCRIPT)
+assert SPEC and SPEC.loader
+builder = importlib.util.module_from_spec(SPEC)
+SPEC.loader.exec_module(builder)
+
+
+class FFmpegSourceBuildTests(unittest.TestCase):
+ def test_source_lock_pins_all_linked_external_dependencies(self):
+ lock = builder.load_sources()
+ self.assertEqual(lock["version"], "9.0.2")
+ self.assertEqual(lock["license"], "GPL-3.0-or-later")
+ self.assertEqual(len(lock["sources"]), 3)
+
+ def make_sources(self, output):
+ lock = builder.load_sources()
+ directory = output / "sources"
+ directory.mkdir()
+ for source in lock["sources"]:
+ archive = directory / source["archive"]
+ with tarfile.open(archive, "w:gz") as package:
+ data = b"upstream source and license\n"
+ entry = tarfile.TarInfo(f"{source['name']}-revision/README")
+ entry.size = len(data)
+ package.addfile(entry, io.BytesIO(data))
+ source["sha256"] = builder.digest(archive)
+ return lock
+
+ def test_prepare_verifies_archives_before_extracting(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ output = Path(temporary).resolve()
+ lock = self.make_sources(output)
+ builder.prepare(output, lock)
+ for source in lock["sources"]:
+ self.assertTrue((output / "work" / source["name"] / "README").is_file())
+ with self.assertRaises(FileExistsError):
+ builder.prepare(output, lock)
+
+ def test_prepare_rejects_source_archive_tampering(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ output = Path(temporary).resolve()
+ lock = self.make_sources(output)
+ (output / "sources" / lock["sources"][0]["archive"]).write_bytes(b"corrupt")
+ with self.assertRaisesRegex(ValueError, "SHA256"):
+ builder.prepare(output, lock)
+ self.assertFalse((output / "work/ffmpeg").exists())
+
+ def test_finish_includes_sources_scripts_licenses_and_matching_hashes(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ output = Path(temporary).resolve()
+ lock = self.make_sources(output)
+ builder.prepare(output, lock)
+ for path in ("ffmpeg/COPYING.GPLv3", "ffmpeg/COPYING.LGPLv2.1",
+ "ffmpeg/COPYING.LGPLv3", "x264/COPYING"):
+ (output / "work" / path).write_text("license", encoding="utf-8")
+ headers = output / "work/nv-codec-headers/include/ffnvcodec"
+ headers.mkdir(parents=True)
+ (headers / "test.h").write_text("/* MIT license */\ncode", encoding="utf-8")
+ (output / "ffmpeg/bin").mkdir(parents=True)
+ for name in ("ffmpeg", "ffprobe"):
+ (output / f"ffmpeg/bin/{name}.exe").write_bytes(b"MZ fixture")
+ (output / "buildconf.mak").write_text("CONFIG_GPL=yes\n", encoding="utf-8")
+ manifest = builder.finish(output, lock)
+ source_zip = output / manifest["source_archive"]["name"]
+ self.assertEqual(builder.digest(source_zip), manifest["source_archive"]["sha256"])
+ for path, expected in manifest["files"].items():
+ self.assertEqual(builder.digest(output / "ffmpeg" / path), expected)
+ with zipfile.ZipFile(source_zip) as package:
+ names = set(package.namelist())
+ self.assertTrue({f"sources/{s['archive']}" for s in lock["sources"]} <= names)
+ self.assertTrue({"scripts/build_ffmpeg_windows.sh", "scripts/fetch_ffmpeg_sources.py",
+ "scripts/ffmpeg-sources.json", "README.md", "buildconf.txt"} <= names)
+ self.assertFalse(any("manifest.json" in name for name in names))
+ stored = json.loads((output / "ffmpeg/manifest.json").read_text(encoding="utf-8"))
+ self.assertEqual(stored, manifest)
+
+
+if __name__ == "__main__":
+ unittest.main()