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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use ffmpeg_common::{CommandBuilder, Result};
use std::collections::HashMap;

/// Format options for input/output
#[derive(Debug, Clone, Default)]
pub struct FormatOptions {
    /// Format name
    format: Option<String>,
    /// Format-specific options
    options: HashMap<String, String>,
    /// Muxer flags
    flags: Vec<String>,
}

impl FormatOptions {
    /// Create new format options
    pub fn new() -> Self {
        Self::default()
    }

    /// Set format
    pub fn format(mut self, format: impl Into<String>) -> Self {
        self.format = Some(format.into());
        self
    }

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

    /// Add a muxer flag
    pub fn flag(mut self, flag: impl Into<String>) -> Self {
        self.flags.push(flag.into());
        self
    }

    /// Build command line arguments
    pub fn build_args(&self) -> Vec<String> {
        let mut cmd = CommandBuilder::new();

        if let Some(ref format) = self.format {
            cmd = cmd.option("-f", format);
        }

        for (key, value) in &self.options {
            cmd = cmd.option(format!("-{}", key), value);
        }

        if !self.flags.is_empty() {
            cmd = cmd.option("-flags", self.flags.join("+"));
        }

        cmd.build()
    }
}

/// Container format configurations
pub mod formats {
    use super::*;

    /// MP4 format options
    pub struct Mp4;

    impl Mp4 {
        /// Standard MP4
        pub fn standard() -> FormatOptions {
            FormatOptions::new()
                .format("mp4")
                .option("movflags", "+faststart")
        }

        /// Fragmented MP4 for streaming
        pub fn fragmented() -> FormatOptions {
            FormatOptions::new()
                .format("mp4")
                .option("movflags", "frag_keyframe+empty_moov+default_base_moof")
                .option("frag_duration", "1000000")
        }

        /// DASH-compatible MP4
        pub fn dash() -> FormatOptions {
            FormatOptions::new()
                .format("mp4")
                .option("movflags", "dash+delay_moov")
                .option("use_timeline", "1")
                .option("use_template", "1")
        }

        /// MP4 for progressive download
        pub fn progressive() -> FormatOptions {
            FormatOptions::new()
                .format("mp4")
                .option("movflags", "+faststart+separate_moof+disable_chpl")
                .option("brand", "mp42")
        }
    }

    /// MKV format options
    pub struct Mkv;

    impl Mkv {
        /// Standard MKV
        pub fn standard() -> FormatOptions {
            FormatOptions::new()
                .format("matroska")
        }

        /// Live streaming MKV
        pub fn streaming() -> FormatOptions {
            FormatOptions::new()
                .format("matroska")
                .option("live", "1")
                .option("cluster_time_limit", "2000")
        }
    }

    /// WebM format options
    pub struct WebM;

    impl WebM {
        /// Standard WebM
        pub fn standard() -> FormatOptions {
            FormatOptions::new()
                .format("webm")
        }

        /// DASH WebM
        pub fn dash() -> FormatOptions {
            FormatOptions::new()
                .format("webm")
                .option("dash", "1")
                .option("cluster_time_limit", "5000")
                .option("cluster_size_limit", "5M")
        }

        /// Live WebM
        pub fn live() -> FormatOptions {
            FormatOptions::new()
                .format("webm")
                .option("live", "1")
                .option("chunk_start_index", "1")
        }
    }

    /// HLS format options
    pub struct Hls;

    impl Hls {
        /// Standard HLS
        pub fn standard() -> FormatOptions {
            FormatOptions::new()
                .format("hls")
                .option("hls_time", "10")
                .option("hls_list_size", "0")
                .option("hls_segment_type", "mpegts")
        }

        /// Live HLS
        pub fn live() -> FormatOptions {
            FormatOptions::new()
                .format("hls")
                .option("hls_time", "2")
                .option("hls_list_size", "5")
                .option("hls_flags", "delete_segments+append_list")
                .option("hls_segment_type", "mpegts")
        }

        /// HLS with fMP4 segments
        pub fn fmp4() -> FormatOptions {
            FormatOptions::new()
                .format("hls")
                .option("hls_segment_type", "fmp4")
                .option("hls_fmp4_init_filename", "init.mp4")
                .option("hls_time", "10")
        }

        /// Event HLS
        pub fn event() -> FormatOptions {
            FormatOptions::new()
                .format("hls")
                .option("hls_playlist_type", "event")
                .option("hls_time", "10")
                .option("hls_list_size", "0")
        }
    }

    /// DASH format options
    pub struct Dash;

    impl Dash {
        /// Standard DASH
        pub fn standard() -> FormatOptions {
            FormatOptions::new()
                .format("dash")
                .option("seg_duration", "4")
                .option("use_timeline", "1")
                .option("use_template", "1")
        }

        /// Live DASH
        pub fn live() -> FormatOptions {
            FormatOptions::new()
                .format("dash")
                .option("seg_duration", "2")
                .option("use_timeline", "1")
                .option("use_template", "1")
                .option("streaming", "1")
                .option("window_size", "5")
                .option("extra_window_size", "2")
        }

        /// Low latency DASH
        pub fn low_latency() -> FormatOptions {
            FormatOptions::new()
                .format("dash")
                .option("seg_duration", "1")
                .option("ldash", "1")
                .option("streaming", "1")
                .option("use_timeline", "0")
                .option("frag_type", "duration")
                .option("frag_duration", "1")
        }
    }

    /// RTMP format options
    pub struct Rtmp;

