tauri-plugin-media-toolkit 0.1.1

Comprehensive media toolkit for Tauri: edit, play, convert audio/video files
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
use serde::de::DeserializeOwned;
use std::fs;
use std::path::Path;
use std::process::Command;
use std::sync::OnceLock;
use tauri::{plugin::PluginApi, AppHandle, Runtime};

use crate::error::{Error, Result};
use crate::models::*;

/// Cache for FFmpeg availability check
static FFMPEG_AVAILABLE: OnceLock<bool> = OnceLock::new();

/// Check if FFmpeg is installed and available
fn check_ffmpeg_available() -> bool {
    *FFMPEG_AVAILABLE.get_or_init(|| {
        let ffmpeg_ok = Command::new("ffmpeg")
            .arg("-version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);

        let ffprobe_ok = Command::new("ffprobe")
            .arg("-version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);

        let available = ffmpeg_ok && ffprobe_ok;
        if available {
            log::info!("FFmpeg and ffprobe are available");
        } else {
            log::warn!(
                "FFmpeg or ffprobe not found. Media editing features will not work on desktop."
            );
        }
        available
    })
}

/// Ensure FFmpeg is available before running operations
fn ensure_ffmpeg() -> Result<()> {
    if !check_ffmpeg_available() {
        return Err(Error::FFmpegError(
            "FFmpeg is not installed. Please install FFmpeg to use media editing features on desktop.".to_string()
        ));
    }
    Ok(())
}

pub struct MediaEditor<R: Runtime> {
    #[allow(dead_code)]
    app: AppHandle<R>,
}

pub fn init<R: Runtime, C: DeserializeOwned>(
    app: &AppHandle<R>,
    _api: PluginApi<R, C>,
) -> Result<MediaEditor<R>> {
    // Check FFmpeg on init (logs warning if not found)
    check_ffmpeg_available();
    Ok(MediaEditor { app: app.clone() })
}

impl<R: Runtime> MediaEditor<R> {
    /// Get media information using ffprobe
    pub fn get_media_info(&self, path: &str) -> Result<MediaInfo> {
        ensure_ffmpeg()?;

        let path = Path::new(path);
        if !path.exists() {
            return Err(Error::FileNotFound(path.display().to_string()));
        }

        let file_size = fs::metadata(path)?.len();

        // Use ffprobe to get media info
        let output = Command::new("ffprobe")
            .args([
                "-v",
                "quiet",
                "-print_format",
                "json",
                "-show_format",
                "-show_streams",
                path.to_str().unwrap_or(""),
            ])
            .output()
            .map_err(|e| {
                Error::FFmpegError(format!(
                    "Failed to run ffprobe: {}. Make sure ffmpeg is installed.",
                    e
                ))
            })?;

        if !output.status.success() {
            return Err(Error::FFmpegError("ffprobe failed".to_string()));
        }

        let json: serde_json::Value = serde_json::from_slice(&output.stdout)
            .map_err(|e| Error::FFmpegError(e.to_string()))?;

        // Parse duration
        let duration_ms = json["format"]["duration"]
            .as_str()
            .and_then(|d| d.parse::<f64>().ok())
            .map(|d| (d * 1000.0) as u64)
            .unwrap_or(0);

        let format = json["format"]["format_name"]
            .as_str()
            .unwrap_or("unknown")
            .split(',')
            .next()
            .unwrap_or("unknown")
            .to_string();

        let mut media_type = MediaType::Unknown;
        let mut audio_codec = None;
        let mut video_codec = None;
        let mut sample_rate = None;
        let mut channels = None;
        let mut audio_bitrate = None;
        let mut width = None;
        let mut height = None;
        let mut frame_rate = None;
        let mut video_bitrate = None;
        let mut has_audio = false;
        let mut has_video = false;

        if let Some(streams) = json["streams"].as_array() {
            for stream in streams {
                let codec_type = stream["codec_type"].as_str().unwrap_or("");

                match codec_type {
                    "audio" => {
                        has_audio = true;
                        if media_type == MediaType::Unknown {
                            media_type = MediaType::Audio;
                        }
                        audio_codec = stream["codec_name"].as_str().map(String::from);
                        sample_rate = stream["sample_rate"].as_str().and_then(|s| s.parse().ok());
                        channels = stream["channels"].as_u64().map(|c| c as u32);
                        audio_bitrate = stream["bit_rate"].as_str().and_then(|s| s.parse().ok());
                    }
                    "video" => {
                        has_video = true;
                        media_type = MediaType::Video;
                        video_codec = stream["codec_name"].as_str().map(String::from);
                        width = stream["width"].as_u64().map(|w| w as u32);
                        height = stream["height"].as_u64().map(|h| h as u32);
                        video_bitrate = stream["bit_rate"].as_str().and_then(|s| s.parse().ok());

                        // Parse frame rate from "30/1" format
                        if let Some(fr) = stream["r_frame_rate"].as_str() {
                            let parts: Vec<&str> = fr.split('/').collect();
                            if parts.len() == 2 {
                                if let (Ok(num), Ok(den)) =
                                    (parts[0].parse::<f64>(), parts[1].parse::<f64>())
                                {
                                    if den > 0.0 {
                                        frame_rate = Some(num / den);
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        Ok(MediaInfo {
            path: path.display().to_string(),
            media_type,
            duration_ms,
            file_size,
            format,
            has_audio,
            has_video,
            audio_codec,
            video_codec,
            sample_rate,
            channels,
            audio_bitrate,
            width,
            height,
            frame_rate,
            video_bitrate,
        })
    }

    /// Trim media file
    pub fn trim(&self, config: TrimConfig) -> Result<OperationResult> {
        let input_path = Path::new(&config.input_path);
        if !input_path.exists() {
            return Err(Error::FileNotFound(config.input_path.clone()));
        }

        // Get input info
        let info = self.get_media_info(&config.input_path)?;

        // Validate time range
        if config.start_ms >= config.end_ms {
            return Err(Error::InvalidTimeRange(
                config.start_ms,
                config.end_ms,
                info.duration_ms,
            ));
        }
        if config.end_ms > info.duration_ms {
            return Err(Error::InvalidTimeRange(
                config.start_ms,
                config.end_ms,
                info.duration_ms,
            ));
        }

        // Determine output format
        let format = config.format.unwrap_or_else(|| {
            match input_path.extension().and_then(|e| e.to_str()) {
                Some("mp3") => OutputFormat::Mp3,
                Some("mp4") | Some("m4v") => OutputFormat::Mp4,
                Some("wav") => OutputFormat::Wav,
                Some("aac") => OutputFormat::Aac,
                Some("m4a") => OutputFormat::M4a,
                Some("webm") => OutputFormat::Webm,
                Some("ogg") => OutputFormat::Ogg,
                Some("flac") => OutputFormat::Flac,
                _ => OutputFormat::Mp4,
            }
        });

        // Check if trying to convert to audio format without audio stream
        let is_audio_format = matches!(
            format,
            OutputFormat::Mp3
                | OutputFormat::Wav
                | OutputFormat::Aac
                | OutputFormat::M4a
                | OutputFormat::Ogg
                | OutputFormat::Flac
        );
        if is_audio_format && !info.has_audio {
            return Err(Error::FFmpegError(
                "Cannot convert to audio format: source file has no audio stream".to_string(),
            ));
        }

        // Build output path - avoid double extension
        let output_path_str = &config.output_path;
        let expected_ext = format.extension();
        let output_path = if output_path_str.ends_with(&format!(".{}", expected_ext)) {
            output_path_str.clone()
        } else {
            format!("{}.{}", output_path_str, expected_ext)
        };

        // Use ffmpeg CLI for trimming
        let start_sec = config.start_ms as f64 / 1000.0;
        let duration_sec = (config.end_ms - config.start_ms) as f64 / 1000.0;

        let mut cmd = Command::new("ffmpeg");
        cmd.args([
            "-y",
            "-ss",
            &format!("{:.3}", start_sec),
            "-i",
            &config.input_path,
            "-t",
            &format!("{:.3}", duration_sec),
        ]);

        // If output is audio-only format, remove video stream
        if is_audio_format {
            cmd.arg("-vn");
        }

        // Determine codec options
        // Note: Cannot use -c copy when:
        // 1. Converting from video to audio-only format
        // 2. Output format requires different codec (e.g., AAC source -> MP3 output)
        let can_stream_copy =
            config.preserve_quality && !is_audio_format && info.media_type == MediaType::Video;

        if can_stream_copy {
            cmd.args(["-c", "copy"]);
        } else {
            // Audio encoding settings
            let audio_quality = config.audio_quality.unwrap_or_default();
            let audio_bitrate = audio_quality.bitrate();

            // Set explicit audio codec based on output format
            match format {
                OutputFormat::Mp3 => {
                    cmd.args(["-c:a", "libmp3lame"]);
                    if audio_bitrate > 0 {
                        cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                    } else {
                        cmd.args(["-b:a", "192k"]); // Default MP3 bitrate
                    }
                }
                OutputFormat::Aac | OutputFormat::M4a => {
                    cmd.args(["-c:a", "aac"]);
                    if audio_bitrate > 0 {
                        cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                    }
                }
                OutputFormat::Wav => {
                    cmd.args(["-c:a", "pcm_s16le"]);
                }
                OutputFormat::Aiff => {
                    cmd.args(["-c:a", "pcm_s16be"]);
                }
                OutputFormat::Caf => {
                    cmd.args(["-c:a", "pcm_s16le"]);
                }
                OutputFormat::Flac => {
                    cmd.args(["-c:a", "flac"]);
                }
                OutputFormat::Ogg => {
                    cmd.args(["-c:a", "libvorbis"]);
                    if audio_bitrate > 0 {
                        cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                    }
                }
                OutputFormat::Webm => {
                    cmd.args(["-c:a", "libopus"]);
                    if audio_bitrate > 0 {
                        cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                    }
                }
                OutputFormat::Mp4 => {
                    // For MP4, use AAC for audio
                    if info.has_audio {
                        if config.preserve_quality {
                            cmd.args(["-c:a", "copy"]);
                        } else if audio_bitrate > 0 {
                            cmd.args([
                                "-c:a",
                                "aac",
                                "-b:a",
                                &format!("{}k", audio_bitrate / 1000),
                            ]);
                        }
                    }
                }
            }

            // Only add video encoding options if output has video
            if !is_audio_format && info.media_type == MediaType::Video {
                if config.preserve_quality {
                    // Try to preserve video quality even when re-encoding audio
                    cmd.args(["-c:v", "copy"]);
                } else {
                    let video_quality = config.video_quality.unwrap_or_default();
                    match video_quality {
                        VideoQuality::Low => {
                            cmd.args(["-crf", "28", "-preset", "fast"]);
                        }
                        VideoQuality::Medium => {
                            cmd.args(["-crf", "23", "-preset", "medium"]);
                        }
                        VideoQuality::High => {
                            cmd.args(["-crf", "18", "-preset", "slow"]);
                        }
                        VideoQuality::Original => {
                            cmd.args(["-c:v", "copy"]);
                        }
                    }
                }
            }
        }

        cmd.arg(&output_path);

        let output = cmd.output().map_err(|e| {
            Error::FFmpegError(format!(
                "Failed to run ffmpeg: {}. Make sure ffmpeg is installed.",
                e
            ))
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::FFmpegError(format!("FFmpeg failed: {}", stderr)));
        }

        // Get output file info
        let output_meta = fs::metadata(&output_path)?;

        Ok(OperationResult {
            success: true,
            output_path,
            duration_ms: config.end_ms - config.start_ms,
            file_size: output_meta.len(),
            warning: None,
        })
    }

    /// Convert media file
    pub fn convert(&self, config: ConvertConfig) -> Result<OperationResult> {
        let input_path = Path::new(&config.input_path);
        if !input_path.exists() {
            return Err(Error::FileNotFound(config.input_path.clone()));
        }

        let info = self.get_media_info(&config.input_path)?;

        // Check if trying to convert to audio format without audio stream
        let is_audio_format = matches!(
            config.format,
            OutputFormat::Mp3
                | OutputFormat::Wav
                | OutputFormat::Aac
                | OutputFormat::M4a
                | OutputFormat::Ogg
                | OutputFormat::Flac
        );
        if is_audio_format && !info.has_audio {
            return Err(Error::FFmpegError(
                "Cannot convert to audio format: source file has no audio stream".to_string(),
            ));
        }

        // Build output path - avoid double extension
        let output_path_str = &config.output_path;
        let expected_ext = config.format.extension();
        let output_path = if output_path_str.ends_with(&format!(".{}", expected_ext)) {
            output_path_str.clone()
        } else {
            format!("{}.{}", output_path_str, expected_ext)
        };

        let mut cmd = Command::new("ffmpeg");
        cmd.args(["-y", "-i", &config.input_path]);

        // If output is audio-only format, remove video stream
        if is_audio_format {
            cmd.arg("-vn");
        }

        // Audio quality settings
        let audio_quality = config.audio_quality.unwrap_or_default();
        let audio_bitrate = audio_quality.bitrate();

        // Set explicit audio codec based on output format
        match config.format {
            OutputFormat::Mp3 => {
                cmd.args(["-c:a", "libmp3lame"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                } else {
                    cmd.args(["-b:a", "192k"]);
                }
            }
            OutputFormat::Aac | OutputFormat::M4a => {
                cmd.args(["-c:a", "aac"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Wav => {
                cmd.args(["-c:a", "pcm_s16le"]);
            }
            OutputFormat::Aiff => {
                cmd.args(["-c:a", "pcm_s16be"]);
            }
            OutputFormat::Caf => {
                cmd.args(["-c:a", "pcm_s16le"]);
            }
            OutputFormat::Flac => {
                cmd.args(["-c:a", "flac"]);
            }
            OutputFormat::Ogg => {
                cmd.args(["-c:a", "libvorbis"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Webm => {
                cmd.args(["-c:a", "libopus"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Mp4 => {
                // For MP4, use AAC for audio
                if info.has_audio && audio_bitrate > 0 {
                    cmd.args(["-c:a", "aac", "-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
        }

        // Video quality (if applicable and output has video)
        if !is_audio_format && info.media_type == MediaType::Video {
            let video_quality = config.video_quality.unwrap_or_default();
            match video_quality {
                VideoQuality::Low => {
                    cmd.args(["-c:v", "libx264", "-crf", "28", "-preset", "fast"]);
                }
                VideoQuality::Medium => {
                    cmd.args(["-c:v", "libx264", "-crf", "23", "-preset", "medium"]);
                }
                VideoQuality::High => {
                    cmd.args(["-c:v", "libx264", "-crf", "18", "-preset", "slow"]);
                }
                VideoQuality::Original => {
                    cmd.args(["-c:v", "copy"]);
                }
            }
        }

        cmd.arg(&output_path);

        let output = cmd.output().map_err(|e| {
            Error::FFmpegError(format!(
                "Failed to run ffmpeg: {}. Make sure ffmpeg is installed.",
                e
            ))
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::FFmpegError(format!("FFmpeg failed: {}", stderr)));
        }

        let output_info = self.get_media_info(&output_path)?;

        Ok(OperationResult {
            success: true,
            output_path,
            duration_ms: output_info.duration_ms,
            file_size: output_info.file_size,
            warning: None,
        })
    }

    /// Extract audio from video
    pub fn extract_audio(&self, config: ExtractAudioConfig) -> Result<OperationResult> {
        let input_path = Path::new(&config.input_path);
        if !input_path.exists() {
            return Err(Error::FileNotFound(config.input_path.clone()));
        }

        // Check if source has audio
        let info = self.get_media_info(&config.input_path)?;
        if !info.has_audio {
            return Err(Error::FFmpegError(
                "Cannot extract audio: source file has no audio stream".to_string(),
            ));
        }

        // Build output path - avoid double extension
        let output_path_str = &config.output_path;
        let expected_ext = config.format.extension();
        let output_path = if output_path_str.ends_with(&format!(".{}", expected_ext)) {
            output_path_str.clone()
        } else {
            format!("{}.{}", output_path_str, expected_ext)
        };

        let audio_quality = config.audio_quality.unwrap_or_default();
        let audio_bitrate = audio_quality.bitrate();

        let mut cmd = Command::new("ffmpeg");
        cmd.args(["-y", "-i", &config.input_path, "-vn"]); // -vn removes video

        // Set explicit audio codec based on output format
        match config.format {
            OutputFormat::Mp3 => {
                cmd.args(["-c:a", "libmp3lame"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                } else {
                    cmd.args(["-b:a", "192k"]);
                }
            }
            OutputFormat::Aac | OutputFormat::M4a => {
                cmd.args(["-c:a", "aac"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Wav => {
                cmd.args(["-c:a", "pcm_s16le"]);
            }
            OutputFormat::Aiff => {
                cmd.args(["-c:a", "pcm_s16be"]);
            }
            OutputFormat::Caf => {
                cmd.args(["-c:a", "pcm_s16le"]);
            }
            OutputFormat::Flac => {
                cmd.args(["-c:a", "flac"]);
            }
            OutputFormat::Ogg => {
                cmd.args(["-c:a", "libvorbis"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Webm => {
                cmd.args(["-c:a", "libopus"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
            OutputFormat::Mp4 => {
                // For MP4 audio extraction, use AAC
                cmd.args(["-c:a", "aac"]);
                if audio_bitrate > 0 {
                    cmd.args(["-b:a", &format!("{}k", audio_bitrate / 1000)]);
                }
            }
        }

        cmd.arg(&output_path);

        let output = cmd.output().map_err(|e| {
            Error::FFmpegError(format!(
                "Failed to run ffmpeg: {}. Make sure ffmpeg is installed.",
                e
            ))
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::FFmpegError(format!("FFmpeg failed: {}", stderr)));
        }

        let output_info = self.get_media_info(&output_path)?;

        Ok(OperationResult {
            success: true,
            output_path,
            duration_ms: output_info.duration_ms,
            file_size: output_info.file_size,
            warning: None,
        })
    }

    /// Start playback using ffplay
    pub fn play(&self, config: PlayConfig) -> Result<()> {
        ensure_ffmpeg()?;

        let path = Path::new(&config.file_path);
        if !path.exists() {
            return Err(Error::FileNotFound(config.file_path.clone()));
        }

        // Get media info to determine if it's audio or video
        let info = self.get_media_info(&config.file_path)?;
        let volume = config.volume.unwrap_or(1.0);

        // Use ffplay for playback
        let mut cmd = Command::new("ffplay");

        // Common args
        cmd.args([
            "-autoexit",
            "-volume",
            &format!("{}", (volume * 100.0) as u32),
        ]);

        // For audio-only files, hide the display window
        // For video files, show the video window
        if info.media_type == MediaType::Audio {
            cmd.arg("-nodisp");
        } else {
            // Add window title for video
            cmd.args(["-window_title", "Affex Media Player"]);
            // Handle video-only files (like YouTube DASH) that may not have audio
            if !info.has_audio {
                cmd.arg("-an"); // Disable audio processing
            }
        }

        // Add input file
        cmd.arg(&config.file_path);

        // Run ffplay in background
        std::thread::spawn(move || {
            let output = cmd.output();
            if let Err(e) = output {
                log::error!("ffplay error: {}", e);
            } else if let Ok(out) = output {
                if !out.status.success() {
                    let stderr = String::from_utf8_lossy(&out.stderr);
                    log::error!("ffplay failed: {}", stderr);
                }
            }
        });

        log::info!("Playback started: {}", config.file_path);
        Ok(())
    }

    /// Pause playback (not supported with ffplay)
    pub fn pause(&self) -> Result<()> {
        log::warn!("Pause not supported with ffplay backend");
        Err(Error::NotImplemented)
    }

    /// Resume playback (not supported with ffplay)
    pub fn resume(&self) -> Result<()> {
        log::warn!("Resume not supported with ffplay backend");
        Err(Error::NotImplemented)
    }

    /// Stop playback (kills ffplay process)
    pub fn stop(&self) -> Result<()> {
        // Kill any running ffplay process
        #[cfg(target_os = "windows")]
        let _ = Command::new("taskkill")
            .args(["/IM", "ffplay.exe", "/F"])
            .output();

        #[cfg(not(target_os = "windows"))]
        let _ = Command::new("pkill").args(["-f", "ffplay"]).output();

        log::info!("Playback stopped");
        Ok(())
    }

    /// Seek (not supported with ffplay)
    pub fn seek(&self, _config: SeekConfig) -> Result<()> {
        log::warn!("Seek not supported with ffplay backend");
        Err(Error::NotImplemented)
    }

    /// Get playback status (limited info with ffplay)
    pub fn get_playback_status(&self) -> Result<PlaybackStatus> {
        // Check if ffplay is running
        #[cfg(not(target_os = "windows"))]
        let output = Command::new("pgrep").args(["-f", "ffplay"]).output();

        #[cfg(target_os = "windows")]
        let output = Command::new("tasklist")
            .args(["/FI", "IMAGENAME eq ffplay.exe"])
            .output();

        let is_playing = output
            .map(|o| o.status.success() && !o.stdout.is_empty())
            .unwrap_or(false);

        Ok(PlaybackStatus {
            state: if is_playing {
                PlaybackState::Playing
            } else {
                PlaybackState::Idle
            },
            position_ms: 0,
            duration_ms: 0,
            volume: 1.0,
            file_path: None,
        })
    }

    /// Set volume (not supported with ffplay after start)
    pub fn set_volume(&self, _volume: f32) -> Result<()> {
        log::warn!("Set volume not supported with ffplay backend");
        Err(Error::NotImplemented)
    }

    /// Select media file (not supported on desktop - use dialog plugin instead)
    pub fn select_media_file(&self) -> Result<crate::models::FileSelectionResult> {
        log::warn!(
            "select_media_file is only supported on Android. Use tauri-plugin-dialog on desktop."
        );
        Err(Error::NotImplemented)
    }

    /// Check permission (always granted on desktop)
    pub fn check_permission(&self) -> Result<crate::models::PermissionResponse> {
        Ok(crate::models::PermissionResponse {
            granted: true,
            can_request: true,
        })
    }

    /// Request permission (always granted on desktop)
    pub fn request_permission(&self) -> Result<crate::models::PermissionResponse> {
        Ok(crate::models::PermissionResponse {
            granted: true,
            can_request: true,
        })
    }

    /// Cleanup cache (no-op on desktop - temp files handled by OS)
    pub fn cleanup_cache(&self) -> Result<crate::models::CleanupResult> {
        Ok(crate::models::CleanupResult {
            success: true,
            files_deleted: 0,
            bytes_freed: 0,
        })
    }
}