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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use ffmpeg_common::{
    Codec, CommandBuilder, Duration, MediaPath, PixelFormat, Result, SampleFormat, Size,
};
use std::collections::HashMap;
use std::time::Duration as StdDuration;

use crate::codec::CodecOptions;
use crate::format::FormatOptions;

/// Output specification for FFmpeg
#[derive(Debug, Clone)]
pub struct Output {
    /// Destination path or URL
    destination: MediaPath,
    /// Format options
    format_options: FormatOptions,
    /// Video codec options
    video_codec: Option<CodecOptions>,
    /// Audio codec options
    audio_codec: Option<CodecOptions>,
    /// Subtitle codec options
    subtitle_codec: Option<CodecOptions>,
    /// Duration limit
    duration: Option<Duration>,
    /// File size limit
    file_size_limit: Option<Size>,
    /// Number of frames to output
    frames: Option<u64>,
    /// Metadata
    metadata: HashMap<String, String>,
    /// Stream metadata
    stream_metadata: HashMap<String, HashMap<String, String>>,
    /// Movflags for MP4
    movflags: Option<String>,
    /// Preset
    preset: Option<String>,
    /// Tune
    tune: Option<String>,
    /// Custom options
    options: HashMap<String, String>,
    /// Disable video
    no_video: bool,
    /// Disable audio
    no_audio: bool,
    /// Disable subtitles
    no_subtitles: bool,
    /// Copy timestamps
    copy_timestamps: bool,
    /// Avoid negative timestamps
    avoid_negative_ts: Option<String>,
    /// Start time
    start_time: Option<Duration>,
}

impl Output {
    /// Create a new output
    pub fn new(destination: impl Into<MediaPath>) -> Self {
        Self {
            destination: destination.into(),
            format_options: FormatOptions::new(),
            video_codec: None,
            audio_codec: None,
            subtitle_codec: None,
            duration: None,
            file_size_limit: None,
            frames: None,
            metadata: HashMap::new(),
            stream_metadata: HashMap::new(),
            movflags: None,
            preset: None,
            tune: None,
            options: HashMap::new(),
            no_video: false,
            no_audio: false,
            no_subtitles: false,
            copy_timestamps: false,
            avoid_negative_ts: None,
            start_time: None,
        }
    }

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

    /// Set video codec
    pub fn video_codec(mut self, codec: Codec) -> Self {
        self.video_codec = Some(CodecOptions::new(codec));
        self
    }

    /// Set video codec with options
    pub fn video_codec_opts(mut self, options: CodecOptions) -> Self {
        self.video_codec = Some(options);
        self
    }

    /// Set audio codec
    pub fn audio_codec(mut self, codec: Codec) -> Self {
        self.audio_codec = Some(CodecOptions::new(codec));
        self
    }

    /// Set audio codec with options
    pub fn audio_codec_opts(mut self, options: CodecOptions) -> Self {
        self.audio_codec = Some(options);
        self
    }

    /// Set subtitle codec
    pub fn subtitle_codec(mut self, codec: Codec) -> Self {
        self.subtitle_codec = Some(CodecOptions::new(codec));
        self
    }

    /// Copy all codecs
    pub fn copy_codecs(self) -> Self {
        self.video_codec(Codec::copy())
            .audio_codec(Codec::copy())
            .subtitle_codec(Codec::copy())
    }

    /// Set duration limit
    pub fn duration(mut self, duration: Duration) -> Self {
        self.duration = Some(duration);
        self
    }

    /// Set file size limit
    pub fn file_size_limit(mut self, size: Size) -> Self {
        self.file_size_limit = Some(size);
        self
    }

    /// Set number of frames to output
    pub fn frames(mut self, count: u64) -> Self {
        self.frames = Some(count);
        self
    }

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

    /// Add stream-specific metadata
    pub fn stream_metadata(
        mut self,
        stream_spec: impl Into<String>,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        let stream_spec = stream_spec.into();
        self.stream_metadata
            .entry(stream_spec)
            .or_default()
            .insert(key.into(), value.into());
        self
    }

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

    /// Enable faststart for MP4 (move moov atom to beginning)
    pub fn faststart(self) -> Self {
        self.movflags("faststart")
    }

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

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

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

    /// Disable video output
    pub fn no_video(mut self) -> Self {
        self.no_video = true;
        self
    }

