use std::path::{Path, PathBuf};
use crate::cli::Cli;
use crate::error::Result;
use crate::ffmpeg::{burn_subtitles, extract_audio, Ffmpeg};
use crate::srt::write_srt;
use crate::whisper::transcribe;
use crate::whisper::TranscribeOptions;
pub struct PipelineOutput {
pub wav_path: PathBuf,
pub srt_path: PathBuf,
pub video_path: Option<PathBuf>,
}
pub fn run(cli: &Cli) -> Result<PipelineOutput> {
let ffmpeg = Ffmpeg::resolve(cli.ffmpeg.as_deref())?;
let input = &cli.input;
let stem = input
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("output");
let parent = input.parent().unwrap_or_else(|| Path::new("."));
let wav_path = parent.join(format!("{stem}.tmp.wav"));
let srt_path = parent.join(format!("{stem}.tmp.srt"));
let output_video = cli
.output
.clone()
.unwrap_or_else(|| parent.join(format!("{stem}_subtitled.mp4")));
eprintln!("[1/4] FFmpeg 提取音频 → {}", wav_path.display());
extract_audio(&ffmpeg, input, &wav_path)?;
eprintln!("[2/4] Whisper 识别…");
let captions = transcribe(
&wav_path,
&TranscribeOptions {
model_path: &cli.model,
language: cli.language.as_deref(),
translate: cli.translate,
},
)?;
eprintln!(" 共 {} 条字幕", captions.len());
eprintln!("[3/4] 生成 SRT → {}", srt_path.display());
write_srt(&srt_path, &captions)?;
let video_path = if cli.srt_only {
eprintln!("[4/4] 跳过烧录(--srt-only)");
None
} else {
eprintln!("[4/4] FFmpeg 烧录字幕 → {}", output_video.display());
burn_subtitles(&ffmpeg, input, &srt_path, &output_video)?;
Some(output_video)
};
if !cli.keep_temp {
let _ = std::fs::remove_file(&wav_path);
if video_path.is_some() {
let _ = std::fs::remove_file(&srt_path);
}
}
Ok(PipelineOutput {
wav_path,
srt_path,
video_path,
})
}