import os import subprocess from pathlib import Path import json import logging from datetime import datetime import shutil import time # ANSI color codes class Colors: PURPLE = '\033[95m' BLUE = '\033[94m' GREEN = '\033[92m' YELLOW = '\033[93m' RED = '\033[91m' ENDC = '\033[0m' def get_gpu_selection(): while True: print(f"\n{Colors.BLUE}Select GPU slot:{Colors.ENDC}") print("0 - First GPU") print("1 - Second GPU") print("2 - Third GPU") gpu = input(f"{Colors.YELLOW}Enter GPU number (0-2):{Colors.ENDC} ").strip() if gpu in ['0', '1', '2']: return gpu print(f"{Colors.RED}Invalid selection. Please try again.{Colors.ENDC}") def get_bitrate(): while True: print(f"\n{Colors.BLUE}Select video bitrate:{Colors.ENDC}") print("Common values: 5000k (5 Mbps), 8000k (8 Mbps), 10000k (10 Mbps), 15000k (15 Mbps)") bitrate = input(f"{Colors.YELLOW}Enter bitrate in kbps (e.g., 10000 for 10 Mbps):{Colors.ENDC} ").strip() try: bitrate_val = int(bitrate) if bitrate_val > 0: return bitrate_val print(f"{Colors.RED}Bitrate must be positive. Please try again.{Colors.ENDC}") except ValueError: print(f"{Colors.RED}Invalid input. Please enter a number.{Colors.ENDC}") # Custom file handler that silently handles I/O errors (for network shares) class SafeFileHandler(logging.FileHandler): """File handler that silently handles I/O errors during flush""" def flush(self): """Override flush to silently handle I/O errors""" try: super().flush() except (OSError, IOError): pass except Exception: pass def emit(self, record): """Override emit to handle errors gracefully""" try: super().emit(record) except (OSError, IOError): self.handleError(record) except Exception: self.handleError(record) def handleError(self, record): """Override to prevent error messages from being printed""" pass # Set up logging log_dir = "logs" os.makedirs(log_dir, exist_ok=True) log_file = os.path.join(log_dir, f"encode_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log") handler = SafeFileHandler(log_file, mode='w', encoding='utf-8') handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(handler) logger.handlers = [handler] def get_file_info(input_file): cmd = [ 'ffprobe', '-v', 'error', '-show_entries', 'format=duration,size:stream=codec_name,width,height,r_frame_rate,channels,channel_layout', '-of', 'json', input_file ] result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace') return json.loads(result.stdout) def get_audio_labels(input_file): cmd = [ 'ffprobe', '-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=index:stream_tags=title', '-of', 'json', input_file ] result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace') info = json.loads(result.stdout) labels = [] for stream in info.get('streams', []): title = stream.get('tags', {}).get('title', None) labels.append(title) return labels def format_size(size_bytes): for unit in ['B', 'KB', 'MB', 'GB']: if size_bytes < 1024: return f"{size_bytes:.2f} {unit}" size_bytes /= 1024 return f"{size_bytes:.2f} TB" def safe_log_info(message, print_msg=None): try: logging.info(message) except (OSError, IOError) as e: if print_msg is None: print(f"{Colors.YELLOW}[Logging failed: {e}] {message}{Colors.ENDC}") else: print(print_msg) print(f"{Colors.YELLOW}[Logging failed: {e}]{Colors.ENDC}") return except Exception as e: if print_msg is None: print(f"{Colors.YELLOW}[Logging error: {e}] {message}{Colors.ENDC}") else: print(print_msg) print(f"{Colors.YELLOW}[Logging error: {e}]{Colors.ENDC}") return if print_msg is not None: print(print_msg) def safe_log_error(message, print_msg=None): try: logging.error(message) except (OSError, IOError) as e: if print_msg is None: print(f"{Colors.RED}[Logging failed: {e}] {message}{Colors.ENDC}") else: print(print_msg) print(f"{Colors.RED}[Logging failed: {e}]{Colors.ENDC}") return except Exception as e: if print_msg is None: print(f"{Colors.RED}[Logging error: {e}] {message}{Colors.ENDC}") else: print(print_msg) print(f"{Colors.RED}[Logging error: {e}]{Colors.ENDC}") return if print_msg is not None: print(print_msg) else: print(f"{Colors.RED}{message}{Colors.ENDC}") def encode_dvr(input_file, output_dir, gpu, bitrate): input_path = Path(input_file) output_path = Path(output_dir) / f"{input_path.stem}{input_path.suffix}" b_v = f"{bitrate}k" maxrate = f"{bitrate}k" bufsize = f"{bitrate * 2}k" file_info = get_file_info(str(input_path)) input_size = int(file_info['format']['size']) duration = float(file_info['format']['duration']) safe_log_info(f"Processing file: {input_path}") safe_log_info(f"Input size: {format_size(input_size)}") safe_log_info(f"Duration: {duration:.2f} seconds") safe_log_info(f"Bitrate settings - b:v: {b_v}, maxrate: {maxrate}, bufsize: {bufsize}") print(f"\n{Colors.BLUE}Processing file: {input_path}{Colors.ENDC}") print(f"Input size: {format_size(input_size)}") print(f"Duration: {duration:.2f} seconds") print(f"Bitrate settings - b:v: {b_v}, maxrate: {maxrate}, bufsize: {bufsize}") for i, stream in enumerate(file_info.get('streams', [])): stream_type = 'Video' if stream.get('codec_name', '').startswith('h') else 'Audio' safe_log_info(f"Stream {i} ({stream_type}):") for key, value in stream.items(): safe_log_info(f" {key}: {value}") if output_path.exists(): output_size = output_path.stat().st_size safe_log_info(f"Skipping {input_path} - output already exists: {output_path}") safe_log_info(f"Output size: {format_size(output_size)}") print(f"{Colors.YELLOW}Skipping {input_path} - output already exists{Colors.ENDC}") return audio_labels = get_audio_labels(str(input_path)) safe_log_info(f"Audio labels: {audio_labels}") cmd = [ 'ffmpeg', '-v', 'info', '-stats', '-stats_period', '1.0', '-i', str(input_path), '-c:v', 'hevc_nvenc', '-gpu', gpu, '-preset', 'p7', '-tune', 'hq', '-rc', 'vbr', '-rc-lookahead', '32', '-spatial-aq', '1', '-aq-strength', '15', '-cq', '0', '-b:v', b_v, '-maxrate', maxrate, '-bufsize', bufsize, '-c:a', 'copy', '-map', '0', ] for idx, label in enumerate(audio_labels): if label: cmd += [f'-metadata:s:a:{idx}', f'title={label}'] cmd.append(str(output_path)) try: process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf-8', errors='replace', bufsize=1 ) for line in iter(process.stdout.readline, ''): if not line: break text = line.strip() try: if text.startswith('frame=') or ' fps=' in text: safe_log_info(f"Progress: {text}", f"{Colors.PURPLE}Progress: {text}{Colors.ENDC}") else: safe_log_info(f"FFmpeg: {text}", f"{Colors.GREEN}FFmpeg: {text}{Colors.ENDC}") except (OSError, IOError) as e: safe_log_error(f"I/O error reading FFmpeg output: {e}") break except Exception as e: safe_log_error(f"Unexpected error processing FFmpeg output: {e}") process.wait() if process.returncode == 0: output_info = get_file_info(str(output_path)) output_size = int(output_info['format']['size']) compression_ratio = input_size / output_size if output_size > 0 else 0 safe_log_info(f"Successfully encoded: {output_path}", f"{Colors.GREEN}Successfully encoded: {output_path}{Colors.ENDC}") safe_log_info(f"Output size: {format_size(output_size)}") safe_log_info(f"Compression ratio: {compression_ratio:.2f}x", f"Compression ratio: {compression_ratio:.2f}x") else: return_code = process.returncode if return_code > 2147483647: return_code = return_code - 4294967296 safe_log_error(f"FFmpeg process failed with return code {return_code}", f"{Colors.RED}FFmpeg process failed with return code {return_code}{Colors.ENDC}") except subprocess.CalledProcessError as e: safe_log_error(f"Error encoding {input_path}: {e}", f"{Colors.RED}Error encoding {input_path}: {e}{Colors.ENDC}") except Exception as e: safe_log_error(f"Unexpected error encoding {input_path}: {type(e).__name__}: {e}", f"{Colors.RED}Unexpected error encoding {input_path}: {e}{Colors.ENDC}") if __name__ == "__main__": gpu = get_gpu_selection() safe_log_info(f"Selected GPU: {gpu}") bitrate = get_bitrate() safe_log_info(f"Selected bitrate: {bitrate}k") input_dir = "input" output_dir = "output" os.makedirs(output_dir, exist_ok=True) files = [f for f in os.listdir(input_dir) if f.endswith(('.mp4', '.DVR.mp4'))] total_files = len(files) if total_files == 0: safe_log_info("No files to process in input directory", f"{Colors.YELLOW}No files to process in input directory{Colors.ENDC}") else: safe_log_info(f"Found {total_files} files to process", f"{Colors.BLUE}Found {total_files} files to process{Colors.ENDC}") for i, file in enumerate(files, 1): input_file = os.path.join(input_dir, file) safe_log_info(f"Processing file {i}/{total_files}: {file}") print(f"\n{Colors.BLUE}Processing file {i}/{total_files}: {file}{Colors.ENDC}") encode_dvr(input_file, output_dir, gpu, bitrate)