    /// Disable audio output
    pub fn no_audio(mut self) -> Self {
        self.no_audio = true;
        self
    }

    /// Disable subtitle output
    pub fn no_subtitles(mut self) -> Self {
        self.no_subtitles = true;
        self
    }

    /// Copy timestamps
    pub fn copy_timestamps(mut self, enable: bool) -> Self {
        self.copy_timestamps = enable;
        self
    }

    /// Avoid negative timestamps
    pub fn avoid_negative_ts(mut self, mode: impl Into<String>) -> Self {
        self.avoid_negative_ts = Some(mode.into());
        self
    }

    /// Set start time
    pub fn start_time(mut self, time: Duration) -> Self {
        self.start_time = Some(time);
        self
    }

    /// Configure for streaming
    pub fn for_streaming(self) -> Self {
        self.format("mp4")
            .movflags("frag_keyframe+empty_moov+default_base_moof")
            .option("g", "52")  // GOP size
            .option("keyint_min", "25")
    }

    /// Configure for HLS output
    pub fn for_hls(self, segment_duration: u32) -> Self {
        self.format("hls")
            .option("hls_time", segment_duration.to_string())
            .option("hls_playlist_type", "vod")
            .option("hls_segment_filename", "segment_%03d.ts")
    }

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

        // Format options
        cmd = cmd.args(self.format_options.build_args());

        // Video codec
        if let Some(ref codec) = self.video_codec {
            cmd = cmd.args(codec.build_args("v"));
        }

        // Audio codec
        if let Some(ref codec) = self.audio_codec {
            cmd = cmd.args(codec.build_args("a"));
        }

        // Subtitle codec
        if let Some(ref codec) = self.subtitle_codec {
            cmd = cmd.args(codec.build_args("s"));
        }

        // Duration and limits
        if let Some(duration) = self.duration {
            cmd = cmd.option("-t", duration.to_ffmpeg_format());
        }

        if let Some(ref size) = self.file_size_limit {
            cmd = cmd.option("-fs", size.as_bytes());
        }

        if let Some(frames) = self.frames {
            cmd = cmd.option("-frames:v", frames);
        }

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

        // Stream metadata
        for (stream_spec, metadata) in &self.stream_metadata {
            for (key, value) in metadata {
                cmd = cmd.option(
                    format!("-metadata:s:{}", stream_spec),
                    format!("{}={}", key, value),
                );
            }
        }

        // MP4 specific
        if let Some(ref flags) = self.movflags {
            cmd = cmd.option("-movflags", flags);
        }

        // Preset and tune
        if let Some(ref preset) = self.preset {
            cmd = cmd.option("-preset", preset);
        }

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

        // Disable streams
        if self.no_video {
            cmd = cmd.flag("-vn");
        }

        if self.no_audio {
            cmd = cmd.flag("-an");
        }

        if self.no_subtitles {
            cmd = cmd.flag("-sn");
        }

        // Timestamp options
        if self.copy_timestamps {
            cmd = cmd.flag("-copyts");
        }

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

        if let Some(start) = self.start_time {
            cmd = cmd.option("-ss", start.to_ffmpeg_format());
        }

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

        // Output file
        cmd = cmd.arg(self.destination.as_str());

        cmd.build()
    }
}

/// Builder for multi-output scenarios
#[derive(Debug, Clone)]
pub struct MultiOutput {
    outputs: Vec<Output>,
}

impl MultiOutput {
    /// Create a new multi-output builder
    pub fn new() -> Self {
        Self {
            outputs: Vec::new(),
        }
    }

    /// Add an output
    pub fn add_output(mut self, output: Output) -> Self {
        self.outputs.push(output);
        self
    }

