Compare commits
3 Commits
main
...
linux-fixe
| Author | SHA1 | Date | |
|---|---|---|---|
| 630ac6dd2d | |||
| 8fd85322ca | |||
| 15e19fa056 |
@@ -21,33 +21,36 @@ def _encode_video(
|
|||||||
hentai_title: str,
|
hentai_title: str,
|
||||||
input_aspect: str = "16:9"
|
input_aspect: str = "16:9"
|
||||||
):
|
):
|
||||||
print(f"Encoding {preset['h']}p AV1")
|
print(f'Encoding {preset['h']}p AV1')
|
||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
"ffmpeg", "-v", "quiet", "-stats",
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
"-i", preset['input_video'],
|
"-i", f'"{preset['input_video']}"',
|
||||||
"-i", source_video,
|
"-i", f'"{source_video}"',
|
||||||
"-map", "0:v:0", # Video from upscale or interpolated file
|
"-map", "0:v:0", # Video from upscale or interpolated file
|
||||||
"-map", "1:a:0", # Audio from source video
|
"-map", "1:a:0", # Audio from source video
|
||||||
"-map", "1:s:0", # Subtitle from source video
|
"-map", "1:s:0", # Subtitle from source video
|
||||||
"-map", "1:t?", # Attachments from source video (optional)
|
"-map", "1:t?", # Attachments from source video (optional)
|
||||||
"-map", "1:d?", # Other Data from source video (optional)
|
"-map", "1:d?", # Other Data from source video (optional)
|
||||||
"-disposition:v:0", "default", # Mark video as default in mkv container
|
"-disposition:v:0", "default", # Mark video as default in mkv container
|
||||||
"-metadata", f"Title={hentai_title} [hstream.moe]",
|
"-metadata", f'Title=\"{hentai_title} [hstream.moe]\"',
|
||||||
"-c:v", "libsvtav1",
|
"-c:v", "libsvtav1",
|
||||||
"-crf", preset['crf'],
|
"-crf", preset['crf'],
|
||||||
"-preset", "4",
|
"-preset", "4",
|
||||||
"-pix_fmt", "yuv420p10le",
|
"-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,
|
"-aspect", input_aspect,
|
||||||
"-c:a", "libopus",
|
"-c:a", "libopus",
|
||||||
"-b:a", "160k",
|
"-b:a", "160k",
|
||||||
"-c:s", "copy",
|
"-c:s", "copy",
|
||||||
output_video
|
f'"{output_video}"'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if sys.platform == 'linux':
|
||||||
|
cmd = ' '.join(cmd)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(cmd, check=True)
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
||||||
sys.exit(e.returncode)
|
sys.exit(e.returncode)
|
||||||
@@ -62,13 +65,13 @@ def encode_downloads(
|
|||||||
input_aspect: str = "16:9"
|
input_aspect: str = "16:9"
|
||||||
):
|
):
|
||||||
presets = [
|
presets = [
|
||||||
{"w": "1920", "h": "1080", "crf": "24", "name": "[1080p-AV1]", "input_video": upscaled_video},
|
{"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1]", "input_video": upscaled_video},
|
||||||
{"w": "1920", "h": "1080", "crf": "24", "name": "[1080p-AV1][48fps]", "input_video": interpolated_video},
|
{"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1][48fps]", "input_video": interpolated_video},
|
||||||
{"w": "3840", "h": "2160", "crf": "26", "name": "[2160p-AV1]", "input_video": upscaled_video},
|
{"w": "3840", "h": "2160", "crf": "24", "name": "[2160p-AV1]", "input_video": upscaled_video},
|
||||||
]
|
]
|
||||||
|
|
||||||
if interpolated_uhd_video is not None:
|
if interpolated_uhd_video is not None:
|
||||||
presets.append({"w": "3840", "h": "2160", "crf": "26", "name": "[2160p-AV1][48fps]", "input_video": interpolated_uhd_video})
|
presets.append({"w": "3840", "h": "2160", "crf": "24", "name": "[2160p-AV1][48fps]", "input_video": interpolated_uhd_video})
|
||||||
|
|
||||||
for preset in presets:
|
for preset in presets:
|
||||||
file_name = f"{hentai_title} {preset['name']}[hstream.moe].mkv"
|
file_name = f"{hentai_title} {preset['name']}[hstream.moe].mkv"
|
||||||
@@ -77,7 +80,7 @@ def encode_downloads(
|
|||||||
|
|
||||||
if os.path.exists(mux_out):
|
if os.path.exists(mux_out):
|
||||||
print(f'Skipped {preset['h']}p AV1 Encode')
|
print(f'Skipped {preset['h']}p AV1 Encode')
|
||||||
continue
|
return
|
||||||
|
|
||||||
_encode_video(preset, source_video, tmp_out, hentai_title, input_aspect)
|
_encode_video(preset, source_video, tmp_out, hentai_title, input_aspect)
|
||||||
_remux_video(tmp_out, mux_out)
|
_remux_video(tmp_out, mux_out)
|
||||||
|
|||||||
@@ -1,208 +1,220 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import shutil
|
import shutil
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def _extract_subs(
|
def _extract_subs(
|
||||||
source_video: str,
|
source_video: str,
|
||||||
cdn_folder: str
|
cdn_folder: str
|
||||||
):
|
):
|
||||||
out_ass = os.path.join(cdn_folder, 'eng.ass')
|
out_ass = os.path.join(cdn_folder, 'eng.ass')
|
||||||
out_vtt = os.path.join(cdn_folder, 'eng.vtt')
|
out_vtt = os.path.join(cdn_folder, 'eng.vtt')
|
||||||
|
|
||||||
if os.path.exists(out_ass):
|
if os.path.exists(out_ass):
|
||||||
print('Skipped Sub Extract')
|
print('Skipped Sub Extract')
|
||||||
return
|
return
|
||||||
|
|
||||||
print('Extracting Sub')
|
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 "{source_video}" -c copy "{out_ass}"', shell=True)
|
||||||
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{out_ass}" "{out_vtt}"', shell=True)
|
subprocess.call(f'ffmpeg -y -v quiet -stats -i "{out_ass}" "{out_vtt}"', shell=True)
|
||||||
|
|
||||||
def _create_sprites(cdn_folder: str):
|
def _create_sprites(cdn_folder: str):
|
||||||
"""
|
"""
|
||||||
Creates video player sprites
|
Creates video player sprites
|
||||||
"""
|
"""
|
||||||
video_file = os.path.join(cdn_folder, 'x264.720p.mp4')
|
video_file = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||||
# Generating Sprites
|
# Generating Sprites
|
||||||
if not os.path.exists(os.path.join(cdn_folder, 'thumbs.vtt')) and os.path.exists(video_file):
|
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.system(f'python makesprites.py "{video_file}"')
|
||||||
os.rename("thumbs.vtt", os.path.join(cdn_folder, 'thumbs.vtt'))
|
os.rename("thumbs.vtt", os.path.join(cdn_folder, 'thumbs.vtt'))
|
||||||
os.rename("sprite.jpg", os.path.join(cdn_folder, 'sprite.jpg'))
|
os.rename("sprite.jpg", os.path.join(cdn_folder, 'sprite.jpg'))
|
||||||
shutil.rmtree('thumbs')
|
shutil.rmtree('thumbs')
|
||||||
shutil.rmtree('logs')
|
shutil.rmtree('logs')
|
||||||
return
|
return
|
||||||
|
|
||||||
print('Skipped Sprites')
|
print('Skipped Sprites')
|
||||||
|
|
||||||
def _encode_720p_fallback(
|
def _encode_720p_fallback(
|
||||||
cdn_folder: str,
|
cdn_folder: str,
|
||||||
video_source: str,
|
video_source: str,
|
||||||
upscale_output: str,
|
upscale_output: str,
|
||||||
aspect_ratio: str = "16:9"
|
aspect_ratio: str = "16:9"
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Fallback video stream for apple devices
|
Fallback video stream for apple devices
|
||||||
"""
|
"""
|
||||||
output = os.path.join(cdn_folder, 'x264.720p.mp4')
|
output = os.path.join(cdn_folder, 'x264.720p.mp4')
|
||||||
|
|
||||||
if os.path.exists(output):
|
if os.path.exists(output):
|
||||||
print('Skipped 720p Encode')
|
print('Skipped 720p Encode')
|
||||||
return
|
return
|
||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
"ffmpeg", "-v", "quiet", "-stats",
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
"-i", upscale_output,
|
"-i", f'"{upscale_output}"',
|
||||||
"-i", video_source,
|
"-i", f'"{video_source}"',
|
||||||
"-map", "0:v:0",
|
"-map", "0:v:0",
|
||||||
"-map", "1:a:0",
|
"-map", "1:a:0",
|
||||||
"-c:v", "libx264",
|
"-c:v", "libx264",
|
||||||
"-pix_fmt", "yuv420p",
|
"-pix_fmt", "yuv420p",
|
||||||
"-vf", "scale=1280:720,setsar=1:1",
|
"-vf", "scale=1280:720,setsar=1:1",
|
||||||
"-aspect", aspect_ratio,
|
"-aspect", aspect_ratio,
|
||||||
"-c:a", "aac",
|
"-c:a", "aac",
|
||||||
"-b:a", "128k",
|
"-b:a", "128k",
|
||||||
"-sn",
|
"-sn",
|
||||||
"-map_metadata", "-1",
|
"-map_metadata", "-1",
|
||||||
"-movflags", "+faststart",
|
"-movflags", "+faststart",
|
||||||
output
|
f'"{output}"'
|
||||||
]
|
]
|
||||||
|
|
||||||
try:
|
if sys.platform == 'linux':
|
||||||
subprocess.run(cmd, check=True)
|
cmd = ' '.join(cmd)
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
try:
|
||||||
sys.exit(e.returncode)
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
def _change_chunk_extension(
|
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
||||||
preset: dict[str, str],
|
sys.exit(e.returncode)
|
||||||
cdn_folder: str,
|
|
||||||
):
|
|
||||||
chunks_folder = os.path.join(cdn_folder, preset['out_folder'], 'chunks')
|
def _change_chunk_extension(
|
||||||
mpd_file = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')
|
preset: dict[str, str],
|
||||||
|
cdn_folder: str,
|
||||||
# Move encodes on Windows to correct folder
|
):
|
||||||
if platform.system() != 'Linux':
|
chunks_folder = os.path.join(cdn_folder, preset['out_folder'], 'chunks')
|
||||||
shutil.move('chunks', chunks_folder)
|
mpd_file = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')
|
||||||
|
|
||||||
# Rename files
|
# Move encodes on Windows to correct folder
|
||||||
for filename in os.listdir(chunks_folder):
|
if platform.system() != 'Linux':
|
||||||
file_path = os.path.join(chunks_folder, filename)
|
shutil.move('chunks', chunks_folder)
|
||||||
if not os.path.isfile(file_path):
|
|
||||||
continue
|
# Rename files
|
||||||
|
for filename in os.listdir(chunks_folder):
|
||||||
new_file_path = file_path.replace('.webm', '.webp')
|
file_path = os.path.join(chunks_folder, filename)
|
||||||
os.rename(file_path, new_file_path)
|
if not os.path.isfile(file_path):
|
||||||
|
continue
|
||||||
# Modify manifest
|
|
||||||
with open(mpd_file, 'r') as file :
|
new_file_path = file_path.replace('.webm', '.webp')
|
||||||
filedata = file.read()
|
os.rename(file_path, new_file_path)
|
||||||
|
|
||||||
# Replace the target string
|
# Modify manifest
|
||||||
filedata = filedata.replace('.webm', '.webp')
|
with open(mpd_file, 'r') as file :
|
||||||
|
filedata = file.read()
|
||||||
# Write the file out again
|
|
||||||
with open(mpd_file, 'w') as file:
|
# Replace the target string
|
||||||
file.write(filedata)
|
filedata = filedata.replace('.webm', '.webp')
|
||||||
|
|
||||||
def _create_folder(
|
# Write the file out again
|
||||||
preset: dict[str, str],
|
with open(mpd_file, 'w') as file:
|
||||||
cdn_folder: str,
|
file.write(filedata)
|
||||||
):
|
|
||||||
if platform.system() == 'Linux' and not os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'chunks')):
|
def _create_folder(
|
||||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder'], 'chunks'))
|
preset: dict[str, str],
|
||||||
return
|
cdn_folder: str,
|
||||||
|
):
|
||||||
# FFmpeg on Windows writes the chunk files
|
if platform.system() == 'Linux' and not os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'chunks')):
|
||||||
# to the chunks folder at the root of where ffmpeg is invoked
|
os.makedirs(os.path.join(cdn_folder, preset['out_folder'], 'chunks'))
|
||||||
if not os.path.exists('chunks'):
|
return
|
||||||
os.makedirs('chunks')
|
|
||||||
|
# FFmpeg on Windows writes the chunk files
|
||||||
# The mpd file however is stored at the correct location
|
# to the chunks folder at the root of where ffmpeg is invoked
|
||||||
if not os.path.exists(os.path.join(cdn_folder, preset['out_folder'])):
|
if not os.path.exists('chunks'):
|
||||||
os.makedirs(os.path.join(cdn_folder, preset['out_folder']))
|
os.makedirs('chunks')
|
||||||
|
|
||||||
def _encode(
|
# The mpd file however is stored at the correct location
|
||||||
preset: dict[str, str],
|
if not os.path.exists(os.path.join(cdn_folder, preset['out_folder'])):
|
||||||
cdn_folder: str,
|
os.makedirs(os.path.join(cdn_folder, preset['out_folder']))
|
||||||
source_video: str,
|
|
||||||
aspect_ratio: str,
|
def _encode(
|
||||||
segment_duration: int = 10,
|
preset: dict[str, str],
|
||||||
keyframe_interval: int = 2,
|
cdn_folder: str,
|
||||||
):
|
source_video: str,
|
||||||
print(f"Encoding {preset['name']}")
|
aspect_ratio: str,
|
||||||
|
segment_duration: int = 10,
|
||||||
cmd = [
|
keyframe_interval: int = 2,
|
||||||
"ffmpeg", "-v", "quiet", "-stats",
|
):
|
||||||
"-i", preset['input_video'],
|
print(f"Encoding {preset['name']}")
|
||||||
"-i", source_video,
|
|
||||||
"-map", "0:v:0", # Video from Upscale
|
cmd = [
|
||||||
"-map", "1:a:0", # Audio from Source
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
"-c:v", preset['encoder'],
|
"-i", f'"{preset['input_video']}"',
|
||||||
"-preset", preset['preset'],
|
"-i", f'"{source_video}"',
|
||||||
"-crf", str(preset['crf']),
|
"-map", "0:v:0", # Video from Upscale
|
||||||
"-pix_fmt", "yuv420p", # 8bit to increase decode performance
|
"-map", "1:a:0", # Audio from Source
|
||||||
"-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1",
|
"-c:v", preset['encoder'],
|
||||||
"-aspect", aspect_ratio,
|
"-preset", preset['preset'],
|
||||||
]
|
"-crf", preset['crf'],
|
||||||
|
"-pix_fmt", "yuv420p", # 8bit to increase decode performance
|
||||||
if preset["encoder"] == "libx264":
|
"-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1",
|
||||||
cmd += ["-x264-params", "keyint=24:min-keyint=24:scenecut=0"]
|
"-aspect", aspect_ratio,
|
||||||
cmd += ["-c:a", "aac", "-b:a", "160k"]
|
]
|
||||||
elif preset["encoder"] == "libsvtav1":
|
|
||||||
cmd += ["-svtav1-params", f"keyint={keyframe_interval}s:fast-decode=1:tune=1"]
|
if preset["encoder"] == "libx264":
|
||||||
cmd += ["-c:a", "aac", "-b:a", "160k"]
|
cmd += ["-x264-params", f"keyint=24:min-keyint=24:scenecut=0"]
|
||||||
|
cmd += ["-c:a", "aac", "-b:a", "128k"]
|
||||||
output_path = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')
|
elif preset["encoder"] == "libsvtav1":
|
||||||
|
cmd += ["-svtav1-params", f"keyint={keyframe_interval}s,fast-decode=1,tune=0"]
|
||||||
cmd += [
|
cmd += ["-c:a", "libopus", "-b:a", "128k"]
|
||||||
"-ac", "2",
|
|
||||||
"-sn", # No subtitles
|
init_seg_name = "chunks/init-stream$RepresentationID$.webm"
|
||||||
"-map_metadata", "-1", # Get rid of metadata which might be incorrect
|
media_seg_name = "chunks/chunk-stream$RepresentationID$-$Number%05d$.webm"
|
||||||
"-use_template", "1", # Don't list every segment url, use template instead
|
|
||||||
"-use_timeline", "1", # Make sure segment timing is always correct
|
if sys.platform == 'linux':
|
||||||
"-init_seg_name", "chunks/init-stream$RepresentationID$.webm",
|
init_seg_name = "chunks/init-stream\$RepresentationID\$.webm"
|
||||||
"-media_seg_name", "chunks/chunk-stream$RepresentationID$-$Number%05d$.webm",
|
media_seg_name = "chunks/chunk-stream\$RepresentationID\$-\$Number%05d\$.webm"
|
||||||
"-seg_duration", str(segment_duration),
|
|
||||||
"-f", "dash",
|
cmd += [
|
||||||
output_path
|
"-ac", "2",
|
||||||
]
|
"-sn", # No subtitles
|
||||||
|
"-map_metadata", "-1", # Get rid of metadata which might be incorrect
|
||||||
try:
|
"-use_template", "1", # Don't list every segment url, use template instead
|
||||||
subprocess.run(cmd, check=True)
|
"-use_timeline", "1", # Make sure segment timing is always correct
|
||||||
except subprocess.CalledProcessError as e:
|
"-init_seg_name", init_seg_name, # Init segment
|
||||||
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
"-media_seg_name", media_seg_name, # Media segments
|
||||||
sys.exit(e.returncode)
|
"-seg_duration", str(segment_duration), # DASH segment duration
|
||||||
|
"-f", "dash",
|
||||||
def encode_streams(
|
f'"{os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')}"'
|
||||||
cdn_folder: str,
|
]
|
||||||
source_video: str,
|
|
||||||
upscaled_video: str,
|
if sys.platform == 'linux':
|
||||||
interpolated_video: str,
|
cmd = ' '.join(cmd)
|
||||||
interpolated_uhd_video: str | None,
|
|
||||||
aspect_ratio: str = "16:9",
|
try:
|
||||||
):
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
presets = [
|
except subprocess.CalledProcessError as e:
|
||||||
{"name": "720p", "w": "1280", "h": "720", "encoder": "libx264", "preset": "medium", "crf": "22", "input_video": upscaled_video, "out_folder": '720'},
|
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
||||||
{"name": "1080p", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": upscaled_video, "out_folder": '1080'},
|
sys.exit(e.returncode)
|
||||||
{"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'},
|
def encode_streams(
|
||||||
]
|
cdn_folder: str,
|
||||||
|
source_video: str,
|
||||||
# Optional UHD Interpolate encode
|
upscaled_video: str,
|
||||||
if interpolated_uhd_video is not None:
|
interpolated_video: str,
|
||||||
presets.append({"name": "2160p48", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": interpolated_uhd_video, "out_folder": '2160i'})
|
interpolated_uhd_video: str | None,
|
||||||
|
aspect_ratio: str = "16:9",
|
||||||
for preset in presets:
|
):
|
||||||
# Skip already encoded streams
|
presets = [
|
||||||
if os.path.exists(os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')):
|
{"name": "720p", "w": "1280", "h": "720", "encoder": "libx264", "preset": "medium", "crf": "22", "input_video": upscaled_video, "out_folder": '720'},
|
||||||
print(f"Skipped {preset['name']}")
|
{"name": "1080p", "w": "1920", "h": "1080", "encoder": "libsvtav1", "preset": "6", "crf": "26", "input_video": upscaled_video, "out_folder": '1080'},
|
||||||
continue
|
{"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'},
|
||||||
_create_folder(preset, cdn_folder)
|
]
|
||||||
_encode(preset, cdn_folder, source_video, aspect_ratio)
|
|
||||||
_change_chunk_extension(preset, cdn_folder)
|
# Optional UHD Interpolate encode
|
||||||
|
if interpolated_uhd_video is not None:
|
||||||
_extract_subs(source_video, cdn_folder)
|
presets.append({"name": "2160p48", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": interpolated_uhd_video, "out_folder": '2160i'})
|
||||||
_encode_720p_fallback(cdn_folder, source_video, upscaled_video, aspect_ratio)
|
|
||||||
_create_sprites(cdn_folder)
|
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 = [
|
script = [
|
||||||
'import vapoursynth as vs',
|
'import vapoursynth as vs',
|
||||||
'from vsrife import rife',
|
'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.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")',
|
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)',
|
'clip = rife(clip=clip, model="4.25.lite", factor_num=2, factor_den=1)',
|
||||||
@@ -40,43 +43,23 @@ def _interpolate(
|
|||||||
vapoursynth_file: str,
|
vapoursynth_file: str,
|
||||||
interpolate_output: str,
|
interpolate_output: str,
|
||||||
):
|
):
|
||||||
print('Started Interpolation')
|
cmd = [
|
||||||
|
"vspipe",
|
||||||
|
"-c", "y4m",
|
||||||
|
f'"{vapoursynth_file}"',
|
||||||
|
"-", "|",
|
||||||
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
|
"-i", "-",
|
||||||
|
"-c:v", "hevc_nvenc",
|
||||||
|
"-qp", "5",
|
||||||
|
f'"{interpolate_output}"'
|
||||||
|
]
|
||||||
|
|
||||||
|
if sys.platform == 'linux':
|
||||||
|
cmd = ' '.join(cmd)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# First process (vspipe)
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
vspipe = subprocess.Popen(
|
|
||||||
[
|
|
||||||
"vspipe",
|
|
||||||
"-c", "y4m",
|
|
||||||
vapoursynth_file,
|
|
||||||
"-"
|
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE
|
|
||||||
)
|
|
||||||
|
|
||||||
# Second process (ffmpeg), reading from vspipe
|
|
||||||
ffmpeg = subprocess.Popen(
|
|
||||||
[
|
|
||||||
"ffmpeg",
|
|
||||||
"-v", "quiet", "-stats",
|
|
||||||
"-f", "yuv4mpegpipe",
|
|
||||||
"-i", "-",
|
|
||||||
"-c:v", "hevc_nvenc",
|
|
||||||
"-qp", "5",
|
|
||||||
interpolate_output
|
|
||||||
],
|
|
||||||
stdin=vspipe.stdout
|
|
||||||
)
|
|
||||||
|
|
||||||
# Important: allow vspipe to receive SIGPIPE if ffmpeg exits
|
|
||||||
vspipe.stdout.close()
|
|
||||||
|
|
||||||
ffmpeg.wait()
|
|
||||||
vspipe.wait()
|
|
||||||
|
|
||||||
if ffmpeg.returncode != 0:
|
|
||||||
raise subprocess.CalledProcessError(ffmpeg.returncode, "ffmpeg")
|
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
||||||
sys.exit(e.returncode)
|
sys.exit(e.returncode)
|
||||||
|
|||||||
213
utils/upcale.py
213
utils/upcale.py
@@ -1,115 +1,98 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from utils.mediainfo import get_framerate
|
from utils.mediainfo import get_framerate
|
||||||
|
|
||||||
MAX_INPUT_WIDTH = '720'
|
MAX_INPUT_WIDTH = '720'
|
||||||
|
|
||||||
def _re_encode(
|
def _re_encode(
|
||||||
source_video: str,
|
source_video: str,
|
||||||
temp_out_video: str,
|
temp_out_video: str,
|
||||||
input_aspect: str = "16:9"
|
input_aspect: str = "16:9"
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Re-Encodes the source video to avoid nasty video bugs
|
Re-Encodes the source video to avoid nasty video bugs
|
||||||
|
|
||||||
:param source_video: Video Input
|
:param source_video: Video Input
|
||||||
:type source_video: str
|
:type source_video: str
|
||||||
:param input_aspect: Aspect Ratio of Video
|
:param input_aspect: Aspect Ratio of Video
|
||||||
:type input_aspect: str
|
:type input_aspect: str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
fps = get_framerate(source_video)
|
cmd = [
|
||||||
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
vf_filter = f"fps={fps},scale=-1:min({MAX_INPUT_WIDTH}\\,ih)"
|
"-i", f'"{source_video}"',
|
||||||
|
"-c:v", "ffv1",
|
||||||
cmd = [
|
"-level", "3",
|
||||||
"ffmpeg", "-v", "quiet", "-stats",
|
"-vf", f"\"fps={get_framerate(source_video)},scale=-1:\'min({MAX_INPUT_WIDTH},ih)\'\"",
|
||||||
"-i", source_video,
|
"-aspect", input_aspect,
|
||||||
"-c:v", "ffv1",
|
"-pix_fmt", "yuv420p",
|
||||||
"-level", "3",
|
"-color_primaries", "1",
|
||||||
"-vf", vf_filter,
|
"-color_trc", "1",
|
||||||
"-aspect", input_aspect,
|
"-colorspace", "1",
|
||||||
"-pix_fmt", "yuv420p",
|
"-an",
|
||||||
"-color_primaries", "1",
|
"-sn",
|
||||||
"-color_trc", "1",
|
"-map_metadata", "-1",
|
||||||
"-colorspace", "1",
|
f'"{temp_out_video}"'
|
||||||
"-an",
|
]
|
||||||
"-sn",
|
|
||||||
"-map_metadata", "-1",
|
if sys.platform == 'linux':
|
||||||
temp_out_video
|
cmd = ' '.join(cmd)
|
||||||
]
|
|
||||||
|
try:
|
||||||
try:
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
subprocess.run(cmd, check=True)
|
except subprocess.CalledProcessError as e:
|
||||||
except subprocess.CalledProcessError as e:
|
print(f"\nffmpeg failed with error code {e.returncode} at _re_encode()", file=sys.stderr)
|
||||||
print(f"\nffmpeg failed with error code {e.returncode} at _re_encode()", file=sys.stderr)
|
sys.exit(e.returncode)
|
||||||
sys.exit(e.returncode)
|
|
||||||
|
def _upscale(
|
||||||
def _upscale(
|
upscale_output: str,
|
||||||
upscale_output: str,
|
input_aspect: str = "16:9"
|
||||||
input_aspect: str = "16:9"
|
):
|
||||||
):
|
print('Started Upscale')
|
||||||
print('Started Upscale')
|
|
||||||
|
vapoursynth_script = os.path.join('utils', 'vs-realesrgan.vpy')
|
||||||
vapoursynth_script = os.path.join('utils', 'vs-realesrgan.vpy')
|
|
||||||
|
cmd = [
|
||||||
try:
|
"vspipe",
|
||||||
# First process (vspipe)
|
"-c", "y4m",
|
||||||
vspipe = subprocess.Popen(
|
f"\"{vapoursynth_script}\"",
|
||||||
[
|
"-", # Video output to pipe
|
||||||
"vspipe",
|
"|", # Pipe
|
||||||
"-c", "y4m",
|
"ffmpeg", "-v", "quiet", "-stats",
|
||||||
vapoursynth_script,
|
"-f", "yuv4mpegpipe",
|
||||||
"-"
|
"-i", "-", # Pipe Video Input
|
||||||
],
|
"-c:v", "hevc_nvenc",
|
||||||
stdout=subprocess.PIPE
|
"-qp", "5",
|
||||||
)
|
"-aspect", input_aspect,
|
||||||
|
f"\"{upscale_output}\""
|
||||||
# Second process (ffmpeg), reading from vspipe
|
]
|
||||||
ffmpeg = subprocess.Popen(
|
|
||||||
[
|
if sys.platform == 'linux':
|
||||||
"ffmpeg",
|
cmd = ' '.join(cmd)
|
||||||
"-v", "quiet", "-stats",
|
|
||||||
"-f", "yuv4mpegpipe",
|
try:
|
||||||
"-i", "-",
|
subprocess.run(cmd, shell=True, check=True)
|
||||||
"-c:v", "hevc_nvenc",
|
except subprocess.CalledProcessError as e:
|
||||||
"-qp", "5",
|
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
||||||
"-aspect", input_aspect,
|
sys.exit(e.returncode)
|
||||||
upscale_output
|
|
||||||
],
|
|
||||||
stdin=vspipe.stdout
|
def upscale(
|
||||||
)
|
source_video: str,
|
||||||
|
upscaled_video_output: str,
|
||||||
# Important: allow vspipe to receive SIGPIPE if ffmpeg exits
|
input_aspect: str,
|
||||||
vspipe.stdout.close()
|
):
|
||||||
|
if os.path.exists(upscaled_video_output):
|
||||||
ffmpeg.wait()
|
print('Skipped Upscale')
|
||||||
vspipe.wait()
|
return
|
||||||
|
|
||||||
if ffmpeg.returncode != 0:
|
temp_out_video = os.path.join('1-Temp', 'source.mkv')
|
||||||
raise subprocess.CalledProcessError(ffmpeg.returncode, "ffmpeg")
|
|
||||||
|
_re_encode(source_video, temp_out_video, input_aspect)
|
||||||
except subprocess.CalledProcessError as e:
|
_upscale(upscaled_video_output, input_aspect)
|
||||||
print(f"\nffmpeg failed with error code {e.returncode}", file=sys.stderr)
|
|
||||||
sys.exit(e.returncode)
|
# Remove Temp Files
|
||||||
|
os.remove(temp_out_video)
|
||||||
|
os.remove(f'{temp_out_video}.ffindex')
|
||||||
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