93 lines
2.3 KiB
Python
93 lines
2.3 KiB
Python
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')
|