ffmpeg-common 0.1.0

Common library for FFmpeg Suite RS
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
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::time::timeout;
use tracing::{debug, trace};
use which::which;

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

/// Find the path to an FFmpeg executable
pub fn find_executable(name: &str) -> Result<PathBuf> {
    which(name).map_err(|_| Error::ExecutableNotFound(name.to_string()))
}

/// Process execution configuration
#[derive(Debug, Clone)]
pub struct ProcessConfig {
    /// Executable path
    pub executable: PathBuf,
    /// Working directory
    pub working_dir: Option<PathBuf>,
    /// Environment variables
    pub env: Vec<(String, String)>,
    /// Timeout for the process
    pub timeout: Option<Duration>,
    /// Whether to capture stdout
    pub capture_stdout: bool,
    /// Whether to capture stderr
    pub capture_stderr: bool,
    /// Whether to pipe stdin
    pub pipe_stdin: bool,
}

impl ProcessConfig {
    /// Create a new process configuration
    pub fn new(executable: impl Into<PathBuf>) -> Self {
        Self {
            executable: executable.into(),
            working_dir: None,
            env: Vec::new(),
            timeout: None,
            capture_stdout: true,
            capture_stderr: true,
            pipe_stdin: false,
        }
    }

    /// Set working directory
    pub fn working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.working_dir = Some(dir.into());
        self
    }

    /// Add environment variable
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.push((key.into(), value.into()));
        self
    }

    /// Set timeout
    pub fn timeout(mut self, duration: Duration) -> Self {
        self.timeout = Some(duration);
        self
    }

    /// Set stdout capture
    pub fn capture_stdout(mut self, capture: bool) -> Self {
        self.capture_stdout = capture;
        self
    }

    /// Set stderr capture
    pub fn capture_stderr(mut self, capture: bool) -> Self {
        self.capture_stderr = capture;
        self
    }

    /// Set stdin piping
    pub fn pipe_stdin(mut self, pipe: bool) -> Self {
        self.pipe_stdin = pipe;
        self
    }
}

/// Process handle for running FFmpeg processes
pub struct Process {
    child: Child,
    config: ProcessConfig,
}

impl Process {
    /// Spawn a new process with arguments
    pub async fn spawn(config: ProcessConfig, args: Vec<String>) -> Result<Self> {
        debug!("Spawning process: {} {:?}", config.executable.display(), args);

        let mut cmd = Command::new(&config.executable);

        // Add arguments
        for arg in &args {
            cmd.arg(arg);
        }

        // Set working directory
        if let Some(ref dir) = config.working_dir {
            cmd.current_dir(dir);
        }

        // Set environment variables
        for (key, value) in &config.env {
            cmd.env(key, value);
        }

        // Configure stdio
        cmd.stdin(if config.pipe_stdin {
            Stdio::piped()
        } else {
            Stdio::null()
        });

        cmd.stdout(if config.capture_stdout {
            Stdio::piped()
        } else {
            Stdio::null()
        });

        cmd.stderr(if config.capture_stderr {
            Stdio::piped()
        } else {
            Stdio::null()
        });

        // Kill on drop
        cmd.kill_on_drop(true);

        let child = cmd.spawn().map_err(Error::Io)?;

        Ok(Self { child, config })
    }

    /// Wait for the process to complete
    pub async fn wait(mut self) -> Result<ProcessOutput> {
        // This async block will capture the process output.
        // We explicitly map `std::io::Error` to our custom `Error::Io` variant
        // to resolve the compiler's type inference ambiguity.
        let wait_future = async {
            let status = self.child.wait().await.map_err(Error::Io)?;

            let stdout = if self.config.capture_stdout {
                if let Some(mut stdout) = self.child.stdout.take() {
                    let mut buf = Vec::new();
                    stdout.read_to_end(&mut buf).await.map_err(Error::Io)?;
                    Some(buf)
                } else {
                    None
                }
            } else {
                None
            };

            let stderr = if self.config.capture_stderr {
                if let Some(mut stderr) = self.child.stderr.take() {
                    let mut buf = Vec::new();
                    stderr.read_to_end(&mut buf).await.map_err(Error::Io)?;
                    Some(buf)
                } else {
                    None
                }
            } else {
                None
            };

            Ok(ProcessOutput {
                status,
                stdout,
                stderr,
            })
        };

        if let Some(timeout_duration) = self.config.timeout {
            match timeout(timeout_duration, wait_future).await {
                // The future completed without timing out. `result` is the `Result` from our future.
                Ok(result) => result,
                // The future timed out.
                Err(_) => {
                    let _ = self.child.kill().await;
                    Err(Error::Timeout(timeout_duration))
                }
            }
        } else {
            // No timeout configured, just await the future.
            wait_future.await
        }
    }

