import os import sys import subprocess def _remux_video( encoded_video_input: str, muxed_video_output: str, ): # Re-Mux with MKVMerge, as FFmpeg has some kind of bug try: subprocess.run(f'mkvmerge --output "{muxed_video_output}" "{encoded_video_input}"', 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 _encode_video( preset: dict[str, str], source_video: str, output_video: str, hentai_title: str, input_aspect: str = "16:9" ): print(f'Encoding {preset['h']}p AV1') cmd = [ "ffmpeg", "-i", preset['input_video'], "-i", 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 "-map", "1:t?", # Attachments from source video (optional) "-map", "1:d?", # Other Data from source video (optional) "-disposition:v:0", "default", # Mark video as default in mkv container "-metadata", f'Title=\"{hentai_title} [hstream.moe]\"', "-c:v", "libsvtav1", "-crf", preset['crf'], "-preset", "4", "-pix_fmt", "yuv420p10le", # 10bit "-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 ] print(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 encode_downloads( folder_name: str, hentai_title: str, source_video: str, upscaled_video: str, interpolated_video: str, interpolated_uhd_video: str | None, input_aspect: str = "16:9" ): presets = [ {"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1]", "input_video": upscaled_video}, {"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1][48fps]", "input_video": interpolated_video}, {"w": "3840", "h": "2160", "crf": "24", "name": "[2160p-AV1]", "input_video": upscaled_video}, ] if interpolated_uhd_video is not None: presets.append({"w": "3840", "h": "2160", "crf": "24", "name": "[2160p-AV1][48fps]", "input_video": interpolated_uhd_video}) for preset in presets: file_name = f"{hentai_title} {preset['name']}[hstream.moe].mkv" tmp_out = os.path.join('2-Out', folder_name, file_name) mux_out = os.path.join('2-Out', folder_name, 'Muxed', file_name) if os.path.exists(mux_out): print(f'Skipped {preset['h']}p AV1 Encode') return _encode_video(preset, source_video, tmp_out, hentai_title, input_aspect) _remux_video(tmp_out, mux_out)