155 lines
6.1 KiB
Python
155 lines
6.1 KiB
Python
# core/encode_engine.py
|
|
"""FFmpeg encoding engine with comprehensive logging."""
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from core.audio_handler import get_audio_streams, choose_audio_bitrate
|
|
from core.logger_helper import setup_logger
|
|
|
|
logger = setup_logger(Path(__file__).parent.parent / "logs")
|
|
|
|
|
|
def run_ffmpeg(input_file: Path, output_file: Path, cq: int, scale_width: int, scale_height: int,
|
|
src_width: int, src_height: int, filter_flags: str, audio_config: dict,
|
|
method: str, bitrate_config: dict, subtitle_file: Path = None, audio_language: str = None):
|
|
"""
|
|
Run FFmpeg encode with comprehensive logging.
|
|
|
|
Returns tuple: (orig_size, out_size, reduction_ratio)
|
|
"""
|
|
streams = get_audio_streams(input_file)
|
|
|
|
# Log comprehensive encode settings
|
|
header = f"\n🧩 ENCODE SETTINGS"
|
|
logger.info(header)
|
|
print(" ")
|
|
|
|
logger.info(f" Video:")
|
|
logger.info(f" • Source Resolution: {src_width}x{src_height}")
|
|
logger.info(f" • Target Resolution: {scale_width}x{scale_height}")
|
|
logger.info(f" • Encoder: av1_nvenc (preset p1, pix_fmt p010le)")
|
|
logger.info(f" • Scale Filter: {filter_flags}")
|
|
logger.info(f" • Encode Method: {method}")
|
|
if method == "CQ":
|
|
logger.info(f" • CQ Value: {cq}")
|
|
else:
|
|
res_key = "1080" if scale_height >= 1080 or scale_width >= 1920 else "720"
|
|
vb = bitrate_config.get(f"bitrate_{res_key}", "900k")
|
|
maxrate = bitrate_config.get(f"maxrate_{res_key}", "1250k")
|
|
logger.info(f" • Bitrate: {vb}, Max: {maxrate}")
|
|
|
|
logger.info(f" Audio Streams ({len(streams)} detected):")
|
|
print(" ")
|
|
|
|
for (index, channels, avg_bitrate, src_lang, meta_bitrate) in streams:
|
|
# Normalize to 2ch or 6ch output
|
|
is_1080_class = scale_height >= 1080 or scale_width >= 1920
|
|
output_channels = 6 if is_1080_class and channels >= 6 else 2
|
|
codec, br = choose_audio_bitrate(output_channels, avg_bitrate, audio_config, is_1080_class)
|
|
|
|
if codec == "copy":
|
|
action = "COPY (preserve)"
|
|
bitrate_display = f"{avg_bitrate}kbps"
|
|
else:
|
|
action = "ENCODE"
|
|
bitrate_display = f"{br/1000:.0f}kbps"
|
|
|
|
line = f" - Stream #{index}: {channels}ch→{output_channels}ch | Lang: {src_lang} | Detected: {avg_bitrate}kbps | Action: {action} | Target: {bitrate_display}"
|
|
print(line)
|
|
logger.info(line)
|
|
|
|
cmd = ["ffmpeg","-y","-i",str(input_file)]
|
|
|
|
# Add subtitle input if present
|
|
if subtitle_file:
|
|
cmd.extend(["-i", str(subtitle_file)])
|
|
|
|
cmd.extend([
|
|
"-vf",f"scale={scale_width}:{scale_height}:flags={filter_flags}:force_original_aspect_ratio=decrease",
|
|
"-map","0:v","-map","0:a"])
|
|
|
|
# Add subtitle mapping if present
|
|
if subtitle_file:
|
|
cmd.extend(["-map", "1:s"])
|
|
else:
|
|
cmd.extend(["-map", "0:s?"])
|
|
|
|
cmd.extend([
|
|
"-c:v","av1_nvenc","-preset","p1","-pix_fmt","p010le"])
|
|
|
|
if method=="CQ":
|
|
cmd += ["-cq", str(cq)]
|
|
else:
|
|
# Use bitrate config (fallback mode)
|
|
res_key = "1080" if scale_height >= 1080 or scale_width >= 1920 else "720"
|
|
vb = bitrate_config.get(f"bitrate_{res_key}", "900k")
|
|
maxrate = bitrate_config.get(f"maxrate_{res_key}", "1250k")
|
|
bufsize = bitrate_config.get(f"bufsize_{res_key}", "1800k")
|
|
cmd += ["-b:v", vb, "-maxrate", maxrate, "-bufsize", bufsize]
|
|
|
|
for i, (index, channels, avg_bitrate, src_lang, meta_bitrate) in enumerate(streams):
|
|
# Normalize to 2ch or 6ch output
|
|
is_1080_class = scale_height >= 1080 or scale_width >= 1920
|
|
output_channels = 6 if is_1080_class and channels >= 6 else 2
|
|
codec, br = choose_audio_bitrate(output_channels, avg_bitrate, audio_config, is_1080_class)
|
|
|
|
if codec == "copy":
|
|
# Preserve original audio
|
|
cmd += [f"-c:a:{i}", "copy"]
|
|
# Only add language metadata if explicitly provided
|
|
if audio_language:
|
|
cmd += [f"-metadata:s:a:{i}", f"language={audio_language}"]
|
|
else:
|
|
# Re-encode with target bitrate
|
|
# EAC3 for multichannel, AAC for stereo
|
|
if codec == "eac3":
|
|
# Enhanced AC-3 (5.1 surround)
|
|
cmd += [
|
|
f"-c:a:{i}", "eac3",
|
|
f"-b:a:{i}", str(br),
|
|
f"-ac:{i}", str(output_channels),
|
|
f"-channel_layout:a:{i}", "5.1"
|
|
]
|
|
else:
|
|
# AAC (stereo)
|
|
cmd += [
|
|
f"-c:a:{i}", "aac",
|
|
f"-b:a:{i}", str(br),
|
|
f"-ac:{i}", str(output_channels),
|
|
f"-channel_layout:a:{i}", "stereo"
|
|
]
|
|
# Only add language metadata if explicitly provided
|
|
if audio_language:
|
|
cmd += [f"-metadata:s:a:{i}", f"language={audio_language}"]
|
|
|
|
# Add subtitle codec and metadata if subtitles are present
|
|
if subtitle_file:
|
|
cmd += ["-c:s", "srt", "-metadata:s:s:0", "language=eng"]
|
|
else:
|
|
cmd += ["-c:s", "copy"]
|
|
|
|
cmd += [str(output_file)]
|
|
|
|
print(f"\n🎬 Running {method} encode: {output_file.name}")
|
|
logger.info(f"Running {method} encode: {output_file.name}")
|
|
|
|
subprocess.run(cmd, check=True)
|
|
|
|
orig_size = input_file.stat().st_size
|
|
out_size = output_file.stat().st_size
|
|
reduction_ratio = out_size / orig_size
|
|
|
|
# Log comprehensive results
|
|
logger.info(f"\n📊 ENCODE RESULTS:")
|
|
logger.info(f" Original Size: {orig_size/1e6:.2f} MB")
|
|
logger.info(f" Encoded Size: {out_size/1e6:.2f} MB")
|
|
logger.info(f" Reduction: {reduction_ratio:.1%} of original ({(1-reduction_ratio):.1%} saved)")
|
|
logger.info(f" Resolution: {src_width}x{src_height} → {scale_width}x{scale_height}")
|
|
logger.info(f" Audio Streams: {len(streams)} streams processed")
|
|
|
|
msg = f"📦 Original: {orig_size/1e6:.2f} MB → Encoded: {out_size/1e6:.2f} MB ({reduction_ratio:.1%} of original)"
|
|
print(msg)
|
|
|
|
return orig_size, out_size, reduction_ratio
|