# 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): """ 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), "-vf",f"scale={scale_width}:{scale_height}:flags={filter_flags}:force_original_aspect_ratio=decrease", "-map","0:v","-map","0:a","-map","0:s?", "-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"] else: # Re-encode with target bitrate cmd += [ f"-c:a:{i}", codec, f"-b:a:{i}", str(br), f"-ac:{i}", str(output_channels), f"-channel_layout:a:{i}", "5.1" if output_channels == 6 else "stereo" ] cmd += ["-c:s","copy",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