Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(library): 九宫格某格抽帧失败时改取更早的时间点

longmeidao13cf6a9206f4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6053f4fdb..3bea0701f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ Removed / Fixed / Security。
### 修复
+- **馆藏**:转存、极短或尾部损坏的视频,九宫格靠后几格解不出时改取更早的画面,不再一直挂在「需要处理」(#392)。
- **采集**:JavDB 匿名接口的一个主机连不上时自动换下一个,三个都不通才暂停刮削(#315)。
- **馆藏**:入库、采集和 JAV 新作、发现页认女优与厂牌更快。
- **界面**:后台任务写入资料时和重启后,目录、索引与统计页打开不再等重算。
diff --git a/src/peach/library_processing.py b/src/peach/library_processing.py
index 12228e7d3..1468267fa 100644
--- a/src/peach/library_processing.py
+++ b/src/peach/library_processing.py
@@ -2426,6 +2426,41 @@ class _PreviewCaptureError(ValueError):
super().__init__(message)
+def _earlier_timestamps(timestamp, duration):
+ """某格抽帧失败后依次改取的更早时间点,只留比上一次更早、仍在片长内的点。
+
+ 转存 mp4、只有几帧的短 hevc、尾部损坏的手机视频,容器声明的时长比实际能解码的长,
+ 靠后的格子落进坏区,硬解软解都取不到帧;前段的帧是好的(#392 实测)。"""
+ candidates = []
+ for value in (timestamp * 0.5, timestamp * 0.25, max(0.5, duration * 0.05)):
+ if 0 < value < duration and value < (candidates[-1] if candidates else timestamp):
+ candidates.append(value)
+ return candidates
+
+
+def _capture_preview_cell(capture, timestamp, duration, *, deadline, progress):
+ """抽九宫格的一格,返回(是否取到, 首抽诊断);回退尝试记进诊断的 earlier_attempts。
+
+ 超时多是挂载慢,换点位只会耗尽预算;FFmpeg 报错或没出帧才往前找好帧。"""
+ diagnostic = {}
+ if capture(timestamp, diagnostic):
+ return True, diagnostic
+ if diagnostic.get('reason') not in ('ffmpeg_error', 'no_frame'):
+ return False, diagnostic
+ earlier = diagnostic['earlier_attempts'] = []
+ for fallback in _earlier_timestamps(timestamp, duration):
+ if deadline <= time.monotonic():
+ break
+ progress()
+ detail = {}
+ captured = capture(fallback, detail)
+ earlier.append(dict(timestamp_seconds=fallback, reason=detail.get('reason'),
+ returncode=detail.get('returncode')))
+ if captured:
+ return True, diagnostic
+ return False, diagnostic
+
+
def _generate_preview(config, row, database, resolver, *, progress):
"""复用九宫格抽帧原语,产物可用后在共享写锁内登记。"""
from . import frame_capture, media_probe
@@ -2477,10 +2512,12 @@ def _generate_preview(config, row, database, resolver, *, progress):
return False
progress()
timestamp = duration * (0.03 + 0.94 * (index + 0.5) / 9)
- 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), decode_options=decode_options):
+ captured, diagnostic = _capture_preview_cell(
+ lambda at, detail, destination=frames / f's{index:02d}.jpg': frame_capture.capture_with_retry(
+ str(choice.path), str(video), at, destination,
+ diagnostics=detail, timeout=remaining(45), decode_options=decode_options),
+ timestamp, duration, deadline=deadline, progress=progress)
+ if not captured:
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_metadata_library.py b/tests/test_metadata_library.py
index 8371ad9e3..a3859cbfa 100644
--- a/tests/test_metadata_library.py
+++ b/tests/test_metadata_library.py
@@ -3689,6 +3689,85 @@ class LibraryPreviewCompletionTests(unittest.TestCase):
self.assertEqual(retried['issue_count'], 0)
self.assertEqual(retried['retryable_asset_ids'], [])
+ def test_cells_in_an_undecodable_tail_take_earlier_frames(self):
+ # 容器写 30 秒,实际只有前 9 秒解得出:靠后的格子先失败,再往前找到好帧。
+ calls = []
+
+ def capture(_ffmpeg, _video, timestamp, _destination, *, diagnostics, timeout, decode_options):
+ calls.append(timestamp)
+ if timestamp < 9:
+ diagnostics.update(reason='success', returncode=0)
+ return True
+ diagnostics.update(reason='ffmpeg_error', returncode=234, attempts=[],
+ stderr='Could not open encoder before EOF')
+ return False
+
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', side_effect=capture), \
+ patch('peach.frame_capture.tile_frames', side_effect=self.make_tile):
+ result = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(result['status'], 'complete')
+ self.assertEqual(result['previews'], 1)
+ planned = [30 * (0.03 + 0.94 * (index + 0.5) / 9) for index in range(9)]
+ healthy = [value for value in planned if value < 9]
+ taken = [value for value in calls if value < 9]
+ self.assertEqual(len(taken), 9)
+ self.assertEqual(taken[:len(healthy)], healthy)
+ self.assertGreater(len(calls), 9)
+ 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_healthy_video_extracts_each_cell_once(self):
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', return_value=True) as capture, \
+ patch('peach.frame_capture.tile_frames', side_effect=self.make_tile):
+ result = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(result['status'], 'complete')
+ self.assertEqual(capture.call_count, 9)
+
+ def test_undecodable_video_still_fails_after_earlier_attempts(self):
+ calls = []
+
+ def fail(_ffmpeg, _video, timestamp, _destination, *, diagnostics, timeout, decode_options):
+ calls.append(timestamp)
+ diagnostics.update(reason='no_frame', returncode=0, attempts=[], stderr='')
+ return False
+
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', side_effect=fail):
+ result = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(result['status'], 'failed')
+ self.assertEqual(result['retryable_asset_ids'], [1])
+ detail = json.loads(Path(result['issues_log']).read_text(encoding='utf-8').splitlines()[0])['diagnostics']
+ self.assertEqual((detail['frame_index'], detail['reason']), (0, 'no_frame'))
+ earlier = [attempt['timestamp_seconds'] for attempt in detail['earlier_attempts']]
+ self.assertTrue(earlier)
+ self.assertEqual(calls[1:], earlier)
+ self.assertEqual(earlier, sorted(earlier, reverse=True))
+ self.assertLess(earlier[0], calls[0])
+ with self.database.read_connection() as connection:
+ self.assertIsNone(connection.execute('SELECT snapshot_path FROM asset WHERE id=1').fetchone()[0])
+
+ def test_timed_out_cell_does_not_retry_earlier_points(self):
+ def slow(_ffmpeg, _video, timestamp, _destination, *, diagnostics, timeout, decode_options):
+ diagnostics.update(reason='timeout', returncode=None, attempts=[], stderr='')
+ return False
+
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', side_effect=slow) as capture:
+ result = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(result['status'], 'failed')
+ self.assertEqual(capture.call_count, 1)
+
+ def test_earlier_points_stay_inside_the_clip_and_move_backwards(self):
+ from peach.library_processing import _earlier_timestamps
+ self.assertEqual(_earlier_timestamps(27.5, 30), [13.75, 6.875, 1.5])
+ # 0.1 秒的短片:0.5 秒那个保底点已经越过片尾,不再尝试。
+ self.assertEqual(_earlier_timestamps(0.08, 0.1), [0.04, 0.02])
+ # 首格本来就靠前:保底点不比上一次更早就跳过。
+ self.assertEqual(_earlier_timestamps(2.4, 30), [1.2, 0.6])
+
def test_selected_assets_fit_the_connection_parameter_limit(self):
from peach.library_processing import _asset_selection
connect = sqlite3.connect