dialog_detective 1.1.1

Automatically identify and rename unknown tv series video files by letting AI listen to their dialogue.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use clap::{Parser, ValueEnum};
use dialog_detective::{
    MatcherType, ProgressEvent, execute_copy, execute_rename, investigate_case, model_downloader,
    plan_operations,
};
use std::path::PathBuf;
use std::process;

/// DialogDetective - Automatically identify and rename unknown video files
///
/// This tool analyzes video files by extracting audio, transcribing speech,
/// and using AI to match the content to TV series episodes.
#[derive(Parser)]
#[command(name = "dialog_detective")]
#[command(version, about, long_about = None)]
#[command(
    after_help = "💡 TIP: Use --season to filter episodes for faster, cheaper, more accurate matching!"
)]
struct Cli {
    /// Directory containing video files to process
    #[arg(required_unless_present = "list_models")]
    video_dir: Option<PathBuf>,

    /// Name of the TV series (e.g., "Breaking Bad")
    #[arg(required_unless_present = "list_models")]
    show_name: Option<String>,

    /// List all available Whisper models and exit
    #[arg(long)]
    list_models: bool,

    /// Select Whisper model by name (auto-downloads if needed)
    ///
    /// By default, the 'base' model is used. Use this flag to select a different
    /// model from the supported list. Use --list-models to see all available models.
    ///
    /// Examples: tiny, base, small, medium, large-v3-turbo, base-q8_0
    #[arg(long, value_name = "NAME", conflicts_with = "model_path")]
    model: Option<String>,

    /// Override with custom model file path (advanced)
    ///
    /// Use this flag to specify a custom model file path instead of using
    /// the auto-download feature. This is for advanced users with custom models.
    #[arg(long, value_name = "PATH", conflicts_with = "model")]
    model_path: Option<PathBuf>,

    /// Filter to specific season(s) - can be repeated (RECOMMENDED)
    ///
    /// Using season filtering speeds up matching, reduces token usage,
    /// and improves accuracy by providing more focused context to the AI.
    #[arg(short, long = "season", value_name = "N")]
    seasons: Vec<usize>,

    /// AI backend to use for episode matching
    #[arg(short = 'm', long, value_enum, default_value_t = Matcher::GeminiFlash)]
    matcher: Matcher,

    /// Operation mode: what to do after matching
    #[arg(long, value_enum, default_value_t = Mode::DryRun)]
    mode: Mode,

    /// Output directory for copy mode (required when mode=copy)
    #[arg(short = 'o', long, value_name = "DIR")]
    output_dir: Option<PathBuf>,

    /// File naming format
    ///
    /// Supported variables:
    ///   {show}    - Series name
    ///   {season}  - Season number (use {season:02} for zero-padding)
    ///   {episode} - Episode number (use {episode:02} for zero-padding)
    ///   {title}   - Episode title
    ///   {ext}     - Original file extension
    #[arg(
        long,
        default_value = "{show} - S{season:02}E{episode:02} - {title}.{ext}"
    )]
    format: String,
}

/// AI backend selection
#[derive(Clone, Copy, ValueEnum)]
enum Matcher {
    /// Gemini CLI (requires 'gemini' in PATH)
    Gemini,
    /// Gemini CLI with gemini-2.5-flash model (default, requires 'gemini' in PATH)
    GeminiFlash,
    /// Claude Code CLI (requires 'claude' in PATH)
    Claude,
}

impl From<Matcher> for MatcherType {
    fn from(m: Matcher) -> Self {
        match m {
            Matcher::Gemini => MatcherType::Gemini,
            Matcher::GeminiFlash => MatcherType::GeminiFlash,
            Matcher::Claude => MatcherType::Claude,
        }
    }
}

/// Operation mode
#[derive(Clone, Copy, ValueEnum)]
enum Mode {
    /// Show what would happen without making changes (default)
    DryRun,
    /// Rename files in place
    Rename,
    /// Copy files to output directory with new names
    Copy,
}

