Skip to main content

ez_ffmpeg/core/context/output/
codec_opts.rs

1use super::Output;
2use std::collections::HashMap;
3
4impl Output {
5    /// Sets a **video codec-specific option**.
6    ///
7    /// These options control **video encoding parameters** such as compression, quality, and speed.
8    ///
9    /// **Supported Parameters:**
10    /// | Parameter | Description |
11    /// |-----------|-------------|
12    /// | `crf=0-51` | Quality level for x264/x265, lower means higher quality (`0` is lossless) |
13    /// | `preset=ultrafast, superfast, fast, medium, slow, veryslow` | Encoding speed, affects compression efficiency |
14    /// | `tune=film, animation, grain, stillimage, fastdecode, zerolatency` | Optimizations for specific types of content |
15    /// | `b=4M` | Bitrate (e.g., `4M` for 4 Mbps) |
16    /// | `g=50` | GOP (Group of Pictures) size, affects keyframe frequency |
17    ///
18    /// **Example Usage:**
19    /// ```rust,ignore
20    /// let output = Output::from("some_url")
21    ///     .set_video_codec_opt("crf", "18")
22    ///     .set_video_codec_opt("preset", "fast");
23    /// ```
24    pub fn set_video_codec_opt(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
25        if let Some(ref mut opts) = self.video_codec_opts {
26            opts.insert(key.into(), value.into());
27        } else {
28            let mut opts = HashMap::new();
29            opts.insert(key.into(), value.into());
30            self.video_codec_opts = Some(opts);
31        }
32        self
33    }
34
35    /// **Sets multiple video codec options at once.**
36    ///
37    /// **Example Usage:**
38    /// ```rust,ignore
39    /// let output = Output::from("some_url")
40    ///     .set_video_codec_opts(vec![
41    ///         ("crf", "18"),
42    ///         ("preset", "fast")
43    ///     ]);
44    /// ```
45    pub fn set_video_codec_opts(
46        mut self,
47        opts: Vec<(impl Into<String>, impl Into<String>)>,
48    ) -> Self {
49        let video_opts = self.video_codec_opts.get_or_insert_with(HashMap::new);
50        for (key, value) in opts {
51            video_opts.insert(key.into(), value.into());
52        }
53        self
54    }
55
56    /// Sets a **audio codec-specific option**.
57    ///
58    /// These options control **audio encoding parameters** such as bitrate, sample rate, and format.
59    ///
60    /// **Supported Parameters:**
61    /// | Parameter | Description |
62    /// |-----------|-------------|
63    /// | `b=192k` | Bitrate (e.g., `128k` for 128 Kbps, `320k` for 320 Kbps) |
64    /// | `compression_level=0-12` | Compression efficiency for formats like FLAC |
65    ///
66    /// **Example Usage:**
67    /// ```rust,ignore
68    /// let output = Output::from("some_url")
69    ///     .set_audio_codec_opt("b", "320k")
70    ///     .set_audio_codec_opt("compression_level", "6");
71    /// ```
72    pub fn set_audio_codec_opt(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
73        if let Some(ref mut opts) = self.audio_codec_opts {
74            opts.insert(key.into(), value.into());
75        } else {
76            let mut opts = HashMap::new();
77            opts.insert(key.into(), value.into());
78            self.audio_codec_opts = Some(opts);
79        }
80        self
81    }
82
83    /// **Sets multiple audio codec options at once.**
84    ///
85    /// **Example Usage:**
86    /// ```rust,ignore
87    /// let output = Output::from("some_url")
88    ///     .set_audio_codec_opts(vec![
89    ///         ("b", "320k"),
90    ///         ("compression_level", "6")
91    ///     ]);
92    /// ```
93    pub fn set_audio_codec_opts(
94        mut self,
95        opts: Vec<(impl Into<String>, impl Into<String>)>,
96    ) -> Self {
97        let audio_opts = self.audio_codec_opts.get_or_insert_with(HashMap::new);
98        for (key, value) in opts {
99            audio_opts.insert(key.into(), value.into());
100        }
101        self
102    }
103
104    /// Sets a **subtitle codec-specific option**.
105    ///
106    /// These options control **subtitle encoding parameters** such as format and character encoding.
107    ///
108    /// **Supported Parameters:**
109    /// | Parameter | Description |
110    /// |-----------|-------------|
111    /// | `mov_text` | Subtitle format for MP4 files |
112    /// | `srt` | Subtitle format for `.srt` files |
113    /// | `ass` | Advanced SubStation Alpha (ASS) subtitle format |
114    /// | `forced_subs=1` | Forces the subtitles to always be displayed |
115    ///
116    /// **Example Usage:**
117    /// ```rust,ignore
118    /// let output = Output::from("some_url")
119    ///     .set_subtitle_codec_opt("mov_text", "");
120    /// ```
121    pub fn set_subtitle_codec_opt(
122        mut self,
123        key: impl Into<String>,
124        value: impl Into<String>,
125    ) -> Self {
126        if let Some(ref mut opts) = self.subtitle_codec_opts {
127            opts.insert(key.into(), value.into());
128        } else {
129            let mut opts = HashMap::new();
130            opts.insert(key.into(), value.into());
131            self.subtitle_codec_opts = Some(opts);
132        }
133        self
134    }
135
136    /// **Sets multiple subtitle codec options at once.**
137    ///
138    /// **Example Usage:**
139    /// ```rust,ignore
140    /// let output = Output::from("some_url")
141    ///     .set_subtitle_codec_opts(vec![
142    ///         ("mov_text", ""),
143    ///         ("forced_subs", "1")
144    ///     ]);
145    /// ```
146    pub fn set_subtitle_codec_opts(
147        mut self,
148        opts: Vec<(impl Into<String>, impl Into<String>)>,
149    ) -> Self {
150        let subtitle_opts = self.subtitle_codec_opts.get_or_insert_with(HashMap::new);
151        for (key, value) in opts {
152            subtitle_opts.insert(key.into(), value.into());
153        }
154        self
155    }
156
157    /// Sets a format-specific option for the output container.
158    ///
159    /// FFmpeg supports various format-specific options that can be passed to the muxer.
160    /// These options allow fine-tuning of the output container’s behavior.
161    ///
162    /// **Example Usage:**
163    /// ```rust,ignore
164    /// let output = Output::from("some_url")
165    ///     .set_format_opt("movflags", "faststart")
166    ///     .set_format_opt("flvflags", "no_duration_filesize");
167    /// ```
168    ///
169    /// ### Common Format Options:
170    /// | Format | Option | Description |
171    /// |--------|--------|-------------|
172    /// | `mp4`  | `movflags=faststart` | Moves moov atom to the beginning of the file for faster playback start |
173    /// | `flv`  | `flvflags=no_duration_filesize` | Removes duration/size metadata for live streaming |
174    ///
175    /// **Parameters:**
176    /// - `key`: The format option name (e.g., `"movflags"`, `"flvflags"`).
177    /// - `value`: The value to set (e.g., `"faststart"`, `"no_duration_filesize"`).
178    ///
179    /// Returns the modified `Output` struct for chaining.
180    pub fn set_format_opt(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
181        if let Some(ref mut opts) = self.format_opts {
182            opts.insert(key.into(), value.into());
183        } else {
184            let mut opts = HashMap::new();
185            opts.insert(key.into(), value.into());
186            self.format_opts = Some(opts);
187        }
188        self
189    }
190
191    /// Sets multiple format-specific options at once.
192    ///
193    /// This method allows setting multiple format options in a single call.
194    ///
195    /// **Example Usage:**
196    /// ```rust,ignore
197    /// let output = Output::from("some_url")
198    ///     .set_format_opts(vec![
199    ///         ("movflags", "faststart"),
200    ///         ("flvflags", "no_duration_filesize")
201    ///     ]);
202    /// ```
203    ///
204    /// **Parameters:**
205    /// - `opts`: A vector of key-value pairs representing format options.
206    ///
207    /// Returns the modified `Output` struct for chaining.
208    pub fn set_format_opts(mut self, opts: Vec<(impl Into<String>, impl Into<String>)>) -> Self {
209        if let Some(ref mut format_opts) = self.format_opts {
210            for (key, value) in opts {
211                format_opts.insert(key.into(), value.into());
212            }
213        } else {
214            let mut format_opts = HashMap::new();
215            for (key, value) in opts {
216                format_opts.insert(key.into(), value.into());
217            }
218            self.format_opts = Some(format_opts);
219        }
220        self
221    }
222}