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
//! Wraps the ffpmeg cli, using `-progress` to report progress
//!
//! Sometimes you just want a simple way to use ffmpeg. Most crates just use ffi, leading to
//! complicated interfaces. `ffmpeg_cli` avoids this by wrapping the cli, for when you don't need
//! the flexibility the real ffmpeg api gives you.
//!
//! ```no_run
//! use std::process::Stdio;
//!
//! use ffmpeg_cli::{FfmpegBuilder, File, Parameter};
//! use futures::{future::ready, StreamExt};
//!
//! #[tokio::main]
//! async fn main() {
//!     let builder = FfmpegBuilder::new()
//!         .stderr(Stdio::piped())
//!         .option(Parameter::Single("nostdin"))
//!         .option(Parameter::Single("y"))
//!         .input(File::new("input.mkv"))
//!         .output(
//!             File::new("output.mp4")
//!                 .option(Parameter::KeyValue("vcodec", "libx265"))
//!                 .option(Parameter::KeyValue("crf", "28")),
//!         );
//!
//!     let ffmpeg = builder.run().await.unwrap();
//!
//!     ffmpeg
//!         .progress
//!         .for_each(|x| {
//!             dbg!(x.unwrap());
//!             ready(())
//!         })
//!         .await;
//!
//!     let output = ffmpeg.process.wait_with_output().unwrap();
//!
//!     println!(
//!         "{}\nstderr:\n{}",
//!         output.status,
//!         std::str::from_utf8(&output.stderr).unwrap()
//!     );
//! }
//! ```
#![warn(missing_docs)]

use std::process::{Command, Stdio};

mod runner;

#[doc(inline)]
pub use runner::*;

/// The main struct which is used to set up ffmpeg.
#[derive(Debug)]
pub struct FfmpegBuilder<'a> {
    /// The global options.
    pub options: Vec<Parameter<'a>>,
    /// The input files.
    pub inputs: Vec<File<'a>>,
    /// The output files.
    pub outputs: Vec<File<'a>>,

    /// The command that's run for ffmpeg. Usually just `ffmpeg`
    pub ffmpeg_command: &'a str,
    /// Passed as [Command::stdin]
    pub stdin: Stdio,
    /// Passed as [Command::stdout]
    pub stdout: Stdio,
    /// Passed as [Command::stderr]
    pub stderr: Stdio,
}

/// A file that ffmpeg operates on.
///
/// This can be an input or output, it depends on what you add it as.
#[derive(Debug)]
pub struct File<'a> {
    /// The url of the file.
    ///
    /// As with ffmpeg, just a normal path works.
    pub url: &'a str,
    /// The options corresponding to this file.
    pub options: Vec<Parameter<'a>>,
}

/// A global or file option to be passed to ffmpeg.
#[derive(Debug)]
pub enum Parameter<'a> {
    /// An option which does not take a value, ex. `-autorotate`.
    ///
    /// `-autorotate` would be represented as `Single("autorotate")`,
    /// as the `-` is inserted automatically.
    Single(&'a str),
    /// An option that takes a key and a value, ex. `-t 10`.
    ///
    /// `-t 10` would be represented as `KeyValue("t", "10")`, as
    /// the `-` is inserted automatically.
    KeyValue(&'a str, &'a str),
}

impl<'a> FfmpegBuilder<'a> {
    /// Gets a [FfmpegBuilder] with nothing set
    pub fn new() -> FfmpegBuilder<'a> {
        FfmpegBuilder {
            options: Vec::new(),
            inputs: Vec::new(),
            outputs: Vec::new(),
            ffmpeg_command: "ffmpeg",
            stdin: Stdio::null(),
            stdout: Stdio::null(),
            stderr: Stdio::null(),
        }
    }

    /// Adds an option.
    pub fn option(mut self, option: Parameter<'a>) -> Self {
        self.options.push(option);

        self
    }

    /// Adds an input.
    pub fn input(mut self, input: File<'a>) -> Self {
        self.inputs.push(input);

        self
    }

    /// Adds an output.
    pub fn output(mut self, output: File<'a>) -> Self {
        self.outputs.push(output);

        self
    }

    /// Sets stdin.
    pub fn stdin(mut self, stdin: Stdio) -> Self {
        self.stdin = stdin;

        self
    }

    /// Sets stdout.
    pub fn stdout(mut self, stdout: Stdio) -> Self {
        self.stdout = stdout;

        self
    }

    /// Sets stderr.
    pub fn stderr(mut self, stderr: Stdio) -> Self {
        self.stderr = stderr;

        self
    }

    /// Turns it into a command, consuming the builder.
    ///
    /// This has to consume the builder for stdin, etc to work
    /// Note that usually you want to use [`Self::run()`], not call this directly
    pub fn to_command(self) -> Command {
        let mut command = Command::new(self.ffmpeg_command);

        for option in self.options {
            option.push_to(&mut command);
        }
        for input in self.inputs {
            input.push_to(&mut command, true);
        }
        for output in self.outputs {
            output.push_to(&mut command, false)
        }

        command.stdin(self.stdin);
        command.stdout(self.stdout);
        command.stderr(self.stderr);

        command
    }
}

impl<'a> File<'a> {
    /// Gets a file without any options set.
    pub fn new(url: &'a str) -> File {
        File {
            url,
            options: Vec::new(),
        }
    }

    /// Adds an option.
    pub fn option(mut self, option: Parameter<'a>) -> Self {
        self.options.push(option);

        self
    }

    fn push_to(&self, command: &mut Command, input: bool) {
        for option in &self.options {
            option.push_to(command);
        }

        if input {
            command.arg("-i");
        }
        command.arg(&self.url);
    }
}

impl<'a> Parameter<'a> {
    fn push_to(&self, command: &mut Command) {
        match &self {
            Parameter::Single(arg) => command.arg("-".to_owned() + arg),
            Parameter::KeyValue(key, value) => {
                command.arg("-".to_owned() + key);
                command.arg(value)
            }
        };
    }
}