CPU transcoding works, quicksync needs help

This commit is contained in:
Shaun Kruger
2026-09-21 22:46:10 -06:00
parent 77a5a64a56
commit f637e34ea1
3 changed files with 110 additions and 27 deletions
+72 -14
View File
@@ -100,6 +100,16 @@ def encoder_available(encoder_name: str) -> bool:
return codec in combined
def is_hevc_input(input_asset: str) -> bool:
process = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=codec_name", "-of", "default=noprint_wrappers=1:nokey=1:noesc=1", input_asset],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=False,
)
return "hevc" in (process.stdout or "").lower()
def build_ffmpeg_command(input_asset: str, profile_config: dict, output_dir: Path, selected_encoder: str):
if selected_encoder not in ENCODER_MAP:
raise ValueError(f"Unsupported encoder '{selected_encoder}'. Options: {', '.join(ENCODER_MAP.keys())}")
@@ -109,9 +119,13 @@ def build_ffmpeg_command(input_asset: str, profile_config: dict, output_dir: Pat
if "profiles" not in profile_config:
raise ValueError("Profile YAML is missing a 'profiles' section")
# pix_fmt = "p010le" if selected_encoder == "quicksync" else "yuv420p"
variant_profiles = profile_config["profiles"]
video_outputs = []
ffmpeg_args = ["ffmpeg", "-y", "-i", input_asset]
if selected_encoder == "quicksync":
ffmpeg_args = ["ffmpeg", "-y", "-hwaccel", "qsv", "-c:v", "h264_qsv", "-i", input_asset]
for profile_key, profile in variant_profiles.items():
profile = dict(profile)
@@ -138,6 +152,8 @@ def build_ffmpeg_command(input_asset: str, profile_config: dict, output_dir: Pat
"bitrate": bitrate,
"maxrate": maxrate,
"bufsize": bufsize,
"width": width,
"size_key": f"v{height}",
"height": height,
"fps": fps,
})
@@ -145,32 +161,64 @@ def build_ffmpeg_command(input_asset: str, profile_config: dict, output_dir: Pat
if not video_outputs:
raise ValueError("No video profiles were found in the configuration")
for _ in video_outputs:
ffmpeg_args.extend(["-map", "0:v:0"])
ffmpeg_args.extend(["-map", "0:a:0"])
ffmpeg_args.extend(["-g", "72", "-keyint_min", "72", "-sc_threshold", "0"])
output_sizes = dict()
# Create complex filter map and deduplicate scale sizes using size_key
for output in video_outputs:
size_key = output["size_key"]
output_sizes[size_key] = {
"width": output["width"],
"height": output["height"],
"scalestring": f"scale=w={output['width']}:h={output['height']}[{size_key}]",
}
if selected_encoder == "quicksync":
output_sizes[size_key]["scalestring"] = f"scale_qsv=w={output['width']}:h={output['height']}[{size_key}]"
complex_filter = "[0:v]split=%d" % len(video_outputs)
complex_filter_scales = list()
complex_filter_count = 0
for size_key, size_info in output_sizes.items():
complex_filter_count += 1
size_ref = f"[s{complex_filter_count}]"
complex_filter += size_ref
complex_filter_scales.append(size_ref + size_info["scalestring"])
complex_filter += ";" + ";".join(complex_filter_scales)
ffmpeg_args.extend(["-filter_complex", complex_filter])
print(repr(complex_filter_scales))
print(repr(complex_filter))
# for _ in video_outputs:
# ffmpeg_args.extend(["-map", "0:v:0"])
# ffmpeg_args.extend(["-map", "0:a:0"])
# ffmpeg_args.extend(["-g", "72", "-keyint_min", "72", "-sc_threshold", "0"])
for index, profile in enumerate(video_outputs):
ffmpeg_args.extend([
"-c:v:%d" % index,
"-map", f"[{profile['size_key']}]", # Map the scaled video stream
"-c:v",
profile["codec"],
"-b:v:%d" % index,
"-b:v",
bitrate_to_string(parse_bitrate(profile["bitrate"])),
"-maxrate:v:%d" % index,
"-maxrate",
bitrate_to_string(profile["maxrate"]),
"-bufsize:v:%d" % index,
"-bufsize",
bitrate_to_string(profile["bufsize"]),
"-pix_fmt:v:%d" % index,
"yuv420p",
"-r:v:%d" % index,
str(profile["fps"]),
"-filter:v:%d" % index,
f"scale=-2:{profile['height']}",
"-g", "72", "-keyint_min", "72", "-sc_threshold", "0",
# "-pix_fmt:v:%d" % index,
# pix_fmt,
# "-r:v:%d" % index,
# str(profile["fps"]),
# "-filter:v:%d" % index,
# f"scale={profile['width']}:{profile['height']}",
"-forced-idr", "1",
"-an",
str(output_dir / profile["output_name"]),
])
ffmpeg_args.extend([
"-map", "0:a?",
"-c:a",
"aac",
"-b:a",
@@ -197,6 +245,14 @@ def parse_args():
return parser.parse_args()
def preexec_nice():
try:
import psutil
psutil.Process().nice(10) # Lower the priority of the ffmpeg process
except ImportError:
print("Warning: psutil is not installed; ffmpeg will run at normal priority.", file=sys.stderr)
def run_ffmpeg(command, output_dir):
process = subprocess.Popen(
command,
@@ -205,6 +261,7 @@ def run_ffmpeg(command, output_dir):
text=True,
bufsize=1,
universal_newlines=True,
preexec_fn=preexec_nice, # Ensure ffmpeg runs at lower priority
)
if process.stdout is None:
@@ -281,6 +338,7 @@ def main():
return_code, merged_output = run_ffmpeg(ffmpeg_command, output_dir)
if return_code != 0 and args.encoder is None and requested_encoder != "cpu" and configured_encoder != "cpu":
sys.exit(1) # If the user explicitly requested a non-CPU encoder, don't retry with CPU
print(
f"Warning: encoder '{requested_encoder}' is not usable with this asset; retrying with CPU.",
file=sys.stderr,