CPU transcoding works, quicksync needs help
This commit is contained in:
@@ -16,3 +16,28 @@ ffmpeg -y -i input.mp4 \
|
||||
* -g 72 -keyint_min 72: Forces a keyframe exactly every 72 frames. At 24fps, this yields an exact 3-second fragment duration.
|
||||
* -sc_threshold 0: Disables adaptive scene-change keyframes. Without this, your streams will have misaligned keyframes, and Bento4 will fail to package them as a single adaptive HLS stream.
|
||||
* -an and -vn: Separates the audio from the video files. Bento4 handles multiplexing and packaging best when audio and video tracks are fed as distinct source files.
|
||||
|
||||
|
||||
# Configuring for quicksync
|
||||
|
||||
1:N transcoding workflow that leverages the GPU hardware filter scale_qsv. This reads the input frame once, scales it on the Intel GPU, and encodes both outputs simultaneously.
|
||||
|
||||
```bash
|
||||
ffmpeg -hwaccel qsv -c:v h264_qsv -i input.mp4 \
|
||||
-filter_complex "[0:v]split=2[s1][s2]; \
|
||||
[s1]scale_qsv=w=1280:h=720[v720]; \
|
||||
[s2]scale_qsv=w=854:h=480[v480]" \
|
||||
-map "[v720]" -c:v h264_qsv -b:v 2500k -maxrate 2500k -bufsize 5000k \
|
||||
-g 60 -keyint_min 60 -sc_threshold 0 -forced-idr 1 -an video_720p.mp4 \
|
||||
-map "[v480]" -c:v h264_qsv -b:v 1000k -maxrate 1000k -bufsize 2000k \
|
||||
-g 60 -keyint_min 60 -sc_threshold 0 -forced-idr 1 -an video_480p.mp4 \
|
||||
-map 0:a? -c:a aac -b:a 128k audio.mp4
|
||||
```
|
||||
|
||||
Key Argument Explanations:
|
||||
* -hwaccel qsv -c:v h264_qsv: Invokes hardware decoding and initialization.
|
||||
* scale_qsv=w=...:h=...: Tells the Intel GPU to handle the video resizing directly inside the Quick Sync processing pipeline.
|
||||
* -g 60 -keyint_min 60: Dictates the GOP size (assuming a 30fps input video, a GOP of 60 forces a keyframe exactly every 2 seconds).
|
||||
* -sc_threshold 0: Disables scene change detection to ensure keyframes are purely fixed.
|
||||
* -forced-idr 1: Forces the QSV encoder to use actual IDR frames instead of basic I-frames at the GOP boundary, which is necessary for fragmenting tools.
|
||||
* -an: Strips audio out of the video streams so Bento4 can package them as clean, isolated tracks.
|
||||
|
||||
+13
-13
@@ -3,29 +3,29 @@
|
||||
|
||||
# Default encoder (can be overridden with --encoder CLI flag)
|
||||
# Options: cpu, quicksync, nvidia
|
||||
encoder: quicksync
|
||||
encoder: cpu
|
||||
|
||||
# Quality profiles for HLS encoding
|
||||
# Each profile generates a separate variant stream
|
||||
profiles:
|
||||
# Full HD
|
||||
# full_hd:
|
||||
# 1080p5000:
|
||||
# name: "Full HD"
|
||||
# resolution: "1920x1080"
|
||||
# bitrate: "5000k"
|
||||
# codec: "h264"
|
||||
# fps: 30
|
||||
#
|
||||
# # HD Ready
|
||||
# hd:
|
||||
# name: "HD Ready"
|
||||
# resolution: "1280x720"
|
||||
# bitrate: "2500k"
|
||||
# codec: "h264"
|
||||
# fps: 30
|
||||
|
||||
# HD Ready
|
||||
720p2500:
|
||||
name: "HD Ready"
|
||||
resolution: "1280x720"
|
||||
bitrate: "2500k"
|
||||
codec: "h264"
|
||||
fps: 30
|
||||
|
||||
# Standard Definition
|
||||
sd:
|
||||
480p1200:
|
||||
name: "Standard Definition"
|
||||
resolution: "854x480"
|
||||
bitrate: "1200k"
|
||||
@@ -33,9 +33,9 @@ profiles:
|
||||
fps: 30
|
||||
|
||||
# Mobile
|
||||
mobile:
|
||||
360p600:
|
||||
name: "Mobile"
|
||||
resolution: "640x360"
|
||||
bitrate: "600k"
|
||||
codec: "h264"
|
||||
fps: 24
|
||||
fps: 30
|
||||
|
||||
+72
-14
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user