Files
mixvideo-v2/cargos/tvai/docs/API.md
imeepos bdac328e19 feat: 完成 tvai 库测试和文档 (阶段六) - 项目完成
集成测试套件
- 创建完整的集成测试 (integration_tests.rs)
- 测试库初始化和配置管理
- 测试 GPU 检测和优化功能
- 测试性能监控和基准测试
- 测试错误处理和用户友好消息
- 测试配置文件持久化
- 测试模型和参数验证
- 测试临时文件管理
- 所有测试通过

 性能基准测试
- 创建完整的基准测试套件 (performance_benchmarks.rs)
- GPU 检测性能: ~193ms
- 设置保存/加载: ~1.56ms
- 预设查找: ~29ns (超快)
- 临时文件管理: ~96μs
- 参数验证: ~3.6ns (极快)
- 错误消息生成: ~266ns
- 模型操作: ~1.9ns (极快)
- 系统检测: 24μs - 30ms

 完整 API 文档
- 创建详细的 API 文档 (docs/API.md)
- 核心组件使用指南
- 所有方法和参数说明
- 代码示例和最佳实践
- 错误处理指南
- 性能优化建议

 用户指南
- 创建完整的用户指南 (docs/USER_GUIDE.md)
- 快速入门教程
- 常见用例和场景
- 配置管理指南
- 模型选择指南
- 性能优化技巧
- 故障排除指南

 更新项目文档
- 更新主 README.md
- 标记项目为 100% 完成
- 添加文档链接和使用指南
- 添加性能和测试信息
- 添加开发设置说明
- 添加变更日志

 测试结果总结
-  单元测试: 6/6 通过
-  集成测试: 10/10 通过
-  文档测试: 1/1 通过
-  基准测试: 13/13 完成
-  所有示例运行成功

 最终项目统计
- **总代码行数**: 4,127行
- **模块文件**: 25个
- **示例文件**: 6个
- **测试文件**: 2个 (单元 + 集成)
- **基准测试**: 1个 (13项基准)
- **文档文件**: 3个 (API + 用户指南 + README)

 功能完整性 (100%)
-  视频处理 (超分辨率 + 插值)
-  图片处理 (超分辨率 + 批量)
-  格式转换 (视频  图片序列)
-  便捷接口 (一键处理函数)
-  配置管理 (全局设置 + 预设)
-  性能优化 (GPU检测 + 监控)
-  错误处理 (用户友好消息)
-  文档和测试 (完整覆盖)

 项目状态: 完成 (COMPLETE)
所有六个开发阶段已完成,tvai 库已准备好用于生产环境!
2025-08-11 16:20:27 +08:00

325 lines
7.5 KiB
Markdown

