Compare commits

3 Commits

Author SHA1 Message Date
w33b 630ac6dd2d Fix running out of vram on linux 2026-03-05 23:07:44 +01:00
w33b 8fd85322ca Escape "$" on linux for DASH encode 2026-03-05 23:05:58 +01:00
w33b 15e19fa056 CMD list handled differently when shell=True 2026-03-05 23:03:59 +01:00
6 changed files with 357 additions and 410 deletions
-21
View File
@@ -1,21 +0,0 @@
#!/bin/bash
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
# Change the directory to where the script is
cd "$SCRIPT_DIR"
# Path to your virtual environment
VENV_PATH="$SCRIPT_DIR/.venv"
source "$VENV_PATH/bin/activate"
# Path to your Python script
SCRIPT_PATH="$SCRIPT_DIR/Start.py"
# Run the Python script
python "$SCRIPT_PATH"
# Deactivate the virtual environment
deactivate
read -p "Press Enter to exit..."
+20 -17
View File
@@ -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 x265") 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", "libx265", "-c:v", "libsvtav1",
"-crf", preset['crf'], "-crf", preset['crf'],
"-preset", "slow", "-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", "128k", "-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": "22", "name": "[1080p-x265]", "input_video": upscaled_video}, {"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1]", "input_video": upscaled_video},
{"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-x265][48fps]", "input_video": interpolated_video}, {"w": "1920", "h": "1080", "crf": "22", "name": "[1080p-AV1][48fps]", "input_video": interpolated_video},
{"w": "3840", "h": "2160", "crf": "24", "name": "[2160p-x265]", "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": "24", "name": "[2160p-x265][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"
@@ -76,8 +79,8 @@ def encode_downloads(
mux_out = os.path.join('2-Out', folder_name, 'Muxed', file_name) mux_out = os.path.join('2-Out', folder_name, 'Muxed', file_name)
if os.path.exists(mux_out): if os.path.exists(mux_out):
print(f'Skipped {preset['h']}p x265 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)
+33 -21
View File
@@ -52,8 +52,8 @@ def _encode_720p_fallback(
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",
@@ -65,15 +65,19 @@ def _encode_720p_fallback(
"-sn", "-sn",
"-map_metadata", "-1", "-map_metadata", "-1",
"-movflags", "+faststart", "-movflags", "+faststart",
output f'"{output}"'
] ]
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)
def _change_chunk_extension( def _change_chunk_extension(
preset: dict[str, str], preset: dict[str, str],
cdn_folder: str, cdn_folder: str,
@@ -134,26 +138,31 @@ def _encode(
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 "-map", "0:v:0", # Video from Upscale
"-map", "1:a:0", # Audio from Source "-map", "1:a:0", # Audio from Source
"-c:v", preset['encoder'], "-c:v", preset['encoder'],
"-preset", preset['preset'], "-preset", preset['preset'],
"-crf", str(preset['crf']), "-crf", preset['crf'],
"-pix_fmt", "yuv420p", # 8bit to increase decode performance "-pix_fmt", "yuv420p", # 8bit to increase decode performance
"-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1", "-vf", f"scale={preset['w']}:{preset['h']},setsar=1:1",
"-aspect", aspect_ratio, "-aspect", aspect_ratio,
] ]
if preset["encoder"] == "libx264": if preset["encoder"] == "libx264":
cmd += ["-x264-params", "keyint=24:min-keyint=24:scenecut=0"] cmd += ["-x264-params", f"keyint=24:min-keyint=24:scenecut=0"]
cmd += ["-c:a", "aac", "-b:a", "160k"] cmd += ["-c:a", "aac", "-b:a", "128k"]
elif preset["encoder"] == "libsvtav1": elif preset["encoder"] == "libsvtav1":
cmd += ["-svtav1-params", f"keyint={keyframe_interval}s:fast-decode=1:tune=1"] cmd += ["-svtav1-params", f"keyint={keyframe_interval}s,fast-decode=1,tune=0"]
cmd += ["-c:a", "aac", "-b:a", "160k"] cmd += ["-c:a", "libopus", "-b:a", "128k"]
output_path = os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd') 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 += [ cmd += [
"-ac", "2", "-ac", "2",
@@ -161,15 +170,18 @@ def _encode(
"-map_metadata", "-1", # Get rid of metadata which might be incorrect "-map_metadata", "-1", # Get rid of metadata which might be incorrect
"-use_template", "1", # Don't list every segment url, use template instead "-use_template", "1", # Don't list every segment url, use template instead
"-use_timeline", "1", # Make sure segment timing is always correct "-use_timeline", "1", # Make sure segment timing is always correct
"-init_seg_name", "chunks/init-stream$RepresentationID$.webm", "-init_seg_name", init_seg_name, # Init segment
"-media_seg_name", "chunks/chunk-stream$RepresentationID$-$Number%05d$.webm", "-media_seg_name", media_seg_name, # Media segments
"-seg_duration", str(segment_duration), "-seg_duration", str(segment_duration), # DASH segment duration
"-f", "dash", "-f", "dash",
output_path f'"{os.path.join(cdn_folder, preset['out_folder'], 'manifest.mpd')}"'
] ]
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)
@@ -184,14 +196,14 @@ def encode_streams(
): ):
presets = [ presets = [
{"name": "720p", "w": "1280", "h": "720", "encoder": "libx264", "preset": "medium", "crf": "22", "input_video": upscaled_video, "out_folder": '720'}, {"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": "28", "input_video": upscaled_video, "out_folder": '1080'}, {"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": "28", "input_video": interpolated_video, "out_folder": '1080i'}, {"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": "32", "input_video": upscaled_video, "out_folder": '2160'}, {"name": "2160", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "28", "input_video": upscaled_video, "out_folder": '2160'},
] ]
# Optional UHD Interpolate encode # Optional UHD Interpolate encode
if interpolated_uhd_video is not None: if interpolated_uhd_video is not None:
presets.append({"name": "2160p48", "w": "3840", "h": "2160", "encoder": "libsvtav1", "preset": "6", "crf": "32", "input_video": interpolated_uhd_video, "out_folder": '2160i'}) 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: for preset in presets:
# Skip already encoded streams # Skip already encoded streams
+13 -30
View File
@@ -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 = [
try:
# First process (vspipe)
vspipe = subprocess.Popen(
[
"vspipe", "vspipe",
"-c", "y4m", "-c", "y4m",
vapoursynth_file, f'"{vapoursynth_file}"',
"-" "-", "|",
], "ffmpeg", "-v", "quiet", "-stats",
stdout=subprocess.PIPE
)
# Second process (ffmpeg), reading from vspipe
ffmpeg = subprocess.Popen(
[
"ffmpeg",
"-v", "quiet", "-stats",
"-f", "yuv4mpegpipe",
"-i", "-", "-i", "-",
"-c:v", "hevc_nvenc", "-c:v", "hevc_nvenc",
"-qp", "5", "-qp", "5",
interpolate_output f'"{interpolate_output}"'
], ]
stdin=vspipe.stdout
)
# Important: allow vspipe to receive SIGPIPE if ffmpeg exits if sys.platform == 'linux':
vspipe.stdout.close() cmd = ' '.join(cmd)
ffmpeg.wait()
vspipe.wait()
if ffmpeg.returncode != 0:
raise subprocess.CalledProcessError(ffmpeg.returncode, "ffmpeg")
try:
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)
-6
View File
@@ -7,12 +7,6 @@ def get_aspect_ratio(video_input: str) -> str:
print('Detected Aspect Ratio : ' + aspect_ratio[0]) print('Detected Aspect Ratio : ' + aspect_ratio[0])
return aspect_ratio[0] return aspect_ratio[0]
def get_video_resolution(video_input: str) -> tuple[int, int]:
media_info = MediaInfo.parse(video_input)
video_track = media_info.video_tracks[0]
print('Detected Resolution : ' + str(video_track.width) + 'x' + str(video_track.height))
return (video_track.width, video_track.height)
def get_framerate(video_input: str) -> str: def get_framerate(video_input: str) -> str:
media_info = MediaInfo.parse(video_input) media_info = MediaInfo.parse(video_input)
video_track = media_info.video_tracks[0] video_track = media_info.video_tracks[0]
+27 -51
View File
@@ -2,8 +2,9 @@ import os
import sys import sys
import subprocess import subprocess
from utils.mediainfo import get_framerate, get_video_resolution from utils.mediainfo import get_framerate
MAX_INPUT_WIDTH = '720'
def _re_encode( def _re_encode(
source_video: str, source_video: str,
@@ -19,28 +20,12 @@ def _re_encode(
:type input_aspect: str :type input_aspect: str
""" """
fps = get_framerate(source_video)
resolution = get_video_resolution(source_video)
scale = "720:480"
if input_aspect == "16:9" and resolution[1] == 480:
scale = "854:480"
elif input_aspect == "16:9" and resolution[1] == 540:
scale = "960:540"
elif input_aspect == "16:9" and resolution[1] >= 720:
scale = "1280:720"
print(f"Scaling at : {scale}")
vf_filter = f"fps={fps},scale={scale},setsar=1"
cmd = [ cmd = [
"ffmpeg", "-v", "quiet", "-stats", "ffmpeg", "-v", "quiet", "-stats",
"-i", source_video, "-i", f'"{source_video}"',
"-c:v", "ffv1", "-c:v", "ffv1",
"-level", "3", "-level", "3",
"-vf", vf_filter, "-vf", f"\"fps={get_framerate(source_video)},scale=-1:\'min({MAX_INPUT_WIDTH},ih)\'\"",
"-aspect", input_aspect, "-aspect", input_aspect,
"-pix_fmt", "yuv420p", "-pix_fmt", "yuv420p",
"-color_primaries", "1", "-color_primaries", "1",
@@ -49,55 +34,46 @@ def _re_encode(
"-an", "-an",
"-sn", "-sn",
"-map_metadata", "-1", "-map_metadata", "-1",
temp_out_video f'"{temp_out_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} 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(upscale_output: str): def _upscale(
upscale_output: str,
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')
try: cmd = [
# First process (vspipe)
vspipe = subprocess.Popen(
[
"vspipe", "vspipe",
"-c", "y4m", "-c", "y4m",
vapoursynth_script, f"\"{vapoursynth_script}\"",
"-" "-", # Video output to pipe
], "|", # Pipe
stdout=subprocess.PIPE "ffmpeg", "-v", "quiet", "-stats",
)
# Second process (ffmpeg), reading from vspipe
ffmpeg = subprocess.Popen(
[
"ffmpeg",
"-v", "quiet", "-stats",
"-f", "yuv4mpegpipe", "-f", "yuv4mpegpipe",
"-i", "-", "-i", "-", # Pipe Video Input
"-c:v", "hevc_nvenc", "-c:v", "hevc_nvenc",
"-qp", "5", "-qp", "5",
upscale_output "-aspect", input_aspect,
], f"\"{upscale_output}\""
stdin=vspipe.stdout ]
)
# Important: allow vspipe to receive SIGPIPE if ffmpeg exits if sys.platform == 'linux':
vspipe.stdout.close() cmd = ' '.join(cmd)
ffmpeg.wait()
vspipe.wait()
if ffmpeg.returncode != 0:
raise subprocess.CalledProcessError(ffmpeg.returncode, "ffmpeg")
try:
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)
@@ -115,7 +91,7 @@ def upscale(
temp_out_video = os.path.join('1-Temp', 'source.mkv') temp_out_video = os.path.join('1-Temp', 'source.mkv')
_re_encode(source_video, temp_out_video, input_aspect) _re_encode(source_video, temp_out_video, input_aspect)
_upscale(upscaled_video_output) _upscale(upscaled_video_output, input_aspect)
# Remove Temp Files # Remove Temp Files
os.remove(temp_out_video) os.remove(temp_out_video)