#!/usr/bin/env python3 """ Advanced Codecs Demonstration Showcases next-generation codec capabilities (AV1, HEVC, HDR) built on the existing comprehensive video processing infrastructure. """ import logging from pathlib import Path from video_processor import ProcessorConfig, VideoProcessor from video_processor.core.advanced_encoders import AdvancedVideoEncoder, HDRProcessor # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def demonstrate_av1_encoding(video_path: Path, output_dir: Path): """Demonstrate AV1 encoding capabilities.""" logger.info("=== AV1 Encoding Demonstration ===") config = ProcessorConfig( base_path=output_dir, output_formats=["av1_mp4", "av1_webm"], # New AV1 formats quality_preset="high", enable_av1_encoding=True, prefer_two_pass_av1=True, ) # Check AV1 support advanced_encoder = AdvancedVideoEncoder(config) print("\nšŸ” AV1 Codec Support Check:") av1_supported = advanced_encoder._check_av1_support() print(f" AV1 Support Available: {'āœ… Yes' if av1_supported else 'āŒ No'}") if not av1_supported: print(" To enable AV1: Install FFmpeg with libaom-av1 encoder") print(" Example: sudo apt install ffmpeg (with AV1 support)") return print("\nāš™ļø AV1 Configuration:") quality_presets = advanced_encoder._get_advanced_quality_presets() current_preset = quality_presets[config.quality_preset] print(f" Quality Preset: {config.quality_preset}") print(f" CRF Value: {current_preset['av1_crf']}") print(f" CPU Used (speed): {current_preset['av1_cpu_used']}") print(f" Bitrate Multiplier: {current_preset['bitrate_multiplier']}") print( f" Two-Pass Encoding: {'āœ… Enabled' if config.prefer_two_pass_av1 else 'āŒ Disabled'}" ) # Process with standard VideoProcessor (uses new AV1 formats) try: processor = VideoProcessor(config) result = processor.process_video(video_path) print("\nšŸŽ‰ AV1 Encoding Results:") for format_name, output_path in result.encoded_files.items(): if "av1" in format_name: file_size = output_path.stat().st_size if output_path.exists() else 0 print( f" {format_name.upper()}: {output_path.name} ({file_size // 1024} KB)" ) # Compare with standard H.264 if result.encoded_files.get("mp4"): av1_size = ( result.encoded_files.get("av1_mp4", Path()).stat().st_size if result.encoded_files.get("av1_mp4", Path()).exists() else 0 ) h264_size = ( result.encoded_files["mp4"].stat().st_size if result.encoded_files["mp4"].exists() else 0 ) if av1_size > 0 and h264_size > 0: savings = (1 - av1_size / h264_size) * 100 print(f" šŸ’¾ AV1 vs H.264 Size: {savings:.1f}% smaller") except Exception as e: logger.error(f"AV1 encoding demonstration failed: {e}") def demonstrate_hevc_encoding(video_path: Path, output_dir: Path): """Demonstrate HEVC/H.265 encoding capabilities.""" logger.info("=== HEVC/H.265 Encoding Demonstration ===") config = ProcessorConfig( base_path=output_dir, output_formats=["hevc", "mp4"], # Compare HEVC vs H.264 quality_preset="high", enable_hevc_encoding=True, enable_hardware_acceleration=True, ) advanced_encoder = AdvancedVideoEncoder(config) print("\nšŸ” HEVC Codec Support Check:") hardware_hevc = advanced_encoder._check_hardware_hevc_support() print( f" Hardware HEVC: {'āœ… Available' if hardware_hevc else 'āŒ Not Available'}" ) print(" Software HEVC: āœ… Available (libx265)") print("\nāš™ļø HEVC Configuration:") print(f" Quality Preset: {config.quality_preset}") print( f" Hardware Acceleration: {'āœ… Enabled' if config.enable_hardware_acceleration else 'āŒ Disabled'}" ) if hardware_hevc: print(" Encoder: hevc_nvenc (hardware) with libx265 fallback") else: print(" Encoder: libx265 (software)") try: processor = VideoProcessor(config) result = processor.process_video(video_path) print("\nšŸŽ‰ HEVC Encoding Results:") for format_name, output_path in result.encoded_files.items(): file_size = output_path.stat().st_size if output_path.exists() else 0 codec_name = "HEVC/H.265" if format_name == "hevc" else "H.264" print(f" {codec_name}: {output_path.name} ({file_size // 1024} KB)") # Compare HEVC vs H.264 compression if "hevc" in result.encoded_files and "mp4" in result.encoded_files: hevc_size = ( result.encoded_files["hevc"].stat().st_size if result.encoded_files["hevc"].exists() else 0 ) h264_size = ( result.encoded_files["mp4"].stat().st_size if result.encoded_files["mp4"].exists() else 0 ) if hevc_size > 0 and h264_size > 0: savings = (1 - hevc_size / h264_size) * 100 print(f" šŸ’¾ HEVC vs H.264 Size: {savings:.1f}% smaller") except Exception as e: logger.error(f"HEVC encoding demonstration failed: {e}") def demonstrate_hdr_processing(video_path: Path, output_dir: Path): """Demonstrate HDR video processing capabilities.""" logger.info("=== HDR Video Processing Demonstration ===") config = ProcessorConfig( base_path=output_dir, enable_hdr_processing=True, ) hdr_processor = HDRProcessor(config) print("\nšŸ” HDR Support Check:") hdr_support = HDRProcessor.get_hdr_support() for standard, supported in hdr_support.items(): status = "āœ… Supported" if supported else "āŒ Not Supported" print(f" {standard.upper()}: {status}") # Analyze input video for HDR content print("\nšŸ“Š Analyzing Input Video for HDR:") hdr_analysis = hdr_processor.analyze_hdr_content(video_path) if hdr_analysis.get("is_hdr"): print(" HDR Content: āœ… Detected") print(f" Color Primaries: {hdr_analysis.get('color_primaries', 'unknown')}") print( f" Transfer Characteristics: {hdr_analysis.get('color_transfer', 'unknown')}" ) print(f" Color Space: {hdr_analysis.get('color_space', 'unknown')}") try: # Process HDR video hdr_result = hdr_processor.encode_hdr_hevc( video_path, output_dir, "demo_hdr", hdr_standard="hdr10" ) print("\nšŸŽ‰ HDR Processing Results:") if hdr_result.exists(): file_size = hdr_result.stat().st_size print(f" HDR10 HEVC: {hdr_result.name} ({file_size // 1024} KB)") print( " Features: 10-bit encoding, BT.2020 color space, HDR10 metadata" ) except Exception as e: logger.warning(f"HDR processing failed: {e}") print(" āš ļø HDR processing requires HEVC encoder with HDR support") else: print(" HDR Content: āŒ Not detected (SDR video)") print(" This is standard dynamic range content") if "error" in hdr_analysis: print(f" Analysis note: {hdr_analysis['error']}") def demonstrate_codec_comparison(video_path: Path, output_dir: Path): """Compare different codec performance and characteristics.""" logger.info("=== Codec Comparison Analysis ===") # Test all available codecs config = ProcessorConfig( base_path=output_dir, output_formats=["mp4", "webm", "hevc", "av1_mp4"], quality_preset="medium", ) print(f"\nšŸ“ˆ Codec Comparison (Quality: {config.quality_preset}):") print(f"{'Codec':<12} {'Container':<10} {'Compression':<12} {'Compatibility'}") print("-" * 60) print(f"{'H.264':<12} {'MP4':<10} {'Baseline':<12} {'Universal'}") print(f"{'VP9':<12} {'WebM':<10} {'~25% better':<12} {'Modern browsers'}") print(f"{'HEVC/H.265':<12} {'MP4':<10} {'~25% better':<12} {'Modern devices'}") print(f"{'AV1':<12} {'MP4/WebM':<10} {'~30% better':<12} {'Latest browsers'}") advanced_encoder = AdvancedVideoEncoder(config) print("\nšŸ”§ Codec Availability:") print(" H.264 (libx264): āœ… Always available") print(" VP9 (libvpx-vp9): āœ… Usually available") print(f" HEVC (libx265): {'āœ… Available' if True else 'āŒ Not available'}") print( f" HEVC Hardware: {'āœ… Available' if advanced_encoder._check_hardware_hevc_support() else 'āŒ Not available'}" ) print( f" AV1 (libaom-av1): {'āœ… Available' if advanced_encoder._check_av1_support() else 'āŒ Not available'}" ) print("\nšŸ’” Recommendations:") print(" šŸ“± Mobile/Universal: H.264 MP4") print(" 🌐 Web streaming: VP9 WebM + H.264 fallback") print(" šŸ“ŗ Modern devices: HEVC MP4") print(" šŸš€ Future-proof: AV1 (with fallbacks)") print(" šŸŽ¬ HDR content: HEVC with HDR10 metadata") def main(): """Main demonstration function.""" # Use test video or user-provided path video_path = Path("tests/fixtures/videos/big_buck_bunny_720p_1mb.mp4") output_dir = Path("/tmp/advanced_codecs_demo") # Create output directory output_dir.mkdir(exist_ok=True) print("šŸŽ¬ Advanced Video Codecs Demonstration") print("=" * 50) if not video_path.exists(): print(f"āš ļø Test video not found: {video_path}") print(" Please provide a video file path as argument:") print(" python examples/advanced_codecs_demo.py /path/to/your/video.mp4") return try: # 1. AV1 demonstration demonstrate_av1_encoding(video_path, output_dir) print("\n" + "=" * 50) # 2. HEVC demonstration demonstrate_hevc_encoding(video_path, output_dir) print("\n" + "=" * 50) # 3. HDR processing demonstration demonstrate_hdr_processing(video_path, output_dir) print("\n" + "=" * 50) # 4. Codec comparison demonstrate_codec_comparison(video_path, output_dir) print("\nšŸŽ‰ Advanced codecs demonstration complete!") print(f" Output files: {output_dir}") print(" Check the generated files to compare codec performance") except Exception as e: logger.error(f"Demonstration failed: {e}") raise if __name__ == "__main__": import sys # Allow custom video path if len(sys.argv) > 1: custom_video_path = Path(sys.argv[1]) if custom_video_path.exists(): # Override main function with custom path def custom_main(): output_dir = Path("/tmp/advanced_codecs_demo") output_dir.mkdir(exist_ok=True) print("šŸŽ¬ Advanced Video Codecs Demonstration") print("=" * 50) print(f"Using custom video: {custom_video_path}") demonstrate_av1_encoding(custom_video_path, output_dir) demonstrate_hevc_encoding(custom_video_path, output_dir) demonstrate_hdr_processing(custom_video_path, output_dir) demonstrate_codec_comparison(custom_video_path, output_dir) print("\nšŸŽ‰ Advanced codecs demonstration complete!") print(f" Output files: {output_dir}") custom_main() else: print(f"āŒ Video file not found: {custom_video_path}") else: main()