# TVAI Library API Documentation
## Overview
The TVAI library provides a comprehensive Rust interface for Topaz Video AI, enabling video and image enhancement through AI-powered super-resolution and frame interpolation.
## Core Components
### TvaiProcessor
The main processor for all video and image operations.
```rust
use tvai::*;
// Create configuration
let config = TvaiConfig::builder()
.topaz_path("/path/to/topaz")
.use_gpu(true)
.build()?;
// Create processor
let mut processor = TvaiProcessor::new(config)?;
```
#### Video Processing Methods
- `upscale_video()` - AI-powered video super-resolution
- `interpolate_video()` - Frame interpolation for slow motion
- `enhance_video()` - Combined upscaling and interpolation
- `images_to_video()` - Convert image sequence to video
- `video_to_images()` - Extract frames from video
#### Image Processing Methods
- `upscale_image()` - AI-powered image super-resolution
- `batch_upscale_images()` - Process multiple images
- `upscale_directory()` - Process entire directories
- `convert_image_format()` - Format conversion
- `resize_image()` - Traditional geometric scaling
### Configuration Management
#### TvaiConfig
Main configuration for the processor.
```rust
let config = TvaiConfig::builder()
.topaz_path("/path/to/topaz")
.use_gpu(true)
.temp_dir("/custom/temp")
.force_topaz_ffmpeg(true)
.build()?;
```
#### Global Settings
Persistent global configuration.
```rust
use tvai::config::global_settings;
let settings = global_settings();
settings.set_default_use_gpu(true)?;
settings.set_max_concurrent_jobs(2)?;
```
### AI Models
#### Upscaling Models
- `Iris3` - Best general purpose model
- `Nyx3` - Optimized for portraits
- `Thf4` - Old content restoration
- `Ghq5` - Game/CG content
- `Prob4` - Problem footage repair
- And 11 more specialized models
#### Interpolation Models
- `Apo8` - High quality interpolation
- `Chr2` - Animation content
- `Apf1` - Fast processing
- `Chf3` - Fast animation
### Parameter Presets
#### Video Presets
```rust
// Built-in presets
let old_video = VideoUpscaleParams::for_old_video();
let game_content = VideoUpscaleParams::for_game_content();
let animation = VideoUpscaleParams::for_animation();
let portrait = VideoUpscaleParams::for_portrait();
// Interpolation presets
let slow_motion = InterpolationParams::for_slow_motion(30, 2.0);
let animation_interp = InterpolationParams::for_animation(24, 2.0);
```
#### Image Presets
```rust
// Built-in presets
let photo = ImageUpscaleParams::for_photo();
let artwork = ImageUpscaleParams::for_artwork();
let screenshot = ImageUpscaleParams::for_screenshot();
let portrait = ImageUpscaleParams::for_portrait();
```
### Preset Management
```rust
use tvai::config::global_presets;
let presets = global_presets();
let preset_manager = presets.lock().unwrap();
// Get preset
if let Some(preset) = preset_manager.get_video_preset("general_2x") {
// Use preset parameters
}
// List all presets
let video_presets = preset_manager.list_video_presets();
let image_presets = preset_manager.list_image_presets();
```
## Quick Start Functions
### Video Processing
```rust
// Quick 2x upscaling
quick_upscale_video(
Path::new("input.mp4"),
Path::new("output.mp4"),
2.0
).await?;
// Automatic enhancement
auto_enhance_video(
Path::new("input.mp4"),
Path::new("enhanced.mp4")
).await?;
```
### Image Processing
```rust
// Quick 2x upscaling
quick_upscale_image(
Path::new("photo.jpg"),
Path::new("photo_2x.png"),
2.0
).await?;
// Automatic enhancement
auto_enhance_image(
Path::new("photo.jpg"),
Path::new("enhanced.png")
).await?;
// Batch directory processing
batch_upscale_directory(
Path::new("input_dir"),
Path::new("output_dir"),
2.0,
true // recursive
).await?;
```
## Performance and Optimization
### GPU Detection
```rust
use tvai::utils::GpuManager;
// Detailed GPU information
let gpu_info = GpuManager::detect_detailed_gpu_info();
println!("CUDA available: {}", gpu_info.cuda_available);
println!("Devices: {}", gpu_info.devices.len());
// Check suitability for AI
let suitable = GpuManager::is_gpu_suitable_for_ai();
// Benchmark performance
let benchmark = GpuManager::benchmark_gpu_performance().await?;
```
### Performance Monitoring
```rust
use tvai::utils::{PerformanceMonitor, optimize_for_system};
// Create optimized settings
let settings = optimize_for_system();
let mut monitor = PerformanceMonitor::new(settings);
// Monitor operation
let _permit = monitor.acquire_slot().await?;
let operation_monitor = monitor.start_operation("upscale", 100.0);
// ... perform processing ...
let metrics = operation_monitor.finish(200.0);
monitor.record_metrics(metrics);
// Get performance summary
let summary = monitor.get_summary();
```
## Error Handling
### Error Types
The library provides comprehensive error handling with user-friendly messages:
```rust
match result {
Ok(output) => println!("Success: {:?}", output),
Err(error) => {
println!("Error category: {}", error.category());
println!("Recoverable: {}", error.is_recoverable());
println!("User message:\n{}", error.user_friendly_message());
}
}
```
### Error Categories
- `installation` - Topaz/FFmpeg not found
- `processing` - Processing failures
- `parameter` - Invalid parameters
- `gpu` - GPU-related errors
- `format` - Unsupported formats
- `resources` - Insufficient resources
- `permission` - Permission denied
- `io` - File I/O errors
## System Detection
### Automatic Detection
```rust
// Detect Topaz installation
let topaz_path = detect_topaz_installation();
// Detect FFmpeg availability
let ffmpeg_info = detect_ffmpeg();
// Detect GPU support
let gpu_info = detect_gpu_support();
```
### File Information
```rust
// Get video information
let video_info = get_video_info(Path::new("video.mp4")).await?;
println!("Duration: {:?}", video_info.duration);
println!("Resolution: {}x{}", video_info.width, video_info.height);
// Get image information
let image_info = get_image_info(Path::new("image.jpg"))?;
println!("Size: {}x{}", image_info.width, image_info.height);
```
## Progress Tracking
```rust
// Create progress callback
let progress_callback: ProgressCallback = Box::new(|progress| {
println!("Progress: {:.1}%", progress * 100.0);
});
// Use with processing functions
processor.upscale_video(
input,
output,
params,
Some(&progress_callback)
).await?;
```
## Temporary File Management
```rust
use tvai::utils::TempFileManager;
let mut temp_manager = TempFileManager::new(None)?;
// Create temporary files
let temp_path = temp_manager.create_temp_path("operation", "temp.mp4");
let unique_path = temp_manager.create_unique_temp_path("output.png");
// Cleanup
temp_manager.cleanup_operation("operation")?;
temp_manager.cleanup_all()?;
```
## Best Practices
1. **Use presets** for common scenarios
2. **Enable GPU** for better performance
3. **Monitor progress** for long operations
4. **Handle errors** gracefully with user-friendly messages
5. **Use global settings** for consistent configuration
6. **Validate parameters** before processing
7. **Clean up** temporary files after processing
8. **Check system requirements** before starting
## Examples
See the `examples/` directory for complete working examples:
- `basic_usage.rs` - Simple getting started example
- `advanced_usage.rs` - Advanced features demonstration
- `video_processing.rs` - Comprehensive video processing
- `image_processing.rs` - Comprehensive image processing
- `convenience_and_optimization.rs` - Convenience features and optimization