    /// Get a handle to stdin
    pub fn stdin(&mut self) -> Option<tokio::process::ChildStdin> {
        self.child.stdin.take()
    }

    /// Get a handle to stdout
    pub fn stdout(&mut self) -> Option<tokio::process::ChildStdout> {
        self.child.stdout.take()
    }

    /// Get a handle to stderr
    pub fn stderr(&mut self) -> Option<tokio::process::ChildStderr> {
        self.child.stderr.take()
    }

    /// Kill the process
    pub async fn kill(&mut self) -> Result<()> {
        self.child.kill().await.map_err(Error::Io)
    }

    /// Get the process ID
    pub fn id(&self) -> Option<u32> {
        self.child.id()
    }

    /// Try to wait for the process without blocking
    pub fn try_wait(&mut self) -> Result<Option<std::process::ExitStatus>> {
        // Explicitly map the error to avoid ambiguity with the `?` operator.
        self.child.try_wait().map_err(Error::Io)
    }
}

/// Output from a completed process
#[derive(Debug)]
pub struct ProcessOutput {
    /// Exit status
    pub status: std::process::ExitStatus,
    /// Stdout data if captured
    pub stdout: Option<Vec<u8>>,
    /// Stderr data if captured
    pub stderr: Option<Vec<u8>>,
}

impl ProcessOutput {
    /// Check if the process succeeded
    pub fn success(&self) -> bool {
        self.status.success()
    }

    /// Get stdout as string
    pub fn stdout_str(&self) -> Option<String> {
        self.stdout.as_ref().map(|b| String::from_utf8_lossy(b).into_owned())
    }

    /// Get stderr as string
    pub fn stderr_str(&self) -> Option<String> {
        self.stderr.as_ref().map(|b| String::from_utf8_lossy(b).into_owned())
    }

    /// Convert to a Result, treating non-zero exit as error
    pub fn into_result(self) -> Result<Self> {
        if self.success() {
            Ok(self)
        } else {
            Err(Error::process_failed(
                format!("Process exited with status: {}", self.status),
                Some(self.status),
                self.stderr_str(),
            ))
        }
    }
}

/// Progress information from FFmpeg
#[derive(Debug, Clone)]
pub struct Progress {
    /// Current frame number
    pub frame: Option<u64>,
    /// Frames per second
    pub fps: Option<f64>,
    /// Quality factor
    pub q: Option<f64>,
    /// Current size in bytes
    pub size: Option<u64>,
    /// Current time position
    pub time: Option<Duration>,
    /// Bitrate in bits/s
    pub bitrate: Option<f64>,
    /// Processing speed
    pub speed: Option<f64>,
}

impl Progress {
    /// Parse progress from FFmpeg stderr line
    pub fn parse_line(line: &str) -> Option<Self> {
        if !line.contains("frame=") {
            return None;
        }

        let mut progress = Progress {
            frame: None,
            fps: None,
            q: None,
            size: None,
            time: None,
            bitrate: None,
            speed: None,
        };

        // This is a more robust way to parse the key-value pairs from FFmpeg,
        // which can have inconsistent spacing (e.g., "key=value" or "key= value").
        let parts: Vec<&str> = line.split_whitespace().collect();
        let mut i = 0;
        while i < parts.len() {
            if let Some((key, mut value)) = parts[i].split_once('=') {
                // If `split_once` gives an empty value, it means the actual value
                // is the next element in `parts` (e.g., "frame=", "100").
                if value.is_empty() {
                    if let Some(next_part) = parts.get(i + 1) {
                        value = next_part;
                        i += 1; // Manually advance to skip the value part in the next iteration.
                    }
                }

                match key.trim() {
                    "frame" => progress.frame = value.trim().parse().ok(),
                    "fps" => progress.fps = value.trim().parse().ok(),
                    "q" => progress.q = value.trim().parse().ok(),
                    "size" => {
                        // Remove "kB" suffix and convert to bytes
                        if let Some(kb_str) = value.trim().strip_suffix("kB") {
                            progress.size = kb_str.parse::<u64>().ok().map(|kb| kb * 1024);
                        }
                    }
                    "time" => {
                        // Parse time in HH:MM:SS.MS format
                        if let Ok(duration) = crate::types::Duration::from_ffmpeg_format(value.trim()) {
                            progress.time = Some(duration.into());
                        }
                    }
                    "bitrate" => {
                        // Remove "kbits/s" suffix
                        if let Some(kbits_str) = value.trim().strip_suffix("kbits/s") {
                            progress.bitrate = kbits_str.parse::<f64>().ok().map(|kb| kb * 1000.0);
                        }
                    }
                    "speed" => {
                        // Remove "x" suffix
                        if let Some(speed_str) = value.trim().strip_suffix('x') {
                            progress.speed = speed_str.parse().ok();
                        }
                    }
                    _ => {}
                }
            }
            i += 1;
        }

        Some(progress)
    }
}

