mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
A transcode or remux refreshes the sha256 sums a download recorded (#906)
The Job replaces the video file (and its ffprobe json), so the sums in the info json's wrolpi section described files that were gone. Once the refresh has re-modeled the Video, its files are hashed again, without a session held, and the sums replaced. A Video without recorded sums gains none. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4c8e9fb68c
commit
78e2741db8
2 changed files with 58 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
"""Tests for the transcode Job and its API (see `wrolpi.jobs`)."""
|
||||
import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import shutil
|
||||
|
|
@ -318,3 +319,41 @@ async def test_transcode_temp_file_is_not_discovered_by_refresh(test_session, te
|
|||
assert str(video_path) in primary_paths
|
||||
assert str(tmp_path) not in primary_paths, 'the transcoder temporary file was indexed'
|
||||
assert not any(str(tmp_path) in str(p) for p in primary_paths)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize('recorded', [True, False])
|
||||
async def test_transcode_video_job_refreshes_sha256(test_session, test_directory, async_client, video_factory,
|
||||
recorded):
|
||||
"""The sums a download recorded follow the files the Job replaced; a Video without sums gains none."""
|
||||
video_path = test_directory / 'videos/NO CHANNEL/movie.webm'
|
||||
video_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
wrolpi = {'custom_title': 'Mine'}
|
||||
if recorded:
|
||||
wrolpi['sha256'] = {'.webm': 'old webm', '.ffprobe.json': 'old ffprobe'}
|
||||
video = video_factory(with_video_file=video_path, with_info_json={'duration': 5, 'wrolpi': wrolpi})
|
||||
test_session.commit()
|
||||
info_json_path = video_path.with_suffix('.info.json')
|
||||
|
||||
async def fake_run_command(cmd, stdout_callback=None, **kwargs):
|
||||
if cmd[-1] == '-':
|
||||
return CommandResult(return_code=0, cancelled=False, stdout=b'', stderr=b'', elapsed=0)
|
||||
shutil.copy(PROJECT_DIR / 'test/big_buck_bunny_720p_1mb.mp4', cmd[-1])
|
||||
return CommandResult(return_code=0, cancelled=False, stdout=b'', stderr=b'', elapsed=1)
|
||||
|
||||
with mock.patch('wrolpi.cmd.run_command', side_effect=fake_run_command):
|
||||
job_id = transcode_video_job.enqueue(file_group_id=video.file_group_id, container='mp4')
|
||||
record = await jobs.wait_for_job(job_id)
|
||||
assert record['status'] == jobs.COMPLETE, record
|
||||
|
||||
wrolpi = json.loads(info_json_path.read_text())['wrolpi']
|
||||
assert wrolpi['custom_title'] == 'Mine'
|
||||
if not recorded:
|
||||
assert 'sha256' not in wrolpi
|
||||
return
|
||||
mp4_path = video_path.with_suffix('.mp4')
|
||||
ffprobe_json_path = video_path.with_suffix('.ffprobe.json')
|
||||
assert wrolpi['sha256'] == {
|
||||
'.mp4': hashlib.sha256(mp4_path.read_bytes()).hexdigest(),
|
||||
'.ffprobe.json': hashlib.sha256(ffprobe_json_path.read_bytes()).hexdigest(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ from wrolpi.captions import FFMPEG_BIN
|
|||
from wrolpi.cmd import run_command
|
||||
from wrolpi.common import logger, get_relative_to_media_directory, wrol_mode_enabled
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.files.lib import sha256_files, sha256_by_suffix
|
||||
from wrolpi.jobs import register_job, get_current_job
|
||||
from wrolpi.vars import DEFAULT_FILE_PERMISSIONS, PYTEST
|
||||
|
||||
|
|
@ -505,6 +506,22 @@ def retarget_file_group(file_group_id: int, old_path: pathlib.Path, new_path: pa
|
|||
file_group.indexed = False
|
||||
|
||||
|
||||
async def refresh_recorded_sha256(file_group_id: int):
|
||||
"""Hash a FileGroup's files again if a download recorded their sums; the recorded sums describe files which
|
||||
may have been replaced. The files are read without a session held."""
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
with get_db_session() as session:
|
||||
file_group = session.get(FileGroup, file_group_id)
|
||||
if 'sha256' not in file_group.get_wrolpi_json():
|
||||
return
|
||||
paths = file_group.my_paths()
|
||||
|
||||
sums = await asyncio.to_thread(sha256_files, paths)
|
||||
with get_db_session(commit=True) as session:
|
||||
session.get(FileGroup, file_group_id).update_wrolpi_json({'sha256': sha256_by_suffix(sums)})
|
||||
|
||||
|
||||
@register_job('transcode_video')
|
||||
async def transcode_video_job(file_group_id: int, video_codec: Optional[str] = None,
|
||||
audio_codec: Optional[str] = None, container: str = 'mp4',
|
||||
|
|
@ -543,4 +560,6 @@ async def transcode_video_job(file_group_id: int, video_codec: Optional[str] = N
|
|||
job_id = file_worker.queue_refresh([final_path], send_events=False)
|
||||
await file_worker.wait_for_job(job_id)
|
||||
|
||||
await refresh_recorded_sha256(file_group_id)
|
||||
|
||||
return {'path': str(get_relative_to_media_directory(final_path))}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue