|
| 1 | +//! Video color adjustment example |
| 2 | +//! |
| 3 | +//! This example demonstrates adjusting video brightness, contrast, and saturation. |
| 4 | +
|
| 5 | +use std::path::Path; |
| 6 | +use video_utils::filters::color::{ |
| 7 | + adjust_color, ColorAdjustConfig, adjust_brightness, adjust_contrast, adjust_saturation, |
| 8 | +}; |
| 9 | +use video_utils::metadata::get_metadata; |
| 10 | + |
| 11 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 12 | + env_logger::init(); |
| 13 | + |
| 14 | + println!("╔════════════════════════════════════════════════════════════════╗"); |
| 15 | + println!("║ 视频颜色调整功能测试 ║"); |
| 16 | + println!("╚════════════════════════════════════════════════════════════════╝"); |
| 17 | + println!(); |
| 18 | + |
| 19 | + // Check if test file exists |
| 20 | + let input_file = "data/test.mp4"; |
| 21 | + if !Path::new(input_file).exists() { |
| 22 | + println!("❌ 测试文件不存在: {}", input_file); |
| 23 | + println!("请先确保有测试视频文件"); |
| 24 | + return Ok(()); |
| 25 | + } |
| 26 | + |
| 27 | + // Get original metadata |
| 28 | + println!("📹 原始视频信息:"); |
| 29 | + let metadata = get_metadata(input_file)?; |
| 30 | + println!(" 时长: {:.2} 秒", metadata.duration); |
| 31 | + println!(" 视频流数: {}", metadata.video_streams_count); |
| 32 | + println!(); |
| 33 | + |
| 34 | + // Test 1: Increase brightness |
| 35 | + println!("【测试1】增加亮度 (+30%)"); |
| 36 | + println!("========================================="); |
| 37 | + |
| 38 | + match adjust_brightness(input_file, "tmp/color_bright.mp4", 30) { |
| 39 | + Ok(_) => println!("✓ 亮度调整完成"), |
| 40 | + Err(e) => println!("❌ 调整失败: {}", e), |
| 41 | + } |
| 42 | + |
| 43 | + verify_output("tmp/color_bright.mp4", "亮度调整")?; |
| 44 | + println!(); |
| 45 | + |
| 46 | + // Test 2: Decrease brightness |
| 47 | + println!("【测试2】降低亮度 (-30%)"); |
| 48 | + println!("========================================="); |
| 49 | + |
| 50 | + match adjust_brightness(input_file, "tmp/color_dark.mp4", -30) { |
| 51 | + Ok(_) => println!("✓ 亮度调整完成"), |
| 52 | + Err(e) => println!("❌ 调整失败: {}", e), |
| 53 | + } |
| 54 | + |
| 55 | + verify_output("tmp/color_dark.mp4", "降低亮度")?; |
| 56 | + println!(); |
| 57 | + |
| 58 | + // Test 3: Increase contrast |
| 59 | + println!("【测试3】增加对比度 (+40%)"); |
| 60 | + println!("========================================="); |
| 61 | + |
| 62 | + match adjust_contrast(input_file, "tmp/color_contrast.mp4", 40) { |
| 63 | + Ok(_) => println!("✓ 对比度调整完成"), |
| 64 | + Err(e) => println!("❌ 调整失败: {}", e), |
| 65 | + } |
| 66 | + |
| 67 | + verify_output("tmp/color_contrast.mp4", "对比度调整")?; |
| 68 | + println!(); |
| 69 | + |
| 70 | + // Test 4: Grayscale (saturation -100) |
| 71 | + println!("【测试4】灰度化 (饱和度 -100%)"); |
| 72 | + println!("========================================="); |
| 73 | + |
| 74 | + match adjust_saturation(input_file, "tmp/color_gray.mp4", -100) { |
| 75 | + Ok(_) => println!("✓ 饱和度调整完成"), |
| 76 | + Err(e) => println!("❌ 调整失败: {}", e), |
| 77 | + } |
| 78 | + |
| 79 | + verify_output("tmp/color_gray.mp4", "灰度化")?; |
| 80 | + println!(); |
| 81 | + |
| 82 | + // Test 5: Combined adjustments |
| 83 | + println!("【测试5】组合调整 (亮度+20, 对比度+30, 饱和度+50)"); |
| 84 | + println!("========================================="); |
| 85 | + |
| 86 | + let config = ColorAdjustConfig::new(input_file, "tmp/color_combined.mp4") |
| 87 | + .with_brightness(20) |
| 88 | + .with_contrast(30) |
| 89 | + .with_saturation(50); |
| 90 | + |
| 91 | + match adjust_color(config) { |
| 92 | + Ok(_) => println!("✓ 组合调整完成"), |
| 93 | + Err(e) => println!("❌ 调整失败: {}", e), |
| 94 | + } |
| 95 | + |
| 96 | + verify_output("tmp/color_combined.mp4", "组合调整")?; |
| 97 | + println!(); |
| 98 | + |
| 99 | + println!("╔════════════════════════════════════════════════════════════════╗"); |
| 100 | + println!("║ 测试完成 ║"); |
| 101 | + println!("╚════════════════════════════════════════════════════════════════╝"); |
| 102 | + |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +/// Verify output file using ffprobe |
| 107 | +fn verify_output(file: &str, test_name: &str) -> Result<(), Box<dyn std::error::Error>> { |
| 108 | + if !Path::new(file).exists() { |
| 109 | + println!(" ⚠️ 输出文件不存在: {}", file); |
| 110 | + return Ok(()); |
| 111 | + } |
| 112 | + |
| 113 | + println!(" 🔍 验证输出文件..."); |
| 114 | + |
| 115 | + // Use ffprobe to get video info |
| 116 | + let output = std::process::Command::new("ffprobe") |
| 117 | + .arg("-v") |
| 118 | + .arg("error") |
| 119 | + .arg("-select_streams") |
| 120 | + .arg("v:0") |
| 121 | + .arg("-show_entries") |
| 122 | + .arg("stream=width,height,duration") |
| 123 | + .arg("-of") |
| 124 | + .arg("default=noprint_wrappers=1:nokey=1") |
| 125 | + .arg(file) |
| 126 | + .output()?; |
| 127 | + |
| 128 | + if output.status.success() { |
| 129 | + let info = String::from_utf8_lossy(&output.stdout); |
| 130 | + let lines: Vec<&str> = info.trim().split('\n').collect(); |
| 131 | + |
| 132 | + println!(" ✅ {} 验证通过:", test_name); |
| 133 | + for line in lines.iter().take(3) { |
| 134 | + let label = match line.trim() { |
| 135 | + l if l.parse::<f32>().is_ok() => "时长", |
| 136 | + l if l.parse::<u32>().is_ok() && l.parse::<u32>().ok().unwrap_or(5000) < 5000 => "宽度/高度", |
| 137 | + _ => line, |
| 138 | + }; |
| 139 | + println!(" {}: {}", label, line.trim()); |
| 140 | + } |
| 141 | + |
| 142 | + // Get file size |
| 143 | + if let Ok(metadata) = std::fs::metadata(file) { |
| 144 | + let size_kb = metadata.len() / 1024; |
| 145 | + println!(" 大小: {} KB", size_kb); |
| 146 | + } |
| 147 | + } else { |
| 148 | + println!(" ⚠️ ffprobe 验证失败"); |
| 149 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 150 | + println!(" 错误: {}", stderr); |
| 151 | + } |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments