stream_download/process/
ffmpeg.rs

1use std::ffi::OsString;
2
3use super::Command;
4
5/// Helper to construct a valid `ffmpeg` command that reads from `stdin`, converts the input to the
6/// provided format, and sends the output to `stdout`. This is useful for performing
7/// post-processing on a media stream that needs to be converted to a different format.
8#[derive(Debug)]
9pub struct FfmpegConvertAudioCommand(Command);
10
11impl From<FfmpegConvertAudioCommand> for Command {
12    fn from(value: FfmpegConvertAudioCommand) -> Self {
13        value.0
14    }
15}
16
17impl FfmpegConvertAudioCommand {
18    /// Constructs a new [`FfmpegConvertAudioCommand`].
19    pub fn new<S>(format: S) -> Self
20    where
21        S: AsRef<str>,
22    {
23        // -i pipe: = pipe input from stdin
24        // -map a = select all audio streams
25        // -f <format> output format
26        // - = output to stdout
27        Self(Command::new("ffmpeg").args([
28            "-i",
29            "pipe:",
30            "-map",
31            "a",
32            "-f",
33            format.as_ref(),
34            "-loglevel",
35            "error",
36            "-",
37        ]))
38    }
39
40    /// Sets the path to the `ffmpeg` binary.
41    #[must_use]
42    pub fn ffmpeg_path<S>(mut self, path: S) -> Self
43    where
44        S: Into<OsString>,
45    {
46        self.0.program = path.into();
47        self
48    }
49
50    /// Adds an argument to the [`FfmpegConvertAudioCommand`].
51    #[must_use]
52    pub fn arg<S>(mut self, arg: S) -> Self
53    where
54        S: Into<OsString>,
55    {
56        // '-' needs to be the last arg, so we'll insert everything right before it
57        let insert_pos = self.0.args.len() - 1;
58        self.0 = self.0.insert_arg(insert_pos, arg);
59        self
60    }
61
62    /// Adds multiple args to the [`FfmpegConvertAudioCommand`].
63    #[must_use]
64    pub fn args<I, S>(mut self, args: I) -> Self
65    where
66        I: IntoIterator<Item = S>,
67        S: Into<OsString>,
68    {
69        for arg in args {
70            self = self.arg(arg);
71        }
72        self
73    }
74}