rust_ffmpeg 1.0.0

Safe and idiomatic Rust wrapper for FFmpeg
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
use std::collections::HashMap;
use ffmpeg_common::{StreamSpecifier, StreamType};
use std::fmt;

/// Stream mapping configuration
#[derive(Debug, Clone)]
pub struct StreamMap {
    /// Input file index
    input_index: usize,
    /// Stream specifier
    stream_spec: Option<StreamSpecifier>,
    /// Whether this is a negative mapping (exclude)
    negative: bool,
}

impl StreamMap {
    /// Map all streams from an input
    pub fn from_input(input_index: usize) -> Self {
        Self {
            input_index,
            stream_spec: None,
            negative: false,
        }
    }

    /// Map a specific stream
    pub fn specific(input_index: usize, stream_spec: StreamSpecifier) -> Self {
        Self {
            input_index,
            stream_spec: Some(stream_spec),
            negative: false,
        }
    }

    /// Map video streams from input
    pub fn video_from(input_index: usize) -> Self {
        Self::specific(input_index, StreamSpecifier::Type(StreamType::Video))
    }

    /// Map audio streams from input
    pub fn audio_from(input_index: usize) -> Self {
        Self::specific(input_index, StreamSpecifier::Type(StreamType::Audio))
    }

    /// Map subtitle streams from input
    pub fn subtitle_from(input_index: usize) -> Self {
        Self::specific(input_index, StreamSpecifier::Type(StreamType::Subtitle))
    }

    /// Map a specific stream by index
    pub fn stream_index(input_index: usize, stream_index: usize) -> Self {
        Self::specific(input_index, StreamSpecifier::Index(stream_index))
    }

    /// Exclude this mapping (negative map)
    pub fn exclude(mut self) -> Self {
        self.negative = true;
        self
    }

    /// Convert to command line format
    pub fn to_string(&self) -> String {
        let mut result = String::new();

        if self.negative {
            result.push('-');
        }

        result.push_str(&self.input_index.to_string());

        if let Some(ref spec) = self.stream_spec {
            result.push(':');
            result.push_str(&spec.to_string());
        }

        result
    }
}

impl fmt::Display for StreamMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

/// Stream selection for processing
#[derive(Debug, Clone)]
pub struct StreamSelection {
    selections: Vec<SelectionRule>,
}

#[derive(Debug, Clone)]
enum SelectionRule {
    All,
    Type(StreamType),
    Index(usize),
    Program(usize),
    Language(String),
    Title(String),
    Metadata { key: String, value: String },
}

impl StreamSelection {
    /// Create a new stream selection
    pub fn new() -> Self {
        Self {
            selections: Vec::new(),
        }
    }

    /// Select all streams
    pub fn all() -> Self {
        Self {
            selections: vec![SelectionRule::All],
        }
    }

    /// Select by type
    pub fn by_type(stream_type: StreamType) -> Self {
        Self {
            selections: vec![SelectionRule::Type(stream_type)],
        }
    }

    /// Select by index
    pub fn by_index(index: usize) -> Self {
        Self {
            selections: vec![SelectionRule::Index(index)],
        }
    }

    /// Select by program
    pub fn by_program(program_id: usize) -> Self {
        Self {
            selections: vec![SelectionRule::Program(program_id)],
        }
    }

    /// Select by language
    pub fn by_language(lang: impl Into<String>) -> Self {
        Self {
            selections: vec![SelectionRule::Language(lang.into())],
        }
    }

    /// Select by title
    pub fn by_title(title: impl Into<String>) -> Self {
        Self {
            selections: vec![SelectionRule::Title(title.into())],
        }
    }

    /// Select by metadata
    pub fn by_metadata(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            selections: vec![SelectionRule::Metadata {
                key: key.into(),
                value: value.into(),
            }],
        }
    }

    /// Add another selection rule (OR operation)
    pub fn or(mut self, rule: SelectionRule) -> Self {
        self.selections.push(rule);
        self
    }

    /// Convert to stream maps
    pub fn to_maps(&self, input_index: usize) -> Vec<StreamMap> {
        self.selections
            .iter()
            .map(|rule| match rule {
                SelectionRule::All => StreamMap::from_input(input_index),
                SelectionRule::Type(t) => {
                    StreamMap::specific(input_index, StreamSpecifier::Type(*t))
                }
                SelectionRule::Index(i) => {
                    StreamMap::specific(input_index, StreamSpecifier::Index(*i))
                }
                SelectionRule::Program(p) => {
                    StreamMap::specific(input_index, StreamSpecifier::Program(*p))
                }
                SelectionRule::Language(lang) => StreamMap::specific(
                    input_index,
                    StreamSpecifier::Metadata {
                        key: "language".to_string(),
                        value: Some(lang.clone()),
                    },
                ),
                SelectionRule::Title(title) => StreamMap::specific(
                    input_index,
                    StreamSpecifier::Metadata {
                        key: "title".to_string(),
                        value: Some(title.clone()),
                    },
                ),
                SelectionRule::Metadata { key, value } => StreamMap::specific(
                    input_index,
                    StreamSpecifier::Metadata {
                        key: key.clone(),
                        value: Some(value.clone()),
                    },
                ),
            })
            .collect()
    }
}

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