/// Handles progress events and prints formatted output to stdout
fn handle_progress_event(event: ProgressEvent) {
    match event {
        ProgressEvent::Started { show_name, .. } => {
            println!("🔍 DialogDetective");
            println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
            println!("📺 Investigating: {}", show_name);
        }
        ProgressEvent::FetchingMetadata { .. } => {
            print!("📡 Fetching metadata... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::MetadataFetched { season_count, .. } => {
            println!("✓ ({} seasons)", season_count);
        }
        ProgressEvent::ScanningVideos => {
            print!("🔎 Scanning directory... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::VideosFound { count } => {
            if count == 0 {
                println!("✗ No videos found");
            } else {
                println!("✓ ({} files)", count);
                println!();
            }
        }
        ProgressEvent::ProcessingVideo {
            index,
            total,
            video_path,
        } => {
            let filename = video_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown");
            println!("🎬 [{}/{}] {}", index + 1, total, filename);
        }
        ProgressEvent::Hashing { .. } => {
            print!("   ├─ Computing hash... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::AudioExtraction { .. } => {
            print!("   ├─ Extracting audio... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::Transcription { .. } => {
            print!("   ├─ Transcribing... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::TranscriptionFinished { language, .. } => {
            println!("✓ ({})", language);
        }
        ProgressEvent::TranscriptCacheHit { language, .. } => {
            println!("   ├─ Transcript cached... ✓ ({})", language);
        }
        ProgressEvent::Matching { .. } => {
            print!("   └─ Matching episode... ");
            std::io::Write::flush(&mut std::io::stdout()).ok();
        }
        ProgressEvent::MatchingCacheHit { episode, .. } => {
            println!(
                "   └─ Match cached... ✓ (S{:02}E{:02} - {})",
                episode.season_number, episode.episode_number, episode.name
            );
        }
        ProgressEvent::HashingFinished { .. }
        | ProgressEvent::AudioExtractionFinished { .. }
        | ProgressEvent::MatchingFinished { .. } => {
            println!("");
        }
        ProgressEvent::Complete { .. } => {
            println!();
            println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
        }
    }
}

/// Displays all available Whisper models with download status and exits
fn display_model_list_and_exit() {
    use std::collections::HashMap;

    println!("🔍 Available Whisper Models");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!();

    // Get cache directory
    let cache_dir = match model_downloader::get_cache_dir() {
        Ok(dir) => dir,
        Err(e) => {
            eprintln!("❌ Error: Failed to access cache directory: {}", e);
            process::exit(1);
        }
    };

    println!("📁 Cache directory: {}", cache_dir.display());
    println!();

    // Get list of cached models
    let cached_models = match model_downloader::list_cached_models() {
        Ok(models) => models,
        Err(e) => {
            eprintln!("⚠️  Warning: Failed to read cached models: {}", e);
            Vec::new()
        }
    };

    // Create a map for quick lookup
    let cached_map: HashMap<String, &model_downloader::CachedModelInfo> = cached_models
        .iter()
        .map(|m| (m.model_name.clone(), m))
        .collect();

    // Display all models
    let all_models = model_downloader::supported_models();

    println!("Available Models:");
    for model in all_models.iter() {
        if let Some(info) = cached_map.get(*model) {
            println!("  ✓ {:<30} ({})", model, info.size_human_readable());
        } else {
            println!("  ○ {:<30} (not downloaded)", model);
        }
    }

    println!();
    println!("💡 Tips:");
    println!("  - Use --model <NAME> to select a model (e.g., --model tiny)");
    println!("  - Models are downloaded automatically on first use");
    println!("  - Smaller models are faster but less accurate");
    println!("  - Quantized models have -q suffix (smaller size, slightly lower quality)");
    println!();

    if !cached_models.is_empty() {
        let total_size: u64 = cached_models.iter().map(|m| m.size_bytes).sum();
        println!(
            "📊 Total cached: {} models, {} used",
            cached_models.len(),
            humansize::format_size(total_size, humansize::BINARY)
        );
    }

    process::exit(0);
}

fn main() {
    let cli = Cli::parse();

    // Handle --list-models flag
    if cli.list_models {
        display_model_list_and_exit();
    }

    // Unwrap required arguments (safe because of required_unless_present)
    let video_dir = cli.video_dir.expect("video_dir should be present");
    let show_name = cli.show_name.expect("show_name should be present");

    // Validate arguments
    if !video_dir.exists() {
        eprintln!(
            "❌ Error: Directory does not exist: {}",
            video_dir.display()
        );
        process::exit(1);
    }

    if !video_dir.is_dir() {
        eprintln!("❌ Error: Path is not a directory: {}", video_dir.display());
        process::exit(1);
    }

    // Resolve model path: custom path, selected model, or default 'base'
    let model_path = if let Some(custom_path) = cli.model_path {
        // Custom model path provided - validate it exists
        if !custom_path.exists() {
            eprintln!(
                "❌ Error: Model file does not exist: {}",
                custom_path.display()
            );
            process::exit(1);
        }

        if !custom_path.is_file() {
            eprintln!(
                "❌ Error: Model path is not a file: {}",
                custom_path.display()
            );
            process::exit(1);
        }

        custom_path
    } else {
        // Determine which model to use
        let model_name = cli.model.as_deref().unwrap_or("base");

        // Validate model name against supported list
        let supported = model_downloader::supported_models();
        if !supported.contains(&model_name) {
            eprintln!("❌ Error: Unsupported model '{}'", model_name);
            eprintln!();
            eprintln!("Supported models:");
            for (i, model) in supported.iter().enumerate() {
                eprint!("  {}", model);
                if (i + 1) % 4 == 0 {
                    eprintln!();
                } else {
                    eprint!("  ");
                }
            }
            if supported.len() % 4 != 0 {
                eprintln!();
            }
            eprintln!();
            eprintln!("💡 Tip: Use --list-models to see all available models with details");
            process::exit(1);
        }

        // Download model if needed
        match model_downloader::ensure_model_available(model_name) {
            Ok(path) => path,
            Err(e) => {
                eprintln!(
                    "❌ Error: Failed to download Whisper model '{}': {}",
                    model_name, e
                );
                eprintln!("💡 Tip: You can manually specify a model path with --model-path");
                process::exit(1);
            }
        }
    };

    // Validate mode-specific requirements
    if matches!(cli.mode, Mode::Copy) && cli.output_dir.is_none() {
        eprintln!("❌ Error: --output-dir is required when using --mode copy");
        process::exit(1);
    }

    // Convert seasons filter
    let season_filter = if cli.seasons.is_empty() {
        None
    } else {
        Some(cli.seasons.clone())
    };

    // Run the investigation with progress callback
    match investigate_case(
        &video_dir,
        &model_path,
        &show_name,
        season_filter,
        cli.matcher.into(),
        handle_progress_event,
    ) {
        Ok(matches) => {
            if matches.is_empty() {
                println!("❌ Case closed: No matches found");
                return;
            }

            // Plan file operations
            let output_dir = cli.output_dir.as_deref();
            let operations = match plan_operations(&matches, &show_name, &cli.format, output_dir) {
                Ok(ops) => ops,
                Err(e) => {
                    eprintln!("\n❌ Failed to plan operations: {}", e);
                    process::exit(1);
                }
            };

            // Display results based on mode
            match cli.mode {
                Mode::DryRun => {
                    println!("📋 Dry Run - No files will be modified:");
                    println!();

                    for op in &operations {
                        let source_name = op
                            .source
                            .file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or("unknown");
                        let dest_name = op
                            .destination
                            .file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or("unknown");

                        let operation_type = if output_dir.is_some() {
                            "COPY"
                        } else {
                            "RENAME"
                        };

                        if let Some(suffix) = op.duplicate_suffix {
                            println!(
                                "  [{}] {}{} (duplicate #{})",
                                operation_type, source_name, dest_name, suffix
                            );
                        } else {
                            println!("  [{}] {}{}", operation_type, source_name, dest_name);
                        }
                    }

                    println!("💡 Use --mode rename or --mode copy to apply these changes");
                }

                Mode::Rename => {
                    println!("📝 Renaming files...");
                    println!();

                    match execute_rename(&operations) {
                        Ok(errors) if errors.is_empty() => {
                            for op in &operations {
                                let source_name = op
                                    .source
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");
                                let dest_name = op
                                    .destination
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");

                                println!("{}{}", source_name, dest_name);
                            }
                            println!();
                            println!("✅ Successfully renamed {} file(s)", operations.len());
                        }
                        Ok(errors) => {
                            let success_count = operations.len() - errors.len();

                            println!("⚠️  Operation completed with errors:");
                            println!();
                            println!("✅ Successfully renamed {} file(s)", success_count);
                            println!("❌ Failed to rename {} file(s):", errors.len());

                            for (op, error) in operations.iter().zip(errors.iter()) {
                                let source_name = op
                                    .source
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");
                                println!("{} - {}", source_name, error);
                            }

                            process::exit(1);
                        }
                        Err(e) => {
                            eprintln!("\n❌ Rename operation failed: {}", e);
                            process::exit(1);
                        }
                    }
                }

                Mode::Copy => {
                    let output = cli.output_dir.as_ref().unwrap(); // Safe unwrap, validated earlier
                    println!("📦 Copying files to {}...", output.display());
                    println!();

                    match execute_copy(&operations, output) {
                        Ok(errors) if errors.is_empty() => {
                            for op in &operations {
                                let source_name = op
                                    .source
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");
                                let dest_name = op
                                    .destination
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");

                                println!("{}{}", source_name, dest_name);
                            }
                            println!();
                            println!(
                                "✅ Successfully copied {} file(s) to {}",
                                operations.len(),
                                output.display()
                            );
                        }
                        Ok(errors) => {
                            let success_count = operations.len() - errors.len();

                            println!("⚠️  Operation completed with errors:");
                            println!();
                            println!("✅ Successfully copied {} file(s)", success_count);
                            println!("❌ Failed to copy {} file(s):", errors.len());

                            for (op, error) in operations.iter().zip(errors.iter()) {
                                let source_name = op
                                    .source
                                    .file_name()
                                    .and_then(|n| n.to_str())
                                    .unwrap_or("unknown");
                                println!("{} - {}", source_name, error);
                            }

                            process::exit(1);
                        }
                        Err(e) => {
                            eprintln!("\n❌ Copy operation failed: {}", e);
                            process::exit(1);
                        }
                    }
                }
            }
        }
        Err(e) => {
            eprintln!("\n❌ Investigation failed: {}", e);
            process::exit(1);
        }
    }
}