    /// Create adaptive streaming outputs (multiple qualities)
    pub fn adaptive_streaming(base_path: impl AsRef<str>) -> Self {
        let base = base_path.as_ref();

        Self::new()
            // 1080p
            .add_output(
                Output::new(format!("{}_1080p.mp4", base))
                    .video_codec_opts(
                        CodecOptions::new(Codec::h264())
                            .bitrate("5000k")
                            .option("maxrate", "5350k")
                            .option("bufsize", "7500k")
                            .size(1920, 1080)
                    )
                    .audio_codec_opts(
                        CodecOptions::new(Codec::aac())
                            .bitrate("192k")
                    )
                    .preset("slow")
            )
            // 720p
            .add_output(
                Output::new(format!("{}_720p.mp4", base))
                    .video_codec_opts(
                        CodecOptions::new(Codec::h264())
                            .bitrate("2800k")
                            .option("maxrate", "3000k")
                            .option("bufsize", "4200k")
                            .size(1280, 720)
                    )
                    .audio_codec_opts(
                        CodecOptions::new(Codec::aac())
                            .bitrate("128k")
                    )
                    .preset("slow")
            )
            // 480p
            .add_output(
                Output::new(format!("{}_480p.mp4", base))
                    .video_codec_opts(
                        CodecOptions::new(Codec::h264())
                            .bitrate("1400k")
                            .option("maxrate", "1500k")
                            .option("bufsize", "2100k")
                            .size(854, 480)
                    )
                    .audio_codec_opts(
                        CodecOptions::new(Codec::aac())
                            .bitrate("128k")
                    )
                    .preset("slow")
            )
    }

    /// Get the outputs
    pub fn into_outputs(self) -> Vec<Output> {
        self.outputs
    }
}

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

/// Builder for image sequence output
#[derive(Debug, Clone)]
pub struct ImageSequenceOutput {
    /// Base path with pattern
    pattern: String,
    /// Image format
    format: String,
    /// Frame rate
    framerate: Option<f64>,
    /// Quality (for JPEG)
    quality: Option<u8>,
    /// Start number
    start_number: Option<u32>,
}

impl ImageSequenceOutput {
    /// Create a new image sequence output
    pub fn new(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            format: "image2".to_string(),
            framerate: None,
            quality: None,
            start_number: None,
        }
    }

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

    /// Set frame rate
    pub fn framerate(mut self, fps: f64) -> Self {
        self.framerate = Some(fps);
        self
    }

    /// Set JPEG quality (2-31, lower is better)
    pub fn quality(mut self, q: u8) -> Self {
        self.quality = Some(q.clamp(2, 31));
        self
    }

    /// Set start number
    pub fn start_number(mut self, num: u32) -> Self {
        self.start_number = Some(num);
        self
    }

    /// Convert to Output
    pub fn into_output(self) -> Output {
        let mut output = Output::new(self.pattern).format(&self.format);

        if let Some(fps) = self.framerate {
            output = output.option("r", fps.to_string());
        }

        if let Some(q) = self.quality {
            output = output.video_codec_opts(
                CodecOptions::new(Codec::new("mjpeg"))
                    .quality(q)
            );
        }

        if let Some(num) = self.start_number {
            output = output.option("start_number", num.to_string());
        }

        output
    }
}

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

    #[test]
    fn test_output_builder() {
        let output = Output::new("output.mp4")
            .format("mp4")
            .video_codec(Codec::h264())
            .audio_codec(Codec::aac())
            .metadata("title", "My Video")
            .faststart();

        let args = output.build_args();
        assert!(args.contains(&"-f".to_string()));
        assert!(args.contains(&"mp4".to_string()));
        assert!(args.contains(&"-c:v".to_string()));
        assert!(args.contains(&"h264".to_string()));
        assert!(args.contains(&"-c:a".to_string()));
        assert!(args.contains(&"aac".to_string()));
        assert!(args.contains(&"-metadata".to_string()));
        assert!(args.contains(&"title=My Video".to_string()));
        assert!(args.contains(&"-movflags".to_string()));
        assert!(args.contains(&"faststart".to_string()));
    }

    #[test]
    fn test_streaming_output() {
        let output = Output::new("output.mp4").for_streaming();
        let args = output.build_args();

        assert!(args.contains(&"-movflags".to_string()));
        assert!(args.iter().any(|arg| arg.contains("frag_keyframe")));
    }

    #[test]
    fn test_image_sequence() {
        let output = ImageSequenceOutput::new("frame_%04d.jpg")
            .quality(5)
            .framerate(1.0)
            .into_output();

        let args = output.build_args();
        assert!(args.contains(&"-f".to_string()));
        assert!(args.contains(&"image2".to_string()));
        assert!(args.contains(&"-r".to_string()));
        assert!(args.contains(&"1".to_string()));
    }
}