import os import subprocess import logging from pathlib import Path # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # Constants SIZE_THRESHOLD = 5 * 1024 * 1024 * 1024 # 5GB in bytes HANDBRAKE_PATH = r"HandBrakeCLI.exe" PRESET_FILE = "xiaowan.json" PRESET_NAME = "1080P x264 xiaowan" def compress_video(input_path, output_path): try: command = [ HANDBRAKE_PATH, "--preset-import-file", PRESET_FILE, "-Z", PRESET_NAME, "-i", str(input_path), "-o", str(output_path) ] subprocess.run(command, check=True) logging.info(f"Successfully compressed: {input_path}") except subprocess.CalledProcessError as e: logging.error(f"Error compressing {input_path}: {e}") def process_folder(input_folder, output_folder): for root, _, files in os.walk(input_folder): for file in files: input_path = Path(root) / file relative_path = input_path.relative_to(input_folder) output_path = Path(output_folder) / relative_path # Check if it's a video file (you may want to add more extensions) if input_path.suffix.lower() in ['.mp4', '.avi', '.mkv', '.mov']: if input_path.stat().st_size > SIZE_THRESHOLD: output_path.parent.mkdir(parents=True, exist_ok=True) compress_video(input_path, output_path) else: logging.info(f"Skipping {input_path} (size < 5GB)") else: logging.info(f"Skipping non-video file: {input_path}") def main(): input_folder = input("Enter the input folder path: ") output_folder = input("Enter the output folder path: ") if not os.path.exists(input_folder): logging.error(f"Input folder does not exist: {input_folder}") return if not os.path.exists(output_folder): os.makedirs(output_folder) logging.info(f"Created output folder: {output_folder}") process_folder(input_folder, output_folder) if __name__ == "__main__": try: main() except Exception as e: logging.exception(f"An unexpected error occurred: {e}")