Add output.rs

This commit is contained in:
2026-03-11 01:46:05 +08:00
parent 78c79cddcd
commit ffb3b728d1

87
src/output.rs Normal file
View File

@@ -0,0 +1,87 @@
use std::path::Path;
use crate::metadata::VideoMetadata;
use crate::error::Result;
/// Save metadata to JSON file
pub fn save_metadata(video_path: &str, metadata: &VideoMetadata) -> Result<String> {
let video_path = Path::new(video_path);
let video_dir = video_path.parent().unwrap_or(Path::new("."));
let video_name = video_path.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown");
let output_file = video_dir.join(format!("{}.probe.json", video_name));
let json = serde_json::to_string_pretty(metadata)?;
std::fs::write(&output_file, json)?;
Ok(output_file.to_string_lossy().to_string())
}
/// Print metadata summary to console
pub fn print_summary(metadata: &VideoMetadata) {
println!("✓ Video probed successfully!\n");
// Format info
if let Some(ref filename) = metadata.format.filename {
println!("File: {}", filename);
}
if let Some(ref format_name) = metadata.format.format_long_name {
println!("Format: {}", format_name);
}
println!("Duration: {:.2} seconds", metadata.format.duration);
println!("Size: {:.2} MB", metadata.format.size as f64 / 1024.0 / 1024.0);
println!("Bit rate: {:.0} kbps", metadata.format.bit_rate as f64 / 1000.0);
// Video stream
if let Some(ref vs) = metadata.video_stream {
println!("\nVideo Stream:");
if let Some(ref codec) = vs.codec_name {
print!(" Codec: {}", codec);
if let Some(ref profile) = vs.profile {
print!(" ({})", profile);
}
println!();
}
println!(" Resolution: {}x{}", vs.width, vs.height);
if let Some(ref frame_rate) = vs.r_frame_rate {
println!(" Frame rate: {}", frame_rate);
}
if let Some(ref pix_fmt) = vs.pix_fmt {
println!(" Pixel format: {}", pix_fmt);
}
}
// Audio streams
if !metadata.audio_streams.is_empty() {
println!("\nAudio Streams: {}", metadata.audio_streams.len());
for (i, audio) in metadata.audio_streams.iter().enumerate() {
print!(" [{}] ", i + 1);
if let Some(ref codec) = audio.codec_name {
print!("{}", codec);
}
print!(" - {} channels", audio.channels);
if let Some(ref sample_rate) = audio.sample_rate {
print!(" @ {} Hz", sample_rate);
}
println!();
}
}
// Subtitle streams
if !metadata.subtitle_streams.is_empty() {
println!("\nSubtitle Streams: {}", metadata.subtitle_streams.len());
for (i, sub) in metadata.subtitle_streams.iter().enumerate() {
print!(" [{}] ", i + 1);
if let Some(ref codec) = sub.codec_name {
print!("{}", codec);
}
if let Some(ref lang) = sub.language {
print!(" ({})", lang);
}
println!();
}
}
}