    impl Rtmp {
        /// RTMP output
        pub fn output() -> FormatOptions {
            FormatOptions::new()
                .format("flv")
                .option("flvflags", "no_duration_filesize")
        }

        /// RTMP with low latency
        pub fn low_latency() -> FormatOptions {
            FormatOptions::new()
                .format("flv")
                .option("flvflags", "no_duration_filesize+aac_seq_header_detect")
                .option("rtmp_buffer", "100")
                .option("rtmp_live", "live")
        }
    }

    /// Image sequence format options
    pub struct ImageSequence;

    impl ImageSequence {
        /// JPEG sequence
        pub fn jpeg() -> FormatOptions {
            FormatOptions::new()
                .format("image2")
                .option("update", "1")
        }

        /// PNG sequence
        pub fn png() -> FormatOptions {
            FormatOptions::new()
                .format("image2")
                .option("update", "1")
        }

        /// Animated GIF
        pub fn gif() -> FormatOptions {
            FormatOptions::new()
                .format("gif")
                .option("loop", "0")
        }
    }

    /// Audio format options
    pub struct Audio;

    impl Audio {
        /// MP3
        pub fn mp3() -> FormatOptions {
            FormatOptions::new()
                .format("mp3")
                .option("id3v2_version", "3")
        }

        /// AAC in ADTS
        pub fn aac() -> FormatOptions {
            FormatOptions::new()
                .format("adts")
        }

        /// FLAC
        pub fn flac() -> FormatOptions {
            FormatOptions::new()
                .format("flac")
        }

        /// OGG
        pub fn ogg() -> FormatOptions {
            FormatOptions::new()
                .format("ogg")
                .option("page_duration", "1000000")
        }

        /// WAV
        pub fn wav() -> FormatOptions {
            FormatOptions::new()
                .format("wav")
                .option("rf64", "auto")
        }
    }

    /// Raw format options
    pub struct Raw;

    impl Raw {
        /// Raw video
        pub fn video() -> FormatOptions {
            FormatOptions::new()
                .format("rawvideo")
        }

        /// Raw audio PCM
        pub fn audio_pcm() -> FormatOptions {
            FormatOptions::new()
                .format("s16le")
        }

        /// Raw H.264
        pub fn h264() -> FormatOptions {
            FormatOptions::new()
                .format("h264")
        }

        /// Raw H.265
        pub fn h265() -> FormatOptions {
            FormatOptions::new()
                .format("hevc")
        }
    }

    /// Null format (for testing)
    pub struct Null;

    impl Null {
        /// Null output
        pub fn output() -> FormatOptions {
            FormatOptions::new()
                .format("null")
        }
    }
}

/// Muxer-specific options
pub struct MuxerOptions {
    options: HashMap<String, String>,
}

impl MuxerOptions {
    /// Create new muxer options
    pub fn new() -> Self {
        Self {
            options: HashMap::new(),
        }
    }

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

    /// Build into FormatOptions
    pub fn build(self) -> FormatOptions {
        let mut format_opts = FormatOptions::new();
        for (key, value) in self.options {
            format_opts = format_opts.option(key, value);
        }
        format_opts
    }
}

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

/// Common muxer configurations
pub mod muxer_configs {
    use super::*;

    /// Configure for fast seeking
    pub fn fast_seeking() -> MuxerOptions {
        MuxerOptions::new()
            .option("movflags", "+faststart")
            .option("keyint_min", "1")
            .option("g", "30")
    }

    /// Configure for low latency
    pub fn low_latency() -> MuxerOptions {
        MuxerOptions::new()
            .option("flush_packets", "1")
            .option("flags", "+low_delay")
            .option("fflags", "+nobuffer+flush_packets")
    }

    /// Configure for archival
    pub fn archival() -> MuxerOptions {
        MuxerOptions::new()
            .option("write_id3v2", "1")
            .option("write_apetag", "0")
            .option("metadata_header_padding", "1024")
    }

    /// Configure for web compatibility
    pub fn web_compatible() -> MuxerOptions {
        MuxerOptions::new()
            .option("brand", "mp42")
            .option("movflags", "+faststart+separate_moof")
            .option("min_frag_duration", "1000000")
    }
}

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

    #[test]
    fn test_format_options() {
        let opts = FormatOptions::new()
            .format("mp4")
            .option("movflags", "+faststart")
            .flag("low_delay");

        let args = opts.build_args();
        assert!(args.contains(&"-f".to_string()));
        assert!(args.contains(&"mp4".to_string()));
        assert!(args.contains(&"-movflags".to_string()));
        assert!(args.contains(&"+faststart".to_string()));
    }

    #[test]
    fn test_mp4_formats() {
        let standard = Mp4::standard();
        let args = standard.build_args();
        assert!(args.contains(&"mp4".to_string()));

        let fragmented = Mp4::fragmented();
        let args = fragmented.build_args();
        assert!(args.iter().any(|arg| arg.contains("frag_keyframe")));
    }

    #[test]
    fn test_hls_formats() {
        let standard = Hls::standard();
        let args = standard.build_args();
        assert!(args.contains(&"hls".to_string()));
        assert!(args.contains(&"-hls_time".to_string()));

        let live = Hls::live();
        let args = live.build_args();
        assert!(args.contains(&"2".to_string()));
    }

    #[test]
    fn test_audio_formats() {
        let mp3 = Audio::mp3();
        let args = mp3.build_args();
        assert!(args.contains(&"mp3".to_string()));

        let flac = Audio::flac();
        let args = flac.build_args();
        assert!(args.contains(&"flac".to_string()));
    }
}