Skip to content

armgit / peach

Public Imported from GitHub
0

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

feat(desktop): 从固定源码构建随包 untrunc

longmeidao5a00a7e5d781
diff --git a/scripts/build_untrunc_windows.sh b/scripts/build_untrunc_windows.sh
new file mode 100644
index 000000000..3cdab5132
--- /dev/null
+++ b/scripts/build_untrunc_windows.sh
@@ -0,0 +1,46 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+script_dir="$(cd "$(dirname "$0")" && pwd)"
+output="${1:?Usage: bash scripts/build_untrunc_windows.sh OUTPUT}"
+mkdir -p "$output"
+output="$(cd "$output" && pwd)"
+python3 "$script_dir/fetch_untrunc_sources.py" prepare "$output"
+
+{
+ x86_64-w64-mingw32-gcc --version
+ x86_64-w64-mingw32-g++ --version
+ make --version
+ nasm -v
+ python3 --version
+} > "$output/build-environment.txt"
+
+cd "$output/work/ffmpeg"
+./configure \
+ --target-os=mingw32 --arch=x86_64 --cross-prefix=x86_64-w64-mingw32- \
+ --pkg-config=pkg-config --disable-autodetect --disable-everything \
+ --disable-programs --disable-doc --disable-debug --disable-shared --enable-static \
+ --disable-network --disable-avdevice --disable-avfilter --disable-swscale --disable-swresample \
+ --enable-protocol=file --enable-demuxer=mov \
+ --enable-decoder=h264,hevc,mpeg4,aac,mp3,alac,amrnb,amrwb,pcm_s16le,pcm_s16be,pcm_s24le,pcm_s24be,mjpeg,prores \
+ --enable-parser=h264,hevc,aac,mpegaudio \
+ --extra-cflags=-O2
+make -j"$(nproc)"
+cp ffbuild/config.mak "$output/buildconf.txt"
+
+cd "$output/work/untrunc"
+revision="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["version"])' "$script_dir/untrunc-sources.json")"
+untrunc_cxxflags="-O3 -std=c++17 -D_FILE_OFFSET_BITS=64 -I$output/work/ffmpeg"
+untrunc_ldflags="-static -static-libgcc -static-libstdc++ -L$output/work/ffmpeg/libavformat -lavformat -L$output/work/ffmpeg/libavcodec -lavcodec -L$output/work/ffmpeg/libavutil -lavutil -lbcrypt -lws2_32 -lsecur32 -lm -s"
+make -j"$(nproc)" FF_VER=shared IS_RELEASE=1 EXE=untrunc.exe \
+ CXX=x86_64-w64-mingw32-g++ \
+ CXXFLAGS="$untrunc_cxxflags" \
+ VER="$revision" \
+ LDFLAGS="$untrunc_ldflags"
+printf '\nUNTRUNC_REVISION=%s\nUNTRUNC_CXXFLAGS=%s\nUNTRUNC_LDFLAGS=%s\n' \
+ "$revision" "$untrunc_cxxflags" "$untrunc_ldflags" >> "$output/buildconf.txt"
+mkdir -p "$output/untrunc"
+cp untrunc.exe "$output/untrunc/untrunc.exe"
+cp /usr/share/doc/g++-mingw-w64-x86-64-posix/copyright "$output/untrunc/LICENSE-GCC-runtime.txt"
+cp /usr/share/doc/mingw-w64-x86-64-dev/copyright "$output/untrunc/LICENSE-MinGW-w64.txt"
+python3 "$script_dir/fetch_untrunc_sources.py" finish "$output"
diff --git a/scripts/fetch_untrunc_sources.py b/scripts/fetch_untrunc_sources.py
new file mode 100644
index 000000000..94b654a0a
--- /dev/null
+++ b/scripts/fetch_untrunc_sources.py
@@ -0,0 +1,91 @@
+"""校验 untrunc 固定源码,整理 Windows 发行目录与完整对应源码包。"""
+from __future__ import annotations
+
+import argparse
+import json
+from pathlib import Path
+import shutil
+import zipfile
+
+from fetch_ffmpeg_sources import digest, prepare
+
+SCRIPT_DIR = Path(__file__).resolve().parent
+LOCK_PATH = SCRIPT_DIR / 'untrunc-sources.json'
+
+
+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']} != {'untrunc', 'ffmpeg'}:
+ raise ValueError('源码清单必须包含 untrunc 与 FFmpeg')
+ 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 finish(output: Path, lock: dict) -> dict:
+ bundle = output / 'untrunc'
+ work = output / 'work'
+ shutil.copyfile(work / 'untrunc/COPYING', bundle / 'LICENSE.txt')
+ shutil.copyfile(work / 'ffmpeg/COPYING.LGPLv2.1', bundle / 'LICENSE-FFmpeg-LGPLv2.1.txt')
+ shutil.copyfile(output / 'buildconf.txt', bundle / 'buildconf.txt')
+ (bundle / 'NOTICE.md').write_text(
+ '# untrunc Windows x64\n\n'
+ 'untrunc 由 anthwlock 与贡献者维护,按 GPL-2.0-or-later 分发。\n'
+ '静态链接 FFmpeg 8.1.3 的 LGPL-2.1-or-later 内置库;完整许可证见本目录 LICENSE 文件。\n'
+ 'FFmpeg 内部结构与 untrunc 绑定,构建使用固定 8.1 维护分支。\n'
+ '不包含 GUI,不使用外部编解码库;Windows 系统库由操作系统提供。\n'
+ '静态 C++ 与 MinGW-w64 运行时的版权和运行时例外见 LICENSE-GCC-runtime.txt 与 LICENSE-MinGW-w64.txt。\n'
+ '完整对应源码、构建脚本和说明见同版本 untrunc-corresponding-source.zip。\n',
+ encoding='utf-8',
+ )
+ archive = output / 'untrunc-corresponding-source.zip'
+ instructions = (
+ '# untrunc Windows x64 对应源码\n\n'
+ '包含未经修改的 untrunc、FFmpeg 固定源码归档、构建脚本与环境记录。\n'
+ 'Ubuntu 安装:sudo apt-get install build-essential gcc-mingw-w64-x86-64-posix '
+ 'g++-mingw-w64-x86-64-posix mingw-w64-x86-64-dev nasm pkg-config python3 ca-certificates xz-utils\n\n'
+ '执行:bash scripts/build_untrunc_windows.sh "$PWD/rebuilt"\n'
+ '离线重建:先建立 rebuilt/sources,将 sources/ 中两份归档复制进去,再执行构建命令。\n'
+ '产物为 rebuilt/untrunc 与 rebuilt/untrunc-corresponding-source.zip。\n'
+ '工具链由系统提供,版本见 build-environment.txt;FFmpeg 配置见 buildconf.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_untrunc_windows.sh', 'fetch_untrunc_sources.py',
+ 'fetch_ffmpeg_sources.py', 'untrunc-sources.json'):
+ package.write(SCRIPT_DIR / name, f'scripts/{name}')
+ package.writestr('README.md', instructions)
+ package.write(output / 'buildconf.txt', 'buildconf.txt')
+ package.write(output / 'build-environment.txt', 'build-environment.txt')
+ manifest = {
+ 'version': lock['version'], 'license': lock['license'],
+ 'files': {path.relative_to(bundle).as_posix(): digest(path) for path in sorted(bundle.rglob('*'))
+ if path.is_file() and path.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/untrunc-sources.json b/scripts/untrunc-sources.json
new file mode 100644
index 000000000..281680ae6
--- /dev/null
+++ b/scripts/untrunc-sources.json
@@ -0,0 +1,20 @@
+{
+ "version": "9d86ec9",
+ "license": "GPL-2.0-or-later",
+ "sources": [
+ {
+ "name": "untrunc",
+ "revision": "9d86ec9ef2ffed1bf8131abe80742c0574db52b6",
+ "url": "https://codeload.github.com/anthwlock/untrunc/tar.gz/9d86ec9ef2ffed1bf8131abe80742c0574db52b6",
+ "sha256": "4e046f36bcd0195aa80b59e15051cc06fa58c5262e1da62005c5d8593e76153c",
+ "archive": "untrunc-9d86ec9ef2ffed1bf8131abe80742c0574db52b6.tar.gz"
+ },
+ {
+ "name": "ffmpeg",
+ "revision": "n8.1.3",
+ "url": "https://ffmpeg.org/releases/ffmpeg-8.1.3.tar.xz",
+ "sha256": "7138d28c96d9d3e3af4ee3d8cad72741f8ffb40da90c1112235dea3ecd3178a3",
+ "archive": "ffmpeg-8.1.3.tar.xz"
+ }
+ ]
+}
diff --git a/src/peach/mp4recover.py b/src/peach/mp4recover.py
index 65f2f8d1d..302d1eb42 100644
--- a/src/peach/mp4recover.py
+++ b/src/peach/mp4recover.py
@@ -16,6 +16,7 @@ from __future__ import annotations
import os
import shutil
import subprocess
+import sys
from dataclasses import dataclass
from pathlib import Path, PureWindowsPath
@@ -44,11 +45,16 @@ class Recovery:
def untrunc_path(tools_root: Path) -> Path | None:
- """环境变量指定的、Peach 托管目录里的、PATH 上的,依次找。"""
+ """依次查环境变量、随包工具、数据根托管目录与 PATH。"""
configured = os.environ.get(UNTRUNC_ENV)
if configured and Path(configured).expanduser().is_file():
return Path(configured).expanduser().resolve()
- managed = Path(tools_root) / "untrunc" / f"untrunc{'.exe' if os.name == 'nt' else ''}"
+ name = f"untrunc{'.exe' if os.name == 'nt' else ''}"
+ if getattr(sys, 'frozen', False):
+ bundled = Path(sys.executable).resolve().parent / 'tools' / 'untrunc' / name
+ if bundled.is_file():
+ return bundled.resolve()
+ managed = Path(tools_root) / "untrunc" / name
if managed.is_file():
return managed.resolve()
found = shutil.which("untrunc")
diff --git a/tests/test_mp4recover.py b/tests/test_mp4recover.py
index aa1978208..00876f4c4 100644
--- a/tests/test_mp4recover.py
+++ b/tests/test_mp4recover.py
@@ -142,6 +142,45 @@ class ReplaceOriginalTests(unittest.TestCase):
class ToolLookupTests(unittest.TestCase):
+ def test_the_frozen_bundle_precedes_the_data_tools_and_path(self):
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory).resolve()
+ executable = root / 'Peach.exe'
+ bundled = root / 'tools/untrunc' / f"untrunc{'.exe' if os.name == 'nt' else ''}"
+ bundled.parent.mkdir(parents=True)
+ bundled.touch()
+ tools = root / 'data/tools'
+ managed = tools / 'untrunc' / bundled.name
+ managed.parent.mkdir(parents=True)
+ managed.touch()
+ with mock.patch.dict(os.environ, {mp4recover.UNTRUNC_ENV: ''}), \
+ mock.patch.object(mp4recover.sys, 'frozen', True, create=True), \
+ mock.patch.object(mp4recover.sys, 'executable', str(executable)), \
+ mock.patch.object(mp4recover.shutil, 'which', return_value=None) as which:
+ self.assertEqual(mp4recover.untrunc_path(tools), bundled)
+ which.assert_not_called()
+
+ def test_an_explicit_binary_precedes_the_frozen_bundle(self):
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory).resolve()
+ custom = root / 'custom-untrunc.exe'
+ custom.touch()
+ with mock.patch.dict(os.environ, {mp4recover.UNTRUNC_ENV: str(custom)}), \
+ mock.patch.object(mp4recover.sys, 'frozen', True, create=True):
+ self.assertEqual(mp4recover.untrunc_path(root / 'data/tools'), custom)
+
+ def test_a_missing_bundled_binary_uses_data_tools(self):
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory).resolve()
+ tools = root / 'data/tools'
+ managed = tools / 'untrunc' / f"untrunc{'.exe' if os.name == 'nt' else ''}"
+ managed.parent.mkdir(parents=True)
+ managed.touch()
+ with mock.patch.dict(os.environ, {mp4recover.UNTRUNC_ENV: ''}), \
+ mock.patch.object(mp4recover.sys, 'frozen', True, create=True), \
+ mock.patch.object(mp4recover.sys, 'executable', str(root / 'Peach.exe')):
+ self.assertEqual(mp4recover.untrunc_path(tools), managed)
+
def test_the_managed_copy_under_tools_is_found(self):
with tempfile.TemporaryDirectory() as directory:
tools = Path(directory)
diff --git a/tests/test_untrunc_build.py b/tests/test_untrunc_build.py
new file mode 100644
index 000000000..5b6c063c6
--- /dev/null
+++ b/tests/test_untrunc_build.py
@@ -0,0 +1,148 @@
+"""untrunc 固定源码与发行清单的完整性。"""
+import importlib.util
+import json
+import os
+from pathlib import Path
+import sys
+import subprocess
+import tempfile
+import unittest
+from unittest.mock import patch
+import zipfile
+
+
+ROOT = Path(__file__).resolve().parents[1]
+with patch.object(sys, 'path', [str(ROOT / 'scripts'), *sys.path]):
+ spec = importlib.util.spec_from_file_location('fetch_untrunc_sources', ROOT / 'scripts/fetch_untrunc_sources.py')
+ build = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(build)
+
+
+class UntruncSourceBuildTests(unittest.TestCase):
+ def test_sources_pin_the_compatible_ffmpeg_branch_and_untrunc_revision(self):
+ lock = build.load_sources()
+ sources = {source['name']: source for source in lock['sources']}
+ self.assertEqual(lock['license'], 'GPL-2.0-or-later')
+ self.assertEqual(sources['ffmpeg']['revision'], 'n8.1.3')
+ self.assertEqual(len(sources['untrunc']['revision']), 40)
+ self.assertEqual(lock['version'], sources['untrunc']['revision'][:7])
+
+ def test_source_lock_rejects_missing_dependencies_and_unsafe_archives(self):
+ lock = build.load_sources()
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory).resolve() / 'sources.json'
+ lock['sources'][0]['archive'] = '../untrunc.tar.gz'
+ path.write_text(json.dumps(lock), encoding='utf-8')
+ with self.assertRaises(ValueError):
+ build.load_sources(path)
+ lock['sources'] = lock['sources'][1:]
+ path.write_text(json.dumps(lock), encoding='utf-8')
+ with self.assertRaises(ValueError):
+ build.load_sources(path)
+
+ def fixture(self, output):
+ for name, payload in {
+ 'untrunc/untrunc.exe': b'MZ-untrunc-fixture',
+ 'work/untrunc/COPYING': b'untrunc-license',
+ 'work/ffmpeg/COPYING.LGPLv2.1': b'ffmpeg-license',
+ 'buildconf.txt': b'--disable-autodetect --disable-network',
+ 'build-environment.txt': b'cross compiler version',
+ 'sources/untrunc.tar.gz': b'untrunc-source-fixture',
+ 'sources/ffmpeg.tar.xz': b'ffmpeg-source-fixture',
+ }.items():
+ path = output / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_bytes(payload)
+ lock = {
+ 'version': 'abcdef0', 'license': 'GPL-2.0-or-later',
+ 'sources': [{'name': name, 'revision': revision, 'archive': archive,
+ 'url': f'https://example.test/{archive}',
+ 'sha256': build.digest(output / 'sources' / archive)}
+ for name, revision, archive in [('untrunc', 'abcdef0', 'untrunc.tar.gz'),
+ ('ffmpeg', 'n8.1.3', 'ffmpeg.tar.xz')]],
+ }
+ return lock
+
+ def test_manifest_hashes_all_distributed_files_and_corresponding_sources(self):
+ with tempfile.TemporaryDirectory() as directory:
+ output = Path(directory).resolve()
+ lock = self.fixture(output)
+ manifest = build.finish(output, lock)
+ self.assertEqual(manifest['version'], lock['version'])
+ bundle = output / 'untrunc'
+ self.assertEqual(set(manifest['files']), {path.name for path in bundle.iterdir()} - {'manifest.json'})
+ for name, digest in manifest['files'].items():
+ self.assertEqual(build.digest(bundle / name), digest)
+ source_archive = output / manifest['source_archive']['name']
+ self.assertEqual(build.digest(source_archive), manifest['source_archive']['sha256'])
+ with zipfile.ZipFile(source_archive) as archive:
+ for source in lock['sources']:
+ self.assertEqual(archive.read('sources/' + source['archive']),
+ (output / 'sources' / source['archive']).read_bytes())
+ self.assertTrue({'scripts/build_untrunc_windows.sh', 'scripts/fetch_ffmpeg_sources.py',
+ 'scripts/fetch_untrunc_sources.py', 'scripts/untrunc-sources.json',
+ 'README.md', 'buildconf.txt', 'build-environment.txt'} <= set(archive.namelist()))
+
+ def test_corrupt_source_archive_prevents_a_release_manifest(self):
+ with tempfile.TemporaryDirectory() as directory:
+ output = Path(directory).resolve()
+ lock = self.fixture(output)
+ (output / 'sources/untrunc.tar.gz').write_bytes(b'tampered')
+ with self.assertRaises(ValueError):
+ build.finish(output, lock)
+ self.assertFalse((output / 'untrunc/manifest.json').exists())
+
+
[email protected](all(os.environ.get(name) for name in (
+ 'PEACH_TEST_UNTRUNC', 'PEACH_TEST_FFMPEG', 'PEACH_TEST_FFPROBE',
+)), '构建验收需要显式提供三份原生工具')
+class UntruncNativeRecoveryTests(unittest.TestCase):
+ def test_native_distribution_has_matching_hashes_and_internal_ffmpeg_version(self):
+ executable = Path(os.environ['PEACH_TEST_UNTRUNC']).resolve()
+ bundle = executable.parent
+ manifest = json.loads((bundle / 'manifest.json').read_text(encoding='utf-8'))
+ self.assertEqual(manifest['license'], 'GPL-2.0-or-later')
+ self.assertEqual(set(manifest['files']), {path.name for path in bundle.iterdir()} - {'manifest.json'})
+ for name, digest in manifest['files'].items():
+ self.assertEqual(build.digest(bundle / name), digest)
+ archive = bundle.parent / manifest['source_archive']['name']
+ self.assertEqual(build.digest(archive), manifest['source_archive']['sha256'])
+ version = subprocess.run([str(executable), '-V'], check=True, capture_output=True,
+ text=True, timeout=15).stdout
+ self.assertIn(manifest['version'], version)
+ self.assertIn("ffmpeg '8.1.3'", version)
+
+ def test_a_synthetic_truncated_mp4_is_recovered_and_decodes(self):
+ from peach import mp4recover
+ from peach.mp4repair import _top_level
+
+ ffmpeg = Path(os.environ['PEACH_TEST_FFMPEG']).resolve()
+ ffprobe = Path(os.environ['PEACH_TEST_FFPROBE']).resolve()
+ untrunc = Path(os.environ['PEACH_TEST_UNTRUNC']).resolve()
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory).resolve()
+ reference = root / 'reference.mp4'
+ subprocess.run([
+ str(ffmpeg), '-hide_banner', '-v', 'error', '-f', 'lavfi',
+ '-i', 'testsrc2=size=128x96:rate=10', '-f', 'lavfi',
+ '-i', 'sine=frequency=440:sample_rate=48000', '-t', '2',
+ '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-c:a', 'aac', str(reference),
+ ], check=True, capture_output=True, timeout=60)
+ with reference.open('rb') as stream:
+ boxes = list(_top_level(stream))
+ self.assertEqual(boxes[-1][0], b'moov')
+ broken = root / 'broken.mp4'
+ broken.write_bytes(reference.read_bytes()[:boxes[-1][1]])
+ self.assertTrue(mp4recover.missing_index(broken))
+ size = broken.stat().st_size
+ result = mp4recover.recover(broken, [reference], untrunc=untrunc,
+ ffmpeg=ffmpeg, ffprobe=ffprobe, work=root / 'recovered')
+ self.assertTrue(result.path.is_file())
+ self.assertFalse(mp4recover.missing_index(result.path))
+ self.assertAlmostEqual(result.duration, 2, delta=.2)
+ self.assertGreater(result.coverage, .95)
+ self.assertEqual(broken.stat().st_size, size)
+
+
+if __name__ == '__main__':
+ unittest.main()