subx_cli/cli/
sync_args.rs

1// src/cli/sync_args.rs
2use clap::Args;
3use std::path::PathBuf;
4
5/// 時間軸同步校正參數
6#[derive(Args, Debug)]
7pub struct SyncArgs {
8    /// 影片檔案路徑
9    pub video: PathBuf,
10
11    /// 字幕檔案路徑
12    pub subtitle: PathBuf,
13
14    /// 手動指定偏移量 (秒)
15    #[arg(long)]
16    pub offset: Option<f64>,
17
18    /// 批量處理模式
19    #[arg(long)]
20    pub batch: bool,
21
22    /// 偏移檢測範圍 (秒),可覆蓋配置檔案中的 max_offset_seconds
23    #[arg(long)]
24    pub range: Option<f32>,
25    /// 相關性閾值,0-1 (可覆蓋配置檔案中的 correlation_threshold)
26    #[arg(long)]
27    pub threshold: Option<f32>,
28}
29
30/// 同步方法
31#[derive(Debug, Clone, PartialEq)]
32pub enum SyncMethod {
33    /// 自動同步:使用音訊分析
34    Auto,
35    /// 手動偏移:使用指定的時間偏移
36    Manual,
37}
38
39impl SyncArgs {
40    /// 根據 offset 參數自動判斷同步方法
41    pub fn sync_method(&self) -> SyncMethod {
42        if self.offset.is_some() {
43            SyncMethod::Manual
44        } else {
45            SyncMethod::Auto
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_sync_method_selection_manual() {
56        let args = SyncArgs {
57            video: PathBuf::from("video.mp4"),
58            subtitle: PathBuf::from("subtitle.srt"),
59            offset: Some(2.5),
60            batch: false,
61            range: None,
62            threshold: None,
63        };
64        assert_eq!(args.sync_method(), SyncMethod::Manual);
65    }
66
67    #[test]
68    fn test_sync_method_selection_auto() {
69        let args = SyncArgs {
70            video: PathBuf::from("video.mp4"),
71            subtitle: PathBuf::from("subtitle.srt"),
72            offset: None,
73            batch: false,
74            range: None,
75            threshold: None,
76        };
77        assert_eq!(args.sync_method(), SyncMethod::Auto);
78    }
79}