From 6a6788335485527877555744eb88e4a1507acda6 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 23 Nov 2022 17:30:25 -0700 Subject: [PATCH] Remuxing video using yt-dlp to handle when output format is wrong. Checking for remuxed destination video file and using it. --- modules/videos/downloader.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 2f972316..18fe6e40 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -286,6 +286,7 @@ class VideoDownloader(Downloader, ABC): '--write-thumbnail', '--write-info-json', '--merge-output-format', PREFERRED_VIDEO_EXTENSION, + '--remux-video', PREFERRED_VIDEO_EXTENSION, '-o', file_name_format, '--no-cache-dir', '--compat-options', 'no-live-chat', @@ -303,6 +304,13 @@ class VideoDownloader(Downloader, ABC): error=error, ) + preferred_path = video_path.with_suffix(f'.{PREFERRED_VIDEO_EXTENSION}') + if not video_path.is_file() and preferred_path.is_file(): + # Prepared filename does not exist, but video with preferred video extension does, it was probably + # remuxed by yt-dlp. + video_path = preferred_path + logger.info(f'Using preferred video file which exists: {preferred_path}') + if not video_path.is_file(): error = f'{stdout}\n\n\n{stderr}\n\n' \ f'Video file could not be found! {video_path}' @@ -369,6 +377,7 @@ class VideoDownloader(Downloader, ABC): options = get_downloader_config().dict() options['outtmpl'] = f'{out_dir}/{options["file_name_format"]}' options['merge_output_format'] = PREFERRED_VIDEO_EXTENSION + options['remuxvideo'] = PREFERRED_VIDEO_EXTENSION options['format'] = PREFERRED_VIDEO_FORMAT # Create a new YoutubeDL for the output directory. @@ -379,8 +388,6 @@ class VideoDownloader(Downloader, ABC): # Get the path where the video will be saved. entry = extract_info(url, ydl=ydl, process=True) final_filename = pathlib.Path(prepare_filename(entry, ydl=ydl)).absolute() - if final_filename.suffix.lower() != f'.{PREFERRED_VIDEO_EXTENSION}': - raise DownloadError(f'Cannot download video {url} because yt-dlp filename is invalid.') logger.debug(f'Downloading {url} to {out_dir}') return final_filename, entry