#!/usr/bin/env python3 """ audio_optimizer.py Optimizes .wav files to .ogg format for faster game loading Features: - Batch conversion of all .wav files - Maintains folder structure - Preserves metadata - Reports file size savings Requirements: pip install pydub Usage: python audio_optimizer.py Created: Jan 10, 2026 Author: David "HIPO" Kotnik Studio: Hipodevil666 Studiosโ„ข """ import os import sys from pathlib import Path try: from pydub import AudioSegment except ImportError: print("โŒ Error: pydub not installed!") print("Install it with: pip install pydub") print("Also requires ffmpeg: brew install ffmpeg (macOS)") sys.exit(1) # Configuration ASSETS_DIR = Path("assets/audio") QUALITY = 5 # OGG quality (0-10, higher = better) def get_file_size_mb(file_path): """Get file size in MB""" return os.path.getsize(file_path) / (1024 * 1024) def convert_wav_to_ogg(wav_path, ogg_path): """Convert single .wav file to .ogg""" try: # Load WAV file audio = AudioSegment.from_wav(wav_path) # Export as OGG with quality settings audio.export( ogg_path, format="ogg", codec="libvorbis", parameters=["-q:a", str(QUALITY)] ) return True except Exception as e: print(f"โŒ Error converting {wav_path}: {e}") return False def optimize_audio_files(): """Main optimization function""" if not ASSETS_DIR.exists(): print(f"โŒ Assets directory not found: {ASSETS_DIR}") return print("๐ŸŽต DolinaSmrti Audio Optimizer") print("=" * 50) print(f"Searching for .wav files in: {ASSETS_DIR}") print() # Find all .wav files wav_files = list(ASSETS_DIR.rglob("*.wav")) if not wav_files: print("โœ… No .wav files found - all audio already optimized!") return print(f"Found {len(wav_files)} .wav files to convert") print() total_before = 0 total_after = 0 converted = 0 skipped = 0 for wav_file in wav_files: # Get relative path rel_path = wav_file.relative_to(ASSETS_DIR) # Create .ogg path ogg_file = wav_file.with_suffix('.ogg') # Skip if .ogg already exists if ogg_file.exists(): print(f"โญ๏ธ Skipped {rel_path} (ogg exists)") skipped += 1 continue # Get original size wav_size = get_file_size_mb(wav_file) total_before += wav_size print(f"๐Ÿ”„ Converting: {rel_path}") print(f" Size: {wav_size:.2f} MB") # Convert if convert_wav_to_ogg(wav_file, ogg_file): ogg_size = get_file_size_mb(ogg_file) total_after += ogg_size savings = ((wav_size - ogg_size) / wav_size) * 100 print(f" โœ… Converted to: {rel_path.with_suffix('.ogg')}") print(f" New size: {ogg_size:.2f} MB ({savings:.1f}% smaller)") print() converted += 1 # Optional: Delete original .wav file # Uncomment the following line to auto-delete .wav files: # wav_file.unlink() else: print() # Summary print("=" * 50) print("๐ŸŽ‰ Optimization Complete!") print() print(f"Files converted: {converted}") print(f"Files skipped: {skipped}") if converted > 0: print(f"Total before: {total_before:.2f} MB") print(f"Total after: {total_after:.2f} MB") total_savings = total_before - total_after percent_savings = (total_savings / total_before) * 100 if total_before > 0 else 0 print(f"Saved: {total_savings:.2f} MB ({percent_savings:.1f}%)") print() print("๐Ÿ’ก Tip: Delete .wav files manually if conversions are successful") print(" This will save even more space!") print() print("๐ŸŽฎ Game loading will be faster with .ogg files!") if __name__ == "__main__": optimize_audio_files()