Compare commits
3 Commits
main
...
linux-fixe
| Author | SHA1 | Date | |
|---|---|---|---|
| 630ac6dd2d | |||
| 8fd85322ca | |||
| 15e19fa056 |
@@ -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)
|
||||
|
||||
@@ -1,209 +1,220 @@
|
||||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
import platform
|
||||
import sys
|
||||
|
||||
def _extract_subs(
|
||||
source_video: str,
|
||||
cdn_folder: str
|
||||
):
|
||||
out_ass = os.path.join(cdn_folder, 'eng.ass')
|
||||
out_vtt = os.path.join(cdn_folder, 'eng.vtt')
|
||||
|
||||
if os.path.exists(out_ass):
|
||||
print('Skipped Sub Extract')
|
||||
return
|
||||
|
||||
print('Extracting Sub')
|
||||
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{source_video}" -c copy "{out_ass}"', shell=True)
|
||||
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{out_ass}" "{out_vtt}"', shell=True)
|
||||
|
||||
def _create_sprites(cdn_folder: str):
|
||||
"""
|
||||
Creates video player sprites
|
||||
"""
|
||||
video_file = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||
# Generating Sprites
|
||||
if not os.path.exists(os.path.join(cdn_folder, 'thumbs.vtt')) and os.path.exists(video_file):
|
||||
os.system(f'python makesprites.py "{video_file}"')
|
||||
os.rename("thumbs.vtt", os.path.join(cdn_folder, 'thumbs.vtt'))
|
||||
os.rename("sprite.jpg", os.path.join(cdn_folder, 'sprite.jpg'))
|
||||
shutil.rmtree('thumbs')
|
||||
shutil.rmtree('logs')
|
||||
return
|
||||
|
||||
print('Skipped Sprites')
|
||||
|
||||
def _encode_720p_fallback(
|
||||
cdn_folder: str,
|
||||
video_source: str,
|
||||
upscale_output: str,
|
||||
aspect_ratio: str = "16:9"
|
||||
):
|
||||
"""
|
||||
Fallback video stream for apple devices
|
||||
"""
|
||||
output = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||
|
||||
if os.path.exists(output):
|
||||
print('Skipped 720p Encode')
|
||||
return
|
||||
|
||||
cmd = [
|
||||
"ffmpeg", "-v", "quiet", "-stats",
|
||||
"-i", upscale_output,
|
||||
"-i", video_source,
|
||||
"-map", "0:v:0",
|
||||
"-map", "1:a:0",
|
||||
"-c:v", "libx264",
|
||||
"-pix_fmt", "yuv420p",
|
||||
"-vf", "scale=1280:720,setsar=1:1",
|
||||
"-aspect", aspect_ratio,
|
||||
"-c:a", "aac",
|
||||
"-b:a", "128k",
|
||||
"-sn",
|
||||
"-map_metadata", "-1",
|
||||
"-movflags", "+faststart",
|
||||
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 _change_chunk_extension(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
):
|
||||
chunks_folder = os.path.join(cdn_folder, preset['out_folder'], 'chunks')
|
||||
mpd_file = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')
|
||||
|
||||
# Move encodes on Windows to correct folder
|
||||
if platform.system() != 'Linux':
|
||||
shutil.move('chunks', chunks_folder)
|
||||
|
||||
# Rename files
|
||||
for filename in os.listdir(chunks_folder):
|
||||
file_path = os.path.join(chunks_folder, filename)
|
||||
if not os.path.isfile(file_path):
|
||||
continue
|
||||
|
||||
new_file_path = file_path.replace('.webm', '.webp')
|
||||
os.rename(file_path, new_file_path)
|
||||
|
||||
# Modify manifest
|
||||
with open(mpd_file, 'r') as file :
|
||||
filedata = file.read()
|
||||
|
||||
# Replace the target string
|
||||
filedata = filedata.replace('.webm', '.webp')
|
||||
|
||||
# Write the file out again
|
||||
with open(mpd_file, 'w') as file:
|
||||
file.write(filedata)
|
||||
|
||||
def _create_folder(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
):
|
||||
if platform.system() == 'Linux' and not os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'chunks')):
|
||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder'], 'chunks'))
|
||||
return
|
||||
|
||||
# FFmpeg on Windows writes the chunk files
|
||||
# to the chunks folder at the root of where ffmpeg is invoked
|
||||
if not os.path.exists('chunks'):
|
||||
os.makedirs('chunks')
|
||||
|
||||
# The mpd file however is stored at the correct location
|
||||
if not os.path.exists(os.path.join(cdn_folder, preset['out_folder'])):
|
||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder']))
|
||||
|
||||
def _encode(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
source_video: str,
|
||||
aspect_ratio: str,
|
||||
segment_duration: int = 10,
|
||||
keyframe_interval: int = 2,
|
||||
):
|
||||
print(f"Encoding {preset['name']}")
|
||||
|
||||
cmd = [
|
||||
"ffmpeg", "-v", "quiet", "-stats",
|
||||
"-i", preset['input_video'],
|
||||
"-i", source_video,
|
||||
"-map", "0:v:0", # Video from Upscale
|
||||
"-map", "1:a:0", # Audio from Source
|
||||
"-c:v", preset['encoder'],
|
||||
"-preset", preset['preset'],
|
||||
"-crf", preset['crf'],
|
||||
"-pix_fmt", "yuv420p", # 8bit to increase decode performance
|
||||
"-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1",
|
||||
"-aspect", aspect_ratio,
|
||||
]
|
||||
|
||||
if preset["encoder"] == "libx264":
|
||||
cmd += ["-x264-params", f"keyint=24:min-keyint=24:scenecut=0"]
|
||||
cmd += ["-c:a", "aac", "-b:a", "128k"]
|
||||
elif preset["encoder"] == "libsvtav1":
|
||||
cmd += ["-svtav1-params", f"keyint={keyframe_interval}s,fast-decode=1,tune=0"]
|
||||
cmd += ["-c:a", "libopus", "-b:a", "128k"]
|
||||
|
||||
cmd += [
|
||||
"-ac", "2",
|
||||
"-sn", # No subtitles
|
||||
"-map_metadata", "-1", # Get rid of metadata which might be incorrect
|
||||
"-use_template", "1", # Don't list every segment url, use template instead
|
||||
"-use_timeline", "1", # Make sure segment timing is always correct
|
||||
"-init_seg_name", "chunks/init-stream$RepresentationID$.webm", # Init segment
|
||||
"-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')
|
||||
]
|
||||
|
||||
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_streams(
|
||||
cdn_folder: str,
|
||||
source_video: str,
|
||||
upscaled_video: str,
|
||||
interpolated_video: str,
|
||||
interpolated_uhd_video: str | None,
|
||||
aspect_ratio: str = "16:9",
|
||||
):
|
||||
presets = [
|
||||
{"name": "720p", "w": "1280", "h": "720", "encoder": "libx264", "preset": "medium", "crf": "22", "input_video": upscaled_video, "out_folder": '720'},
|
||||
{"name": "1080p", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": upscaled_video, "out_folder": '1080'},
|
||||
{"name": "1080p48", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": interpolated_video, "out_folder": '1080i'},
|
||||
{"name": "2160", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": upscaled_video, "out_folder": '2160'},
|
||||
]
|
||||
|
||||
# Optional UHD Interpolate encode
|
||||
if interpolated_uhd_video is not None:
|
||||
presets.append({"name": "2160p48", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": interpolated_uhd_video, "out_folder": '2160i'})
|
||||
|
||||
for preset in presets:
|
||||
# Skip already encoded streams
|
||||
if os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')):
|
||||
print(f"Skipped {preset['name']}")
|
||||
continue
|
||||
|
||||
_create_folder(preset, cdn_folder)
|
||||
_encode(preset, cdn_folder, source_video, aspect_ratio)
|
||||
_change_chunk_extension(preset, cdn_folder)
|
||||
|
||||
_extract_subs(source_video, cdn_folder)
|
||||
_encode_720p_fallback(cdn_folder, source_video, upscaled_video, aspect_ratio)
|
||||
_create_sprites(cdn_folder)
|
||||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
import platform
|
||||
import sys
|
||||
|
||||
def _extract_subs(
|
||||
source_video: str,
|
||||
cdn_folder: str
|
||||
):
|
||||
out_ass = os.path.join(cdn_folder, 'eng.ass')
|
||||
out_vtt = os.path.join(cdn_folder, 'eng.vtt')
|
||||
|
||||
if os.path.exists(out_ass):
|
||||
print('Skipped Sub Extract')
|
||||
return
|
||||
|
||||
print('Extracting Sub')
|
||||
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{source_video}" -c copy "{out_ass}"', shell=True)
|
||||
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{out_ass}" "{out_vtt}"', shell=True)
|
||||
|
||||
def _create_sprites(cdn_folder: str):
|
||||
"""
|
||||
Creates video player sprites
|
||||
"""
|
||||
video_file = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||
# Generating Sprites
|
||||
if not os.path.exists(os.path.join(cdn_folder, 'thumbs.vtt')) and os.path.exists(video_file):
|
||||
os.system(f'python makesprites.py "{video_file}"')
|
||||
os.rename("thumbs.vtt", os.path.join(cdn_folder, 'thumbs.vtt'))
|
||||
os.rename("sprite.jpg", os.path.join(cdn_folder, 'sprite.jpg'))
|
||||
shutil.rmtree('thumbs')
|
||||
shutil.rmtree('logs')
|
||||
return
|
||||
|
||||
print('Skipped Sprites')
|
||||
|
||||
def _encode_720p_fallback(
|
||||
cdn_folder: str,
|
||||
video_source: str,
|
||||
upscale_output: str,
|
||||
aspect_ratio: str = "16:9"
|
||||
):
|
||||
"""
|
||||
Fallback video stream for apple devices
|
||||
"""
|
||||
output = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||
|
||||
if os.path.exists(output):
|
||||
print('Skipped 720p Encode')
|
||||
return
|
||||
|
||||
cmd = [
|
||||
"ffmpeg", "-v", "quiet", "-stats",
|
||||
"-i", f'"{upscale_output}"',
|
||||
"-i", f'"{video_source}"',
|
||||
"-map", "0:v:0",
|
||||
"-map", "1:a:0",
|
||||
"-c:v", "libx264",
|
||||
"-pix_fmt", "yuv420p",
|
||||
"-vf", "scale=1280:720,setsar=1:1",
|
||||
"-aspect", aspect_ratio,
|
||||
"-c:a", "aac",
|
||||
"-b:a", "128k",
|
||||
"-sn",
|
||||
"-map_metadata", "-1",
|
||||
"-movflags", "+faststart",
|
||||
f'"{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 _change_chunk_extension(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
):
|
||||
chunks_folder = os.path.join(cdn_folder, preset['out_folder'], 'chunks')
|
||||
mpd_file = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')
|
||||
|
||||
# Move encodes on Windows to correct folder
|
||||
if platform.system() != 'Linux':
|
||||
shutil.move('chunks', chunks_folder)
|
||||
|
||||
# Rename files
|
||||
for filename in os.listdir(chunks_folder):
|
||||
file_path = os.path.join(chunks_folder, filename)
|
||||
if not os.path.isfile(file_path):
|
||||
continue
|
||||
|
||||
new_file_path = file_path.replace('.webm', '.webp')
|
||||
os.rename(file_path, new_file_path)
|
||||
|
||||
# Modify manifest
|
||||
with open(mpd_file, 'r') as file :
|
||||
filedata = file.read()
|
||||
|
||||
# Replace the target string
|
||||
filedata = filedata.replace('.webm', '.webp')
|
||||
|
||||
# Write the file out again
|
||||
with open(mpd_file, 'w') as file:
|
||||
file.write(filedata)
|
||||
|
||||
def _create_folder(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
):
|
||||
if platform.system() == 'Linux' and not os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'chunks')):
|
||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder'], 'chunks'))
|
||||
return
|
||||
|
||||
# FFmpeg on Windows writes the chunk files
|
||||
# to the chunks folder at the root of where ffmpeg is invoked
|
||||
if not os.path.exists('chunks'):
|
||||
os.makedirs('chunks')
|
||||
|
||||
# The mpd file however is stored at the correct location
|
||||
if not os.path.exists(os.path.join(cdn_folder, preset['out_folder'])):
|
||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder']))
|
||||
|
||||
def _encode(
|
||||
preset: dict[str, str],
|
||||
cdn_folder: str,
|
||||
source_video: str,
|
||||
aspect_ratio: str,
|
||||
segment_duration: int = 10,
|
||||
keyframe_interval: int = 2,
|
||||
):
|
||||
print(f"Encoding {preset['name']}")
|
||||
|
||||
cmd = [
|
||||
"ffmpeg", "-v", "quiet", "-stats",
|
||||
"-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'],
|
||||
"-preset", preset['preset'],
|
||||
"-crf", preset['crf'],
|
||||
"-pix_fmt", "yuv420p", # 8bit to increase decode performance
|
||||
"-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1",
|
||||
"-aspect", aspect_ratio,
|
||||
]
|
||||
|
||||
if preset["encoder"] == "libx264":
|
||||
cmd += ["-x264-params", f"keyint=24:min-keyint=24:scenecut=0"]
|
||||
cmd += ["-c:a", "aac", "-b:a", "128k"]
|
||||
elif preset["encoder"] == "libsvtav1":
|
||||
cmd += ["-svtav1-params", f"keyint={keyframe_interval}s,fast-decode=1,tune=0"]
|
||||
cmd += ["-c:a", "libopus", "-b:a", "128k"]
|
||||
|
||||
init_seg_name = "chunks/init-stream$RepresentationID$.webm"
|
||||
media_seg_name = "chunks/chunk-stream$RepresentationID$-$Number%05d$.webm"
|
||||
|
||||
if sys.platform == 'linux':
|
||||
init_seg_name = "chunks/init-stream\$RepresentationID\$.webm"
|
||||
media_seg_name = "chunks/chunk-stream\$RepresentationID\$-\$Number%05d\$.webm"
|
||||
|
||||
cmd += [
|
||||
"-ac", "2",
|
||||
"-sn", # No subtitles
|
||||
"-map_metadata", "-1", # Get rid of metadata which might be incorrect
|
||||
"-use_template", "1", # Don't list every segment url, use template instead
|
||||
"-use_timeline", "1", # Make sure segment timing is always correct
|
||||
"-init_seg_name", init_seg_name, # Init segment
|
||||
"-media_seg_name", media_seg_name, # Media segments
|
||||
"-seg_duration", str(segment_duration), # DASH segment duration
|
||||
"-f", "dash",
|
||||
f'"{os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')}"'
|
||||
]
|
||||
|
||||
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 encode_streams(
|
||||
cdn_folder: str,
|
||||
source_video: str,
|
||||
upscaled_video: str,
|
||||
interpolated_video: str,
|
||||
interpolated_uhd_video: str | None,
|
||||
aspect_ratio: str = "16:9",
|
||||
):
|
||||
presets = [
|
||||
{"name": "720p", "w": "1280", "h": "720", "encoder": "libx264", "preset": "medium", "crf": "22", "input_video": upscaled_video, "out_folder": '720'},
|
||||
{"name": "1080p", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": upscaled_video, "out_folder": '1080'},
|
||||
{"name": "1080p48", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": interpolated_video, "out_folder": '1080i'},
|
||||
{"name": "2160", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": upscaled_video, "out_folder": '2160'},
|
||||
]
|
||||
|
||||
# Optional UHD Interpolate encode
|
||||
if interpolated_uhd_video is not None:
|
||||
presets.append({"name": "2160p48", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": interpolated_uhd_video, "out_folder": '2160i'})
|
||||
|
||||
for preset in presets:
|
||||
# Skip already encoded streams
|
||||
if os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')):
|
||||
print(f"Skipped {preset['name']}")
|
||||
continue
|
||||
|
||||
_create_folder(preset, cdn_folder)
|
||||
_encode(preset, cdn_folder, source_video, aspect_ratio)
|
||||
_change_chunk_extension(preset, cdn_folder)
|
||||
|
||||
_extract_subs(source_video, cdn_folder)
|
||||
_encode_720p_fallback(cdn_folder, source_video, upscaled_video, aspect_ratio)
|
||||
_create_sprites(cdn_folder)
|
||||
|
||||
@@ -26,6 +26,9 @@ def _create_vsrife_script(
|
||||
script = [
|
||||
'import vapoursynth as vs',
|
||||
'from vsrife import rife',
|
||||
# this isn't a real fix, as this SHOULDN'T FIX IT
|
||||
# however for some reason it does
|
||||
'vs.core.max_cache_size=8192',
|
||||
f'clip = vs.core.ffms2.Source(source="./{hentai_name} [4k][HEVC].mkv")',
|
||||
f'clip = vs.core.resize.Bicubic(clip, width={video_width}, height=2160, format=vs.RGBS, matrix_in_s="709")',
|
||||
'clip = rife(clip=clip, model="4.25.lite", factor_num=2, factor_den=1)',
|
||||
@@ -43,15 +46,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:
|
||||
|
||||
190
utils/upcale.py
190
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')
|
||||
|
||||
Reference in New Issue
Block a user