ff-remux 0.18.1

Stream-copy remuxing (trim, audio replace/extract/add) over 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Audio stream operations (replacement, extraction, addition) via ff-sys safe accessors.

// FFmpeg-boundary lints: intentional narrowing/sign casts at the C ABI and
// acronym-heavy FFmpeg doc terms concentrate in this module.
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::doc_markdown)]

use std::path::Path;

use crate::error::RemuxError;

/// Replace the audio stream of `video_input` with the audio from `audio_input`,
/// writing the combined result to `output`.
///
/// The bitstream is stream-copied (no decode/encode cycle); all FFmpeg access
/// goes through owned ff-sys types, so every context frees itself on drop.
pub(crate) fn run_audio_replacement(
    video_input: &Path,
    audio_input: &Path,
    output: &Path,
) -> Result<(), RemuxError> {
    // All contexts are owned; every early return drops them (closing IO /
    // freeing) with no manual teardown on any path.
    let mut vid_ctx = ff_sys::InputFormatContext::open(video_input)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    vid_ctx
        .find_stream_info()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    let Some(video_stream_idx) = vid_ctx
        .streams()
        .find(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_VIDEO)
        .map(|s| s.index() as usize)
    else {
        return Err(RemuxError::OperationFailed {
            reason: format!(
                "no video stream found in video input path={}",
                video_input.display()
            ),
        });
    };

    let mut aud_ctx = ff_sys::InputFormatContext::open(audio_input)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    aud_ctx
        .find_stream_info()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    let Some(audio_stream_idx) = aud_ctx
        .streams()
        .find(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO)
        .map(|s| s.index() as usize)
    else {
        return Err(RemuxError::OperationFailed {
            reason: format!(
                "no audio stream found in audio input path={}",
                audio_input.display()
            ),
        });
    };

    let mut out_ctx = ff_sys::OutputFormatContext::new(None, output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // copy_stream_params deep-copies the parameters and clears codec_tag so the
    // muxer assigns the correct value for the container. The input time base is
    // stable across write_header, so capture it here.
    let vid_out_idx = out_ctx.new_stream(None).map_err(|_| RemuxError::Ffmpeg {
        code: 0,
        message: "avformat_new_stream failed for video".to_string(),
    })?;
    let vid_in_tb = {
        let vid_in =
            vid_ctx
                .stream(video_stream_idx)
                .ok_or_else(|| RemuxError::OperationFailed {
                    reason: "video input stream missing".to_string(),
                })?;
        out_ctx
            .copy_stream_params(vid_out_idx, vid_in.codecpar())
            .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;
        vid_in.time_base()
    };

    let aud_out_idx = out_ctx.new_stream(None).map_err(|_| RemuxError::Ffmpeg {
        code: 0,
        message: "avformat_new_stream failed for audio".to_string(),
    })?;
    let aud_in_tb = {
        let aud_in =
            aud_ctx
                .stream(audio_stream_idx)
                .ok_or_else(|| RemuxError::OperationFailed {
                    reason: "audio input stream missing".to_string(),
                })?;
        out_ctx
            .copy_stream_params(aud_out_idx, aud_in.codecpar())
            .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;
        aud_in.time_base()
    };

    out_ctx
        .open_io(output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    out_ctx
        .write_header()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // Read output time bases after avformat_write_header — the muxer may adjust
    // them; the input time bases were captured before the header write.
    let vid_out_tb = out_ctx.stream_time_base(vid_out_idx);
    let aud_out_tb = out_ctx.stream_time_base(aud_out_idx);

    log::debug!(
        "audio replacement header written \
         video_stream_idx={video_stream_idx} audio_stream_idx={audio_stream_idx}"
    );

    // The owned packet frees itself exactly once on drop at scope end.
    let Ok(mut pkt) = ff_sys::Packet::new() else {
        let _ = out_ctx.write_trailer();
        return Err(RemuxError::Ffmpeg {
            code: 0,
            message: "av_packet_alloc failed".to_string(),
        });
    };

    // Alternate between video and audio inputs; use av_interleaved_write_frame
    // so the muxer buffers and flushes packets in the correct timestamp order.
    let mut loop_err: Option<RemuxError> = None;
    let mut vid_eof = false;
    let mut aud_eof = false;

    'copy: loop {
        // Read one packet from the video input, forwarding only the target stream.
        if !vid_eof {
            match vid_ctx.read_frame(&mut pkt) {
                Err(e) if e.is_eof() => {
                    vid_eof = true;
                }
                Err(e) => {
                    loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                    break 'copy;
                }
                Ok(()) => {
                    if pkt.stream_index() as usize == video_stream_idx {
                        pkt.rescale_ts(vid_in_tb, vid_out_tb);
                        pkt.set_stream_index(0);
                        let write_res = out_ctx.write_interleaved(&mut pkt);
                        // av_interleaved_write_frame takes the packet's buf reference;
                        // unref to clear any remaining fields.
                        pkt.unref();
                        if let Err(e) = write_res {
                            loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                            break 'copy;
                        }
                    } else {
                        pkt.unref();
                    }
                }
            }
        }

        // Read one packet from the audio input, forwarding only the target stream.
        if !aud_eof {
            match aud_ctx.read_frame(&mut pkt) {
                Err(e) if e.is_eof() => {
                    aud_eof = true;
                }
                Err(e) => {
                    loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                    break 'copy;
                }
                Ok(()) => {
                    if pkt.stream_index() as usize == audio_stream_idx {
                        pkt.rescale_ts(aud_in_tb, aud_out_tb);
                        pkt.set_stream_index(1);
                        let write_res = out_ctx.write_interleaved(&mut pkt);
                        pkt.unref();
                        if let Err(e) = write_res {
                            loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                            break 'copy;
                        }
                    } else {
                        pkt.unref();
                    }
                }
            }
        }

        if vid_eof && aud_eof {
            break 'copy;
        }
    }

    let _ = out_ctx.write_trailer();

    // The owned `vid_ctx` / `aud_ctx` / `out_ctx` close their IO and free
    // themselves when they drop at scope end; no manual teardown is needed.
    log::info!("audio replaced output={}", output.display());

    match loop_err {
        Some(e) => Err(e),
        None => Ok(()),
    }
}

// Audio extraction

/// Demux the audio track at `stream_index` (or the first audio stream when
/// `stream_index` is `None`) from `input` and write it to `output`.
///
/// The audio bitstream is stream-copied (no decode/encode cycle).
pub(crate) fn run_audio_extraction(
    input: &Path,
    output: &Path,
    requested_idx: Option<usize>,
) -> Result<(), RemuxError> {
    // The input and output contexts are owned; every early return drops them.
    let mut in_ctx = ff_sys::InputFormatContext::open(input)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    in_ctx
        .find_stream_info()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    let nb_streams = in_ctx.nb_streams() as usize;
    let audio_stream_idx = if let Some(idx) = requested_idx {
        // Validate that the requested index is actually an audio stream.
        if idx >= nb_streams {
            return Err(RemuxError::OperationFailed {
                reason: format!("stream index {idx} out of range (input has {nb_streams} streams)"),
            });
        }
        let is_audio = in_ctx
            .stream(idx)
            .is_some_and(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO);
        if !is_audio {
            return Err(RemuxError::OperationFailed {
                reason: format!("stream index {idx} is not an audio stream"),
            });
        }
        idx
    } else {
        // Find the first audio stream.
        let Some(idx) = in_ctx
            .streams()
            .find(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO)
            .map(|s| s.index() as usize)
        else {
            return Err(RemuxError::OperationFailed {
                reason: format!("no audio stream found in input path={}", input.display()),
            });
        };
        idx
    };

    let mut out_ctx = ff_sys::OutputFormatContext::new(None, output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // copy_stream_params deep-copies the parameters and clears codec_tag so the
    // muxer assigns the correct value for the container. The input time base is
    // stable across write_header, so capture it here.
    let out_idx = out_ctx.new_stream(None).map_err(|_| RemuxError::Ffmpeg {
        code: 0,
        message: "avformat_new_stream failed".to_string(),
    })?;
    let in_tb = {
        let in_stream =
            in_ctx
                .stream(audio_stream_idx)
                .ok_or_else(|| RemuxError::OperationFailed {
                    reason: "audio input stream missing".to_string(),
                })?;
        out_ctx
            .copy_stream_params(out_idx, in_stream.codecpar())
            .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;
        in_stream.time_base()
    };

    out_ctx
        .open_io(output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // A non-zero return here usually means the codec is incompatible with the
    // chosen output container. Wrap it with a clear message so callers know what
    // went wrong.
    out_ctx
        .write_header()
        .map_err(|e| RemuxError::OperationFailed {
            reason: format!(
                "codec incompatible with output container: {}",
                ff_sys::av_error_string(e.code())
            ),
        })?;

    // Read the output time base after avformat_write_header — the muxer may
    // adjust it; the input time base was captured before the header write.
    let out_tb = out_ctx.stream_time_base(out_idx);

    log::debug!(
        "audio extraction header written audio_stream_idx={audio_stream_idx} \
         output={}",
        output.display()
    );

    // The owned packet frees itself exactly once on drop at scope end.
    let Ok(mut pkt) = ff_sys::Packet::new() else {
        let _ = out_ctx.write_trailer();
        return Err(RemuxError::Ffmpeg {
            code: 0,
            message: "av_packet_alloc failed".to_string(),
        });
    };

    let mut loop_err: Option<RemuxError> = None;

    'read: loop {
        match in_ctx.read_frame(&mut pkt) {
            Err(e) if e.is_eof() => break 'read,
            Err(e) => {
                loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                break 'read;
            }
            Ok(()) => {}
        }

        if pkt.stream_index() as usize != audio_stream_idx {
            // Skip non-audio packets.
            pkt.unref();
            continue 'read;
        }

        // Rescale timestamps to the output stream's time base and remap index.
        pkt.rescale_ts(in_tb, out_tb);
        pkt.set_stream_index(0);

        let write_res = out_ctx.write_interleaved(&mut pkt);
        // av_interleaved_write_frame takes the packet's buf reference; unref to clear.
        pkt.unref();
        if let Err(e) = write_res {
            loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
            break 'read;
        }
    }

    let _ = out_ctx.write_trailer();

    // The owned `in_ctx` / `out_ctx` close their IO and free themselves when they
    // drop at scope end; no manual teardown is needed.
    log::info!(
        "audio extracted output={} stream_index={audio_stream_idx}",
        output.display()
    );

    match loop_err {
        Some(e) => Err(e),
        None => Ok(()),
    }
}

// Audio addition

/// Mux `audio_input` into `video_input`, writing both streams to `output`.
///
/// The video bitstream is stream-copied (no decode/encode cycle).  When
/// `loop_audio` is true and the audio is shorter than the video, the audio
/// track is looped by re-seeking to the start and advancing the PTS offset.
pub(crate) fn run_audio_addition(
    video_input: &Path,
    audio_input: &Path,
    output: &Path,
    loop_audio: bool,
) -> Result<(), RemuxError> {
    // All contexts are owned; every early return drops them.
    let mut vid_ctx = ff_sys::InputFormatContext::open(video_input)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    vid_ctx
        .find_stream_info()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    let Some(video_stream_idx) = vid_ctx
        .streams()
        .find(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_VIDEO)
        .map(|s| s.index() as usize)
    else {
        return Err(RemuxError::OperationFailed {
            reason: format!(
                "no video stream found in video input path={}",
                video_input.display()
            ),
        });
    };

    let mut aud_ctx = ff_sys::InputFormatContext::open(audio_input)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    aud_ctx
        .find_stream_info()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    let Some(audio_stream_idx) = aud_ctx
        .streams()
        .find(|s| s.codecpar().codec_type() == ff_sys::AVMediaType_AVMEDIA_TYPE_AUDIO)
        .map(|s| s.index() as usize)
    else {
        return Err(RemuxError::OperationFailed {
            reason: format!(
                "no audio stream found in audio input path={}",
                audio_input.display()
            ),
        });
    };

    // Loop only when requested AND audio duration < video duration.
    // Durations are in AV_TIME_BASE (microseconds); a value ≤ 0 means unknown.
    let vid_duration_us = vid_ctx.duration();
    let aud_duration_us = aud_ctx.duration();
    let should_loop = loop_audio
        && vid_duration_us > 0
        && aud_duration_us > 0
        && aud_duration_us < vid_duration_us;

    let mut out_ctx = ff_sys::OutputFormatContext::new(None, output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // copy_stream_params deep-copies the parameters and clears codec_tag so the
    // muxer assigns the correct value for the container. The input time base is
    // stable across write_header, so capture it here.
    let vid_out_idx = out_ctx.new_stream(None).map_err(|_| RemuxError::Ffmpeg {
        code: 0,
        message: "avformat_new_stream failed for video".to_string(),
    })?;
    let vid_in_tb = {
        let vid_in =
            vid_ctx
                .stream(video_stream_idx)
                .ok_or_else(|| RemuxError::OperationFailed {
                    reason: "video input stream missing".to_string(),
                })?;
        out_ctx
            .copy_stream_params(vid_out_idx, vid_in.codecpar())
            .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;
        vid_in.time_base()
    };

    let aud_out_idx = out_ctx.new_stream(None).map_err(|_| RemuxError::Ffmpeg {
        code: 0,
        message: "avformat_new_stream failed for audio".to_string(),
    })?;
    let aud_in_tb = {
        let aud_in =
            aud_ctx
                .stream(audio_stream_idx)
                .ok_or_else(|| RemuxError::OperationFailed {
                    reason: "audio input stream missing".to_string(),
                })?;
        out_ctx
            .copy_stream_params(aud_out_idx, aud_in.codecpar())
            .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;
        aud_in.time_base()
    };

    out_ctx
        .open_io(output)
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    out_ctx
        .write_header()
        .map_err(|e| RemuxError::from_ffmpeg_error(e.code()))?;

    // Read output time bases after avformat_write_header — the muxer may adjust
    // them; the input time bases were captured before the header write.
    let vid_out_tb = out_ctx.stream_time_base(vid_out_idx);
    let aud_out_tb = out_ctx.stream_time_base(aud_out_idx);

    // Duration of the audio stream in its INPUT timebase — used to compute the
    // PTS offset when the audio is looped.  Fall back to 0 when unknown.
    let aud_loop_duration_in_tb: i64 = aud_ctx
        .stream(audio_stream_idx)
        .map_or(0, |s| s.duration().max(0));

    log::debug!(
        "audio addition header written should_loop={should_loop} \
         video_stream_idx={video_stream_idx} audio_stream_idx={audio_stream_idx}"
    );

    // The owned packet frees itself exactly once on drop at scope end.
    let Ok(mut pkt) = ff_sys::Packet::new() else {
        let _ = out_ctx.write_trailer();
        return Err(RemuxError::Ffmpeg {
            code: 0,
            message: "av_packet_alloc failed".to_string(),
        });
    };

    // Terminate when video is exhausted.  Audio terminates naturally (non-loop)
    // or is re-seeked with an advancing PTS offset (loop).
    let mut add_loop_err: Option<RemuxError> = None;
    let mut vid_eof = false;
    let mut aud_eof = false;
    // Cumulative PTS offset applied to looped audio packets (in audio IN timebase).
    let mut aud_pts_offset_in_tb: i64 = 0;

    'copy: loop {
        // video packet
        if !vid_eof {
            match vid_ctx.read_frame(&mut pkt) {
                Err(e) if e.is_eof() => {
                    vid_eof = true;
                }
                Err(e) => {
                    add_loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                    break 'copy;
                }
                Ok(()) => {
                    if pkt.stream_index() as usize == video_stream_idx {
                        pkt.rescale_ts(vid_in_tb, vid_out_tb);
                        pkt.set_stream_index(0);
                        let write_res = out_ctx.write_interleaved(&mut pkt);
                        pkt.unref();
                        if let Err(e) = write_res {
                            add_loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                            break 'copy;
                        }
                    } else {
                        pkt.unref();
                    }
                }
            }
        }

        // Stop as soon as video is done — no point reading more audio.
        if vid_eof {
            break 'copy;
        }

        // audio packet
        if !aud_eof {
            match aud_ctx.read_frame(&mut pkt) {
                Err(e) if e.is_eof() => {
                    if should_loop {
                        // Re-seek audio to the start and advance the PTS offset
                        // so that looped packets continue from where the last
                        // packet ended.
                        let _ = aud_ctx.seek_frame(
                            audio_stream_idx as i32,
                            0,
                            ff_sys::avformat::seek_flags::BACKWARD,
                        );
                        aud_pts_offset_in_tb += aud_loop_duration_in_tb;
                        // pkt was not filled on EOF; nothing to unref.
                    } else {
                        aud_eof = true;
                    }
                }
                Err(e) => {
                    add_loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                    break 'copy;
                }
                Ok(()) => {
                    if pkt.stream_index() as usize == audio_stream_idx {
                        // Apply the cumulative loop offset before rescaling so
                        // that PTS values are monotonically increasing across loops.
                        if pkt.pts() != ff_sys::AV_NOPTS_VALUE {
                            pkt.set_pts(pkt.pts() + aud_pts_offset_in_tb);
                        }
                        if pkt.dts() != ff_sys::AV_NOPTS_VALUE {
                            pkt.set_dts(pkt.dts() + aud_pts_offset_in_tb);
                        }
                        pkt.rescale_ts(aud_in_tb, aud_out_tb);
                        pkt.set_stream_index(1);
                        let write_res = out_ctx.write_interleaved(&mut pkt);
                        pkt.unref();
                        if let Err(e) = write_res {
                            add_loop_err = Some(RemuxError::from_ffmpeg_error(e.code()));
                            break 'copy;
                        }
                    } else {
                        pkt.unref();
                    }
                }
            }
        }
    }

    let _ = out_ctx.write_trailer();

    // The owned `vid_ctx` / `aud_ctx` / `out_ctx` close their IO and free
    // themselves when they drop at scope end; no manual teardown is needed.
    log::info!(
        "audio added output={} loop_audio={loop_audio}",
        output.display()
    );

    match add_loop_err {
        Some(e) => Err(e),
        None => Ok(()),
    }
}