/// Progress callback type
pub type ProgressCallback = Box<dyn Fn(Progress) + Send + Sync>;

/// Stream progress updates from FFmpeg stderr
pub async fn stream_progress<R: AsyncRead + Unpin + Send + 'static>(
    stderr: R,
    mut callback: impl FnMut(Progress) + Send + 'static,
) {
    let reader = BufReader::new(stderr);
    let mut lines = reader.lines();

    while let Ok(Some(line)) = lines.next_line().await {
        trace!("FFmpeg stderr: {}", line);
        if let Some(progress) = Progress::parse_line(&line) {
            callback(progress);
        }
    }
}

/// Command builder with safe argument construction
#[derive(Debug, Clone)]
pub struct CommandBuilder {
    args: Vec<String>,
}

impl CommandBuilder {
    /// Create a new command builder
    pub fn new() -> Self {
        Self { args: Vec::new() }
    }

    /// Add a flag (no value)
    pub fn flag(mut self, flag: impl AsRef<str>) -> Self {
        self.args.push(flag.as_ref().to_string());
        self
    }

    /// Add an option with a value
    pub fn option(mut self, key: impl AsRef<str>, value: impl ToString) -> Self {
        self.args.push(key.as_ref().to_string());
        self.args.push(value.to_string());
        self
    }

    /// Add an option only if the value is Some
    pub fn option_if_some<T: ToString>(self, key: impl AsRef<str>, value: Option<T>) -> Self {
        if let Some(val) = value {
            self.option(key, val)
        } else {
            self
        }
    }

    /// Add a flag only if the condition is true
    pub fn flag_if(self, flag: impl AsRef<str>, condition: bool) -> Self {
        if condition {
            self.flag(flag)
        } else {
            self
        }
    }

    /// Add raw arguments
    pub fn args(mut self, args: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
        for arg in args {
            self.args.push(arg.as_ref().to_string());
        }
        self
    }

    /// Add raw argument
    pub fn arg(mut self, arg: impl AsRef<str>) -> Self {
        self.args.push(arg.as_ref().to_string());
        self
    }

    /// Build into a vector of arguments
    pub fn build(self) -> Vec<String> {
        self.args
    }
}

impl Default for CommandBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper to validate paths exist
pub fn validate_input_path(path: &Path) -> Result<()> {
    if !path.exists() {
        return Err(Error::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("Input file not found: {}", path.display()),
        )));
    }
    Ok(())
}

/// Helper to validate output path can be written
pub fn validate_output_path(path: &Path) -> Result<()> {
    if let Some(parent) = path.parent() {
        if !parent.exists() {
            return Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Output directory does not exist: {}", parent.display()),
            )));
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_command_builder() {
        let args = CommandBuilder::new()
            .flag("-y")
            .option("-i", "input.mp4")
            .option_if_some("-ss", Some("00:00:10"))
            .option_if_some("-t", None::<&str>)
            .flag_if("-n", false)
            .arg("output.mp4")
            .build();

        assert_eq!(args, vec!["-y", "-i", "input.mp4", "-ss", "00:00:10", "output.mp4"]);
    }

    #[test]
    fn test_progress_parsing() {
        let line = "frame=  100 fps=25.0 q=28.0 size=    1024kB time=00:00:04.00 bitrate=2097.2kbits/s speed=1.00x";
        let progress = Progress::parse_line(line).unwrap();

        assert_eq!(progress.frame, Some(100));
        assert_eq!(progress.fps, Some(25.0));
        assert_eq!(progress.q, Some(28.0));
        assert_eq!(progress.size, Some(1024 * 1024));
        assert_eq!(progress.time, Some(Duration::from_secs(4)));
        assert_eq!(progress.bitrate, Some(2_097_200.0));
        assert_eq!(progress.speed, Some(1.0));
    }
}