From 15e19fa056616f9e926b87379fafdfcf99d3ed63 Mon Sep 17 00:00:00 2001 From: w33b Date: Thu, 5 Mar 2026 23:03:59 +0100 Subject: [PATCH] CMD list handled differently when shell=True --- utils/encode_downloads.py | 13 +-- utils/encode_stream.py | 18 ++-- utils/interpolate.py | 7 +- utils/upcale.py | 190 ++++++++++++++++++++------------------ 4 files changed, 121 insertions(+), 107 deletions(-) diff --git a/utils/encode_downloads.py b/utils/encode_downloads.py index ad83058..3687f99 100644 --- a/utils/encode_downloads.py +++ b/utils/encode_downloads.py @@ -24,9 +24,9 @@ def _encode_video( print(f'Encoding {preset['h']}p AV1') cmd = [ - "ffmpeg", - "-i", preset['input_video'], - "-i", source_video, + "ffmpeg", "-v", "quiet", "-stats", + "-i", f'"{preset['input_video']}"', + "-i", f'"{source_video}"', "-map", "0:v:0", # Video from upscale or interpolated file "-map", "1:a:0", # Audio from source video "-map", "1:s:0", # Subtitle from source video @@ -38,15 +38,16 @@ def _encode_video( "-crf", preset['crf'], "-preset", "4", "-pix_fmt", "yuv420p10le", # 10bit - "-vf", f"scale=\'min({preset['w']},iw)\':-2,setsar=1:1", + "-vf", f"\"scale=\'min({preset['w']},iw)\':-2,setsar=1:1\"", "-aspect", input_aspect, "-c:a", "libopus", "-b:a", "160k", "-c:s", "copy", - output_video + f'"{output_video}"' ] - print(cmd) + if sys.platform == 'linux': + cmd = ' '.join(cmd) try: subprocess.run(cmd, shell=True, check=True) diff --git a/utils/encode_stream.py b/utils/encode_stream.py index 9d71fb9..bfcab13 100644 --- a/utils/encode_stream.py +++ b/utils/encode_stream.py @@ -52,8 +52,8 @@ def _encode_720p_fallback( cmd = [ "ffmpeg", "-v", "quiet", "-stats", - "-i", upscale_output, - "-i", video_source, + "-i", f'"{upscale_output}"', + "-i", f'"{video_source}"', "-map", "0:v:0", "-map", "1:a:0", "-c:v", "libx264", @@ -65,9 +65,12 @@ def _encode_720p_fallback( "-sn", "-map_metadata", "-1", "-movflags", "+faststart", - output + f'"{output}"' ] + if sys.platform == 'linux': + cmd = ' '.join(cmd) + try: subprocess.run(cmd, shell=True, check=True) except subprocess.CalledProcessError as e: @@ -135,8 +138,8 @@ def _encode( cmd = [ "ffmpeg", "-v", "quiet", "-stats", - "-i", preset['input_video'], - "-i", source_video, + "-i", f'"{preset['input_video']}"', + "-i", f'"{source_video}"', "-map", "0:v:0", # Video from Upscale "-map", "1:a:0", # Audio from Source "-c:v", preset['encoder'], @@ -164,10 +167,11 @@ def _encode( "-media_seg_name", "chunks/chunk-stream$RepresentationID$-$Number%05d$.webm", # Media segments "-seg_duration", str(segment_duration), # DASH segment duration "-f", "dash", - os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd') + f'"{os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')}"' ] - print(cmd) + if sys.platform == 'linux': + cmd = ' '.join(cmd) try: subprocess.run(cmd, shell=True, check=True) diff --git a/utils/interpolate.py b/utils/interpolate.py index d746280..064c5f6 100644 --- a/utils/interpolate.py +++ b/utils/interpolate.py @@ -43,15 +43,18 @@ def _interpolate( cmd = [ "vspipe", "-c", "y4m", - vapoursynth_file, + f'"{vapoursynth_file}"', "-", "|", "ffmpeg", "-v", "quiet", "-stats", "-i", "-", "-c:v", "hevc_nvenc", "-qp", "5", - interpolate_output + f'"{interpolate_output}"' ] + if sys.platform == 'linux': + cmd = ' '.join(cmd) + try: subprocess.run(cmd, shell=True, check=True) except subprocess.CalledProcessError as e: diff --git a/utils/upcale.py b/utils/upcale.py index ffe449c..ce13bf5 100644 --- a/utils/upcale.py +++ b/utils/upcale.py @@ -1,92 +1,98 @@ -import os -import sys -import subprocess - -from utils.mediainfo import get_framerate - -MAX_INPUT_WIDTH = '720' - -def _re_encode( - source_video: str, - temp_out_video: str, - input_aspect: str = "16:9" -): - """ - Re-Encodes the source video to avoid nasty video bugs - - :param source_video: Video Input - :type source_video: str - :param input_aspect: Aspect Ratio of Video - :type input_aspect: str - """ - - cmd = [ - "ffmpeg", "-v", "quiet", "-stats", - "-i", source_video, - "-c:v", "ffv1", - "-level", "3", - "-vf", f"fps={get_framerate(source_video)},scale=-1:\'min({MAX_INPUT_WIDTH},ih)\'", - "-aspect", input_aspect, - "-pix_fmt", "yuv420p", - "-color_primaries", "1", - "-color_trc", "1", - "-colorspace", "1", - "-an", - "-sn", - "-map_metadata", "-1", - temp_out_video - ] - - try: - subprocess.run(cmd, shell=True, check=True) - except subprocess.CalledProcessError as e: - print(f"\nffmpeg failed with error code {e.returncode} at _re_encode()", file=sys.stderr) - sys.exit(e.returncode) - -def _upscale( - upscale_output: str, - input_aspect: str = "16:9" -): - print('Started Upscale') - - vapoursynth_script = os.path.join('utils', 'vs-realesrgan.vpy') - - cmd = [ - "vspipe", - "-c", "y4m", - vapoursynth_script, - "-", # Video output to pipe - "|", # Pipe - "ffmpeg", "-v", "quiet", "-stats", - "-f", "yuv4mpegpipe", - "-i", "-", # Pipe Video Input - "-c:v", "hevc_nvenc", - "-qp", "5", - "-aspect", input_aspect, - upscale_output - ] - - try: - subprocess.run(cmd, shell=True, check=True) - except subprocess.CalledProcessError as e: - print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr) - sys.exit(e.returncode) - - -def upscale( - source_video: str, - upscaled_video_output: str, - input_aspect: str, -): - if os.path.exists(upscaled_video_output): - print('Skipped Upscale') - return - - temp_out_video = os.path.join('1-Temp', 'source.mkv') - - _re_encode(source_video, temp_out_video, input_aspect) - _upscale(upscaled_video_output, input_aspect) - - # Remove Temp Files - os.remove(temp_out_video) - os.remove(f'{temp_out_video}.ffindex') +import os +import sys +import subprocess + +from utils.mediainfo import get_framerate + +MAX_INPUT_WIDTH = '720' + +def _re_encode( + source_video: str, + temp_out_video: str, + input_aspect: str = "16:9" +): + """ + Re-Encodes the source video to avoid nasty video bugs + + :param source_video: Video Input + :type source_video: str + :param input_aspect: Aspect Ratio of Video + :type input_aspect: str + """ + + cmd = [ + "ffmpeg", "-v", "quiet", "-stats", + "-i", f'"{source_video}"', + "-c:v", "ffv1", + "-level", "3", + "-vf", f"\"fps={get_framerate(source_video)},scale=-1:\'min({MAX_INPUT_WIDTH},ih)\'\"", + "-aspect", input_aspect, + "-pix_fmt", "yuv420p", + "-color_primaries", "1", + "-color_trc", "1", + "-colorspace", "1", + "-an", + "-sn", + "-map_metadata", "-1", + f'"{temp_out_video}"' + ] + + if sys.platform == 'linux': + cmd = ' '.join(cmd) + + try: + subprocess.run(cmd, shell=True, check=True) + except subprocess.CalledProcessError as e: + print(f"\nffmpeg failed with error code {e.returncode} at _re_encode()", file=sys.stderr) + sys.exit(e.returncode) + +def _upscale( + upscale_output: str, + input_aspect: str = "16:9" +): + print('Started Upscale') + + vapoursynth_script = os.path.join('utils', 'vs-realesrgan.vpy') + + cmd = [ + "vspipe", + "-c", "y4m", + f"\"{vapoursynth_script}\"", + "-", # Video output to pipe + "|", # Pipe + "ffmpeg", "-v", "quiet", "-stats", + "-f", "yuv4mpegpipe", + "-i", "-", # Pipe Video Input + "-c:v", "hevc_nvenc", + "-qp", "5", + "-aspect", input_aspect, + f"\"{upscale_output}\"" + ] + + if sys.platform == 'linux': + cmd = ' '.join(cmd) + + try: + subprocess.run(cmd, shell=True, check=True) + except subprocess.CalledProcessError as e: + print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr) + sys.exit(e.returncode) + + +def upscale( + source_video: str, + upscaled_video_output: str, + input_aspect: str, +): + if os.path.exists(upscaled_video_output): + print('Skipped Upscale') + return + + temp_out_video = os.path.join('1-Temp', 'source.mkv') + + _re_encode(source_video, temp_out_video, input_aspect) + _upscale(upscaled_video_output, input_aspect) + + # Remove Temp Files + os.remove(temp_out_video) + os.remove(f'{temp_out_video}.ffindex')