1
import subprocess
from pathlib import Path
# Configuration
input_folder = Path(r"E:\Staging")
output_folder = Path(r"Y:\Encodes")
ffmpeg_path = Path(r"C:\Programs\FFMPEG\bin\ffmpeg.exe")
# Ensure output directory exists
output_folder.mkdir(parents=True, exist_ok=True)
# Loop through .mkv files in input directory
for input_file in input_folder.glob("*.mkv"):
filename = input_file.stem
output_file = output_folder / f"{filename}.mkv"
if output_file.exists():
print(f"[Skip] {filename} already encoded. Skipping.\n")
continue
print(f"[Start] {filename}")
cmd = [
str(ffmpeg_path),
"-hide_banner",
"-loglevel", "quiet",
"-stats",
"-hwaccel", "cuda",
#"-hwaccel_output_format", "cuda",
"-i", str(input_file),
# Include only specific streams explicitly
"-map", "0:v:0", # First video stream
"-map", "0:a", # All audio streams
"-map", "0:s?", # All subtitle streams (optional - won't fail if none)
# Video encoding
"-vf", "scale=w=-1:h=720:flags=lanczos+accurate_rnd",
"-c:v", "hevc_nvenc",
"-preset", "p7",
"-rc", "vbr",
"-tune", "hq",
"-multipass", "fullres",
"-cq", "19",
"-maxrate", "3500k",
"-bufsize", "7000k",
"-bf", "4",
"-b_ref_mode", "middle",
"-nonref_p", "1",
"-strict_gop", "1",
"-g", "250",
"-keyint_min", "25",
"-pix_fmt", "p010le",
"-profile:v", "main10",
"-spatial-aq", "1",
"-temporal-aq", "1",
"-aq-strength", "8",
"-refs", "5",
"-rc-lookahead", "32",
"-colorspace", "bt709",
"-color_primaries", "bt709",
"-color_trc", "bt709",
# Audio encoding for all streams
"-c:a", "libopus",
"-b:a", "96k",
# Subtitle stream copy
"-c:s", "copy",
"-y", str(output_file)
]
try:
subprocess.run(cmd, check=True)
print(f"[Finish] {filename}")
print(f"Output: {output_file}\n")
except subprocess.CalledProcessError as e:
print(f"[Error] Failed to encode: {input_file}")
print(f"Reason: {e}\n")
print("All encodes complete!")
For immediate assistance, please email our customer support: [email protected]