From bb0582b13289535f756b7733ab19328ba9f2bac8 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 8 Aug 2023 21:25:02 -0600 Subject: [PATCH] Only remove file if it exists. --- modules/videos/common.py | 3 ++- scripts/find_corrupt_videos.py | 33 ++++++--------------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/modules/videos/common.py b/modules/videos/common.py index dbb10a69..646031b2 100644 --- a/modules/videos/common.py +++ b/modules/videos/common.py @@ -147,7 +147,8 @@ def ffmpeg_video_complete(video_path: Path, seconds: int = None) -> bool: except Exception: return False finally: - path.unlink() + if path.is_file(): + path.unlink() def generate_video_poster(video_path: Path, seconds: int = 5) -> Tuple[Path, Optional[int]]: diff --git a/scripts/find_corrupt_videos.py b/scripts/find_corrupt_videos.py index 8ec16a3f..55c0335c 100755 --- a/scripts/find_corrupt_videos.py +++ b/scripts/find_corrupt_videos.py @@ -18,6 +18,7 @@ import aiohttp from sqlalchemy.orm import Session from yt_dlp.utils import YoutubeDLError +from modules.videos.common import ffmpeg_video_complete from modules.videos.downloader import extract_info, VideoDownloader from modules.videos.models import Video, Channel from wrolpi.dates import seconds_to_timestamp @@ -138,33 +139,11 @@ async def find_corrupt_videos(channel_id: int = None): if not video.duration: logger.warning(f'No duration for {video}') continue - if video.duration > 5: - timestamp = seconds_to_timestamp(video.duration - 5) - else: - timestamp = seconds_to_timestamp(video.duration - 1) - # Create a screenshot from the end of the video. If this fails, the video is corrupt. - with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as fh: - fh = pathlib.Path(fh.name) - fh.unlink() - try: - cmd = ( - 'ffmpeg', '-n', - '-ss', timestamp, - '-i', str(video.video_path), - '-f', 'mjpeg', - '-vframes', '1', - '-q:v', '2', - str(fh), - ) - proc = subprocess.run(cmd, check=True, capture_output=True, timeout=10) - if fh.is_file() and fh.stat().st_size: - logger.debug(f'{video.video_path.name} is valid') - video.validated = True - session.commit() - fh.unlink() - continue - except subprocess.CalledProcessError as e: - logger.error(f'Failed to generate poster for {video.video_path}', exc_info=e) + if ffmpeg_video_complete(video.video_path): + logger.debug(f'{video.video_path.name} is valid') + video.validated = True + session.commit() + continue path = video.video_path await download_or_ask_delete(video, session, f'Corrupt video. {path} Delete? (y/N)')