Peach: Local-first video metadata scraper and media manager.
perf(preview): 同部九宫格复用解码探测
longmeidaoc6db045a6a4e
diff --git a/src/peach/frame_capture.py b/src/peach/frame_capture.pyindex a7c08b61f..c3ef80417 100644--- a/src/peach/frame_capture.py+++ b/src/peach/frame_capture.py@@ -87,13 +87,17 @@ def capture_frame(ffmpeg: str, path: str, timestamp: float, destination: Path, *def capture_with_retry(ffmpeg: str, path: str, timestamp: float, destination: Path,*, width: int = 480, timeout: float = 45,- diagnostics: dict | None = None) -> bool:- """抽一帧;只在错误明说色彩元数据不合法时,带 bt709 声明重试一次。"""+ diagnostics: dict | None = None,+ decode_options: tuple[str, ...] | None = None) -> bool:+ """抽一帧;坏色彩元数据可重试一次,同部多帧可复用 decode_options。++ None 表示现场探测,空元组表示软件解码;硬件失败仍在本帧内回退。+ """from .smb_media import input_urlpath = input_url(path)from .hardware_transcode import frame_decode_optionsdeadline = time.monotonic() + timeout- hardware = frame_decode_options(ffmpeg, path)+ hardware = frame_decode_options(ffmpeg, path) if decode_options is None else decode_optionsattempts, detail, ok = [], {}, Falsefor options in ((hardware, ()) if hardware else ((),)):remaining = deadline - time.monotonic()diff --git a/src/peach/library_processing.py b/src/peach/library_processing.pyindex c6632d059..621ac688b 100644--- a/src/peach/library_processing.py+++ b/src/peach/library_processing.py@@ -2294,6 +2294,8 @@ def _generate_preview(config, row, database, resolver, *, progress):digest = hashlib.sha1(str(row['path']).encode('utf-8')).hexdigest()[:16]destination = root / 'local' / digest[:2] / (digest + '.jpg')if not artifact_rules.current(destination) or not measure_image_file(destination):+ from .hardware_transcode import frame_decode_options+ decode_options = frame_decode_options(str(choice.path), str(video))with tempfile.TemporaryDirectory(prefix='peach-preview-') as temporary:frames = Path(temporary)for index in range(9):@@ -2304,7 +2306,7 @@ def _generate_preview(config, row, database, resolver, *, progress):diagnostic = {}if not frame_capture.capture_with_retry(str(choice.path), str(video), timestamp, frames / f's{index:02d}.jpg',- diagnostics=diagnostic, timeout=remaining(45)):+ diagnostics=diagnostic, timeout=remaining(45), decode_options=decode_options):for attempt in diagnostic.get('attempts', []):attempt['stderr'] = attempt.get('stderr', '').replace(str(video), '<media>')[-4096:]diagnostic['stderr'] = diagnostic.get('stderr', '').replace(str(video), '<media>')[-4096:]diff --git a/tests/test_frame_capture.py b/tests/test_frame_capture.pyindex c7d641e13..ccb242d36 100644--- a/tests/test_frame_capture.py+++ b/tests/test_frame_capture.py@@ -34,13 +34,20 @@ class CaptureFrameTests(unittest.TestCase):return subprocess.CompletedProcess(command, 1, stderr=b'unsupported hardware decoder')Image.new('RGB', (480, 270)).save(command[-1], format='JPEG')return subprocess.CompletedProcess(command, 0, stderr=b'')- detail = {}- with patch('peach.hardware_transcode.frame_decode_options', return_value=('-hwaccel', 'videotoolbox')), \- patch.object(frame_capture.subprocess, 'run', side_effect=run):- self.assertTrue(frame_capture.capture_with_retry('ffmpeg', 'sample.mp4', 1, destination, diagnostics=detail))- self.assertEqual(len(calls), 2)- self.assertNotIn('-hwaccel', calls[1])- self.assertEqual([attempt['hardware_decode'] for attempt in detail['attempts']], [True, False])+ for options in (None, ('-hwaccel', 'videotoolbox'), ()):+ calls.clear()+ detail = {}+ with self.subTest(decode_options=options), \+ patch('peach.hardware_transcode.frame_decode_options', return_value=('-hwaccel', 'videotoolbox')) as probe, \+ patch.object(frame_capture.subprocess, 'run', side_effect=run):+ self.assertTrue(frame_capture.capture_with_retry(+ 'ffmpeg', 'sample.mp4', 1, destination, diagnostics=detail, decode_options=options))+ expected = [False] if options == () else [True, False]+ self.assertEqual(len(calls), len(expected))+ self.assertNotIn('-hwaccel', calls[-1])+ self.assertEqual([attempt['hardware_decode'] for attempt in detail['attempts']], expected)+ if options is not None:+ probe.assert_not_called()def setUp(self):self.root = Path(tempfile.mkdtemp(prefix="frame-capture-")).resolve()@@ -154,7 +161,7 @@ class CaptureFrameTests(unittest.TestCase):(b"", 1),):self.commands = []- with self._run(produce=False, stderr=stderr):+ with self._run(produce=False, stderr=stderr), patch('peach.error_reporting.subprocess_failure'):ok = frame_capture.capture_with_retry("ffmpeg", "movie.mp4", 1.0,self.root / "retry.jpg")self.assertFalse(ok)@@ -186,10 +193,12 @@ class TileFramesTests(unittest.TestCase):def test_a_tile_that_did_not_land_is_not_reported_as_made(self):with patch.object(frame_capture.subprocess, "run",- lambda command, **_kwargs: _Completed()):+ lambda command, **_kwargs: _Completed()), \+ patch('peach.error_reporting.subprocess_failure') as report:made = frame_capture.tile_frames("ffmpeg", str(self.root / "s%03d.jpg"), 3, 3,self.root / "out" / "000.jpg")self.assertFalse(made)+ self.assertEqual(report.call_args.kwargs['reason'], 'tile_unavailable')class FFmpegCaptureIntegrationTests(unittest.TestCase):diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.pyindex ea0943b66..07bf5eca7 100644--- a/tests/test_metadata_library.py+++ b/tests/test_metadata_library.py@@ -3314,6 +3314,38 @@ class LibraryPreviewCompletionTests(unittest.TestCase):self.assertEqual(second['status'], 'complete')capture.assert_not_called()+ def test_one_video_reuses_decode_probe_for_all_nine_frames(self):+ from peach import frame_capture, hardware_transcode+ from peach.media_io import file_stat+ ffmpeg = self.root / ('ffmpeg.exe' if os.name == 'nt' else 'ffmpeg')+ ffmpeg.write_bytes(b'fixture')+ ffmpeg.with_name('ffprobe' + ffmpeg.suffix).write_bytes(b'fixture')+ self.resolver.ffmpeg.return_value = SimpleNamespace(path=ffmpeg)+ timestamps = []++ def capture(_ffmpeg, _source, timestamp, destination, **options):+ timestamps.append(timestamp)+ self.assertEqual(options['input_options'], ('-hwaccel', 'd3d11va'))+ Image.new('RGB', (480, 270), 'green').save(destination, format='JPEG')+ return True, ''++ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \+ patch('peach.hardware_transcode.sys.platform', 'win32'), \+ patch.object(hardware_transcode, '_d3d11va_ready', return_value=True), \+ patch.object(hardware_transcode, '_frame_codec', return_value='hevc'), \+ patch('peach.media_processing_policy.acceleration', return_value='auto'), \+ patch('peach.media_io.file_stat', wraps=file_stat) as reads, \+ patch.object(frame_capture, 'capture_frame', side_effect=capture), \+ patch.object(frame_capture, 'tile_frames', side_effect=self.make_tile):+ result = self.run_processing(stage='previews')+ self.assertEqual(result['status'], 'complete')+ self.assertEqual(result['previews'], 1)+ self.assertEqual(len(set(timestamps)), 9)+ self.assertEqual(reads.call_count, 1)+ with self.database.read_connection() as connection:+ snapshot = connection.execute('SELECT snapshot_path FROM asset WHERE id=1').fetchone()[0]+ self.assertTrue(Path(snapshot).is_file())+def test_preview_only_leaves_candidates_and_sources_untouched(self):factory = Mock(side_effect=AssertionError('外部资料不属于预览任务'))with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \@@ -3444,7 +3476,8 @@ class LibraryPreviewCompletionTests(unittest.TestCase):import zipfilefrom peach import support_bundle- def fail(_ffmpeg, video, _timestamp, _destination, *, diagnostics, timeout):+ def fail(_ffmpeg, video, _timestamp, _destination, *, diagnostics, timeout, decode_options):+ self.assertEqual(decode_options, ())self.assertGreater(timeout, 0)self.assertLessEqual(timeout, 45)diagnostics.update(reason='ffmpeg_error', returncode=69, timestamp_seconds=2.4667,