/// Stream disposition flags
#[derive(Debug, Clone, Default)]
pub struct StreamDisposition {
    pub default: bool,
    pub dub: bool,
    pub original: bool,
    pub comment: bool,
    pub lyrics: bool,
    pub karaoke: bool,
    pub forced: bool,
    pub hearing_impaired: bool,
    pub visual_impaired: bool,
    pub clean_effects: bool,
    pub attached_pic: bool,
    pub timed_thumbnails: bool,
    pub captions: bool,
    pub descriptions: bool,
    pub metadata: bool,
}

impl StreamDisposition {
    /// Create default disposition
    pub fn new() -> Self {
        Self::default()
    }

    /// Set as default stream
    pub fn set_default(mut self) -> Self {
        self.default = true;
        self
    }

    /// Set as forced stream
    pub fn set_forced(mut self) -> Self {
        self.forced = true;
        self
    }

    /// Build disposition string
    pub fn to_string(&self) -> String {
        let mut parts = Vec::new();

        if self.default {
            parts.push("default");
        }
        if self.dub {
            parts.push("dub");
        }
        if self.original {
            parts.push("original");
        }
        if self.comment {
            parts.push("comment");
        }
        if self.lyrics {
            parts.push("lyrics");
        }
        if self.karaoke {
            parts.push("karaoke");
        }
        if self.forced {
            parts.push("forced");
        }
        if self.hearing_impaired {
            parts.push("hearing_impaired");
        }
        if self.visual_impaired {
            parts.push("visual_impaired");
        }
        if self.clean_effects {
            parts.push("clean_effects");
        }
        if self.attached_pic {
            parts.push("attached_pic");
        }
        if self.timed_thumbnails {
            parts.push("timed_thumbnails");
        }
        if self.captions {
            parts.push("captions");
        }
        if self.descriptions {
            parts.push("descriptions");
        }
        if self.metadata {
            parts.push("metadata");
        }

        parts.join("+")
    }
}

/// Common stream mapping patterns
pub mod patterns {
    use super::*;

    /// Map best quality video and audio
    pub fn best_quality() -> Vec<StreamMap> {
        vec![
            StreamMap::video_from(0),
            StreamMap::audio_from(0),
        ]
    }

    /// Map all video, select specific audio language
    pub fn video_with_language(language: &str) -> Vec<StreamMap> {
        vec![
            StreamMap::video_from(0),
            StreamMap::specific(
                0,
                StreamSpecifier::Metadata {
                    key: "language".to_string(),
                    value: Some(language.to_string()),
                },
            ),
        ]
    }

    /// Map multiple audio tracks
    pub fn multi_audio() -> Vec<StreamMap> {
        vec![
            StreamMap::video_from(0),
            StreamMap::audio_from(0),
        ]
    }

    /// Map for subtitles extraction
    pub fn subtitles_only() -> Vec<StreamMap> {
        vec![StreamMap::subtitle_from(0)]
    }

    /// Map everything except subtitles
    pub fn no_subtitles() -> Vec<StreamMap> {
        vec![
            StreamMap::video_from(0),
            StreamMap::audio_from(0),
        ]
    }

    /// Map specific streams by index
    pub fn by_indices(indices: &[(usize, usize)]) -> Vec<StreamMap> {
        indices
            .iter()
            .map(|(input, stream)| StreamMap::stream_index(*input, *stream))
            .collect()
    }

    /// Complex multi-input mapping
    pub fn multi_input_merge(input_count: usize) -> Vec<StreamMap> {
        let mut maps = vec![StreamMap::video_from(0)];

        for i in 0..input_count {
            maps.push(StreamMap::audio_from(i));
        }

        maps
    }
}

/// Stream metadata builder
#[derive(Debug, Clone, Default)]
pub struct StreamMetadata {
    metadata: HashMap<String, String>,
}

impl StreamMetadata {
    /// Create new stream metadata
    pub fn new() -> Self {
        Self::default()
    }

    /// Set title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.metadata.insert("title".to_string(), title.into());
        self
    }

    /// Set language
    pub fn language(mut self, lang: impl Into<String>) -> Self {
        self.metadata.insert("language".to_string(), lang.into());
        self
    }

    /// Set handler name
    pub fn handler(mut self, name: impl Into<String>) -> Self {
        self.metadata.insert("handler_name".to_string(), name.into());
        self
    }

    /// Add custom metadata
    pub fn custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Get metadata map
    pub fn into_map(self) -> HashMap<String, String> {
        self.metadata
    }
}

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

    #[test]
    fn test_stream_map() {
        let map = StreamMap::from_input(0);
        assert_eq!(map.to_string(), "0");

        let map = StreamMap::video_from(1);
        assert_eq!(map.to_string(), "1:v");

        let map = StreamMap::stream_index(0, 2).exclude();
        assert_eq!(map.to_string(), "-0:2");
    }

    #[test]
    fn test_stream_selection() {
        let selection = StreamSelection::by_type(StreamType::Audio)
            .or(SelectionRule::Language("eng".to_string()));

        let maps = selection.to_maps(0);
        assert_eq!(maps.len(), 2);
    }

    #[test]
    fn test_stream_disposition() {
        let disp = StreamDisposition::new()
            .set_default()
            .set_forced();

        let s = disp.to_string();
        assert!(s.contains("default"));
        assert!(s.contains("forced"));
    }

    #[test]
    fn test_patterns() {
        let best = patterns::best_quality();
        assert_eq!(best.len(), 2);

        let lang = patterns::video_with_language("eng");
        assert_eq!(lang.len(), 2);
    }
}