Skip to main content

viser_ffmpeg/
encode.rs

1use std::path::{Path, PathBuf};
2use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
3use tokio::io::AsyncBufReadExt;
4use tokio::process::Command;
5
6use crate::{Codec, RateControlMode, Resolution, ffmpeg_path, probe};
7
8/// Parameters for a single encode.
9#[derive(Debug, Clone)]
10pub struct EncodeJob {
11    /// Source media file path.
12    pub input: String,
13    /// Destination file path for the encoded output.
14    pub output: String,
15    /// Optional target resolution; when set, scales with the lanczos filter.
16    pub resolution: Option<Resolution>,
17    /// Video codec to encode with.
18    pub codec: Codec,
19    /// Constant rate factor / quantizer value (interpretation depends on `rate_control`).
20    pub crf: i32,
21    /// Rate-control mode that determines how `crf`/bitrate fields are applied.
22    pub rate_control: RateControlMode,
23    /// Target bitrate in kbps; used for VBR mode.
24    pub target_bitrate: f64, // kbps, used for VBR mode
25    /// Maximum bitrate cap in kbps; used for capped CRF mode.
26    pub max_bitrate: f64, // kbps, used for capped CRF mode
27    /// VBV buffer size in kbps; used for capped CRF mode.
28    pub bufsize: f64, // kbps, used for capped CRF mode
29    /// Encoder speed preset (e.g. `"medium"`); empty leaves the encoder default.
30    pub preset: String,
31    /// Extra raw FFmpeg arguments appended verbatim before the output path.
32    pub extra_args: Vec<String>,
33}
34
35/// Output of a completed encode.
36#[derive(Debug, Clone)]
37pub struct EncodeResult {
38    /// The job that produced this result.
39    pub job: EncodeJob,
40    /// Average bitrate of the output in kbps, measured by probing it.
41    pub bitrate: f64, // kbps (average)
42    /// Output file size in bytes.
43    pub file_size: u64, // bytes
44    /// Wall-clock time taken to encode.
45    pub duration: Duration, // wall-clock encode time
46}
47
48/// Real-time encoding progress info parsed from FFmpeg.
49#[derive(Debug, Clone, Default)]
50pub struct Progress {
51    /// Number of frames encoded so far.
52    pub frame: i64,
53    /// Current encoding rate in frames per second.
54    pub fps: f64,
55    /// Current output bitrate in kbps.
56    pub bitrate: f64, // kbps
57    /// Encoding speed relative to real time (e.g. 2.5 means 2.5x).
58    pub speed: f64, // e.g. 2.5x
59    /// Output timestamp reached so far.
60    pub time: Duration,
61}
62
63/// Runs an FFmpeg encode job. Progress updates are sent on the channel if provided.
64pub async fn encode(
65    job: EncodeJob,
66    progress_tx: Option<tokio::sync::mpsc::Sender<Progress>>,
67) -> anyhow::Result<EncodeResult> {
68    match job.rate_control {
69        RateControlMode::Vbr => encode_two_pass(job, progress_tx).await,
70        _ => encode_single_pass(job, progress_tx).await,
71    }
72}
73
74async fn encode_single_pass(
75    job: EncodeJob,
76    progress_tx: Option<tokio::sync::mpsc::Sender<Progress>>,
77) -> anyhow::Result<EncodeResult> {
78    let args = build_encode_args(&job, EncodePass::Single)?;
79    run_encode(job, args, progress_tx).await
80}
81
82async fn encode_two_pass(
83    job: EncodeJob,
84    progress_tx: Option<tokio::sync::mpsc::Sender<Progress>>,
85) -> anyhow::Result<EncodeResult> {
86    if job.target_bitrate <= 0.0 {
87        anyhow::bail!("target bitrate must be greater than zero for VBR mode");
88    }
89
90    let passlog_prefix = make_passlog_prefix(&job.output);
91    let cleanup = PasslogCleanup::new(passlog_prefix.clone());
92
93    let first_pass_args = build_encode_args(&job, EncodePass::First(&passlog_prefix))?;
94    run_ffmpeg(first_pass_args, None).await?;
95
96    let second_pass_args = build_encode_args(&job, EncodePass::Second(&passlog_prefix))?;
97    let result = run_encode(job, second_pass_args, progress_tx).await;
98
99    cleanup.run();
100    result
101}
102
103async fn run_encode(
104    job: EncodeJob,
105    args: Vec<String>,
106    progress_tx: Option<tokio::sync::mpsc::Sender<Progress>>,
107) -> anyhow::Result<EncodeResult> {
108    let start = Instant::now();
109    run_ffmpeg(args, progress_tx).await?;
110
111    let elapsed = start.elapsed();
112
113    // Probe the output to get actual bitrate and file size
114    let meta = std::fs::metadata(&job.output)
115        .map_err(|e| anyhow::anyhow!("failed to stat output: {e}"))?;
116
117    let probe_result = probe(&job.output).await?;
118    let bitrate = probe_result.format.bit_rate as f64 / 1000.0;
119
120    Ok(EncodeResult { job, bitrate, file_size: meta.len(), duration: elapsed })
121}
122
123async fn run_ffmpeg(
124    args: Vec<String>,
125    progress_tx: Option<tokio::sync::mpsc::Sender<Progress>>,
126) -> anyhow::Result<()> {
127    let mut cmd = Command::new(ffmpeg_path());
128    cmd.args(&args).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::piped());
129
130    let mut child = cmd.spawn().map_err(|e| anyhow::anyhow!("failed to start ffmpeg: {e}"))?;
131
132    // Parse progress from stdout
133    if let Some(stdout) = child.stdout.take() {
134        let tx = progress_tx.clone();
135        tokio::spawn(async move {
136            let reader = tokio::io::BufReader::new(stdout);
137            let mut lines = reader.lines();
138            let mut p = Progress::default();
139            while let Ok(Some(line)) = lines.next_line().await {
140                if parse_progress_line(&line, &mut p) {
141                    if let Some(ref tx) = tx {
142                        let _ = tx.try_send(p.clone());
143                    }
144                }
145            }
146        });
147    }
148
149    let output = child.wait_with_output().await?;
150    if !output.status.success() {
151        let stderr = String::from_utf8_lossy(&output.stderr);
152        anyhow::bail!("ffmpeg encode failed: {stderr}");
153    }
154
155    Ok(())
156}
157
158/// Copies a segment of a video file without re-encoding.
159pub async fn extract(input: &str, output: &str, start: f64, duration: f64) -> anyhow::Result<()> {
160    let args = vec![
161        "-y".to_string(),
162        "-ss".into(),
163        format!("{start:.6}"),
164        "-i".into(),
165        input.into(),
166        "-t".into(),
167        format!("{duration:.6}"),
168        "-c".into(),
169        "copy".into(),
170        "-avoid_negative_ts".into(),
171        "make_zero".into(),
172        output.into(),
173    ];
174
175    let output = Command::new(ffmpeg_path())
176        .args(&args)
177        .stderr(std::process::Stdio::piped())
178        .output()
179        .await?;
180
181    if !output.status.success() {
182        let stderr = String::from_utf8_lossy(&output.stderr);
183        anyhow::bail!("ffmpeg extract failed: {stderr}");
184    }
185    Ok(())
186}
187
188/// Concatenates multiple encoded chunks into a single output without re-encoding.
189pub async fn concat(inputs: &[String], output: &str) -> anyhow::Result<()> {
190    if inputs.is_empty() {
191        anyhow::bail!("cannot concat an empty input list");
192    }
193
194    let list_path = make_concat_list_path(output);
195    let list_body = inputs
196        .iter()
197        .map(|path| format!("file '{}'", path.replace('\'', "'\\''")))
198        .collect::<Vec<_>>()
199        .join("\n");
200    std::fs::write(&list_path, format!("{list_body}\n"))?;
201
202    let args = vec![
203        "-y".to_string(),
204        "-f".into(),
205        "concat".into(),
206        "-safe".into(),
207        "0".into(),
208        "-i".into(),
209        list_path.to_string_lossy().into_owned(),
210        "-c".into(),
211        "copy".into(),
212        output.into(),
213    ];
214
215    let result = run_ffmpeg(args, None).await;
216    let _ = std::fs::remove_file(&list_path);
217    result
218}
219
220enum EncodePass<'a> {
221    Single,
222    First(&'a Path),
223    Second(&'a Path),
224}
225
226fn build_encode_args(job: &EncodeJob, pass: EncodePass<'_>) -> anyhow::Result<Vec<String>> {
227    let mut args = vec!["-y".into(), "-i".into(), job.input.clone(), "-an".into()];
228
229    if !matches!(pass, EncodePass::First(_)) {
230        args.extend(["-progress".into(), "pipe:1".into(), "-nostats".into()]);
231    }
232
233    args.extend(["-c:v".into(), job.codec.as_str().into()]);
234
235    // Rate control mode
236    match job.rate_control {
237        RateControlMode::Qp => match job.codec {
238            Codec::SvtAv1 => {
239                args.extend(["-qp".into(), job.crf.to_string()]);
240                args.extend(["-svtav1-params".into(), "enable-adaptive-quantization=0".into()]);
241            }
242            _ => {
243                args.extend(["-qp".into(), job.crf.to_string()]);
244            }
245        },
246        RateControlMode::CappedCrf => {
247            if job.max_bitrate <= 0.0 {
248                anyhow::bail!("max bitrate must be greater than zero for capped CRF mode");
249            }
250            let bufsize = if job.bufsize > 0.0 { job.bufsize } else { job.max_bitrate * 2.0 };
251            args.extend(["-crf".into(), job.crf.to_string()]);
252            args.extend(["-maxrate".into(), format!("{:.0}k", job.max_bitrate)]);
253            args.extend(["-bufsize".into(), format!("{bufsize:.0}k")]);
254        }
255        RateControlMode::Vbr => {
256            if job.target_bitrate <= 0.0 {
257                anyhow::bail!("target bitrate must be greater than zero for VBR mode");
258            }
259            args.extend(["-b:v".into(), format!("{:.0}k", job.target_bitrate)]);
260            args.extend(["-maxrate".into(), format!("{:.0}k", job.target_bitrate * 2.0)]);
261            args.extend(["-bufsize".into(), format!("{:.0}k", job.target_bitrate * 4.0)]);
262
263            let passlog = match pass {
264                EncodePass::First(path) => {
265                    args.extend(["-pass".into(), "1".into()]);
266                    path
267                }
268                EncodePass::Second(path) => {
269                    args.extend(["-pass".into(), "2".into()]);
270                    path
271                }
272                EncodePass::Single => {
273                    anyhow::bail!("VBR mode requires a two-pass encode flow");
274                }
275            };
276
277            args.extend(["-passlogfile".into(), passlog.to_string_lossy().into_owned()]);
278        }
279        RateControlMode::Crf => {
280            args.extend(["-crf".into(), job.crf.to_string()]);
281        }
282    }
283
284    if !job.preset.is_empty() {
285        args.extend(["-preset".into(), job.preset.clone()]);
286    }
287
288    if let Some(ref res) = job.resolution {
289        if res.width > 0 && res.height > 0 {
290            args.extend([
291                "-vf".into(),
292                format!("scale={}:{}:flags=lanczos", res.width, res.height),
293            ]);
294        }
295    }
296
297    args.extend(job.extra_args.iter().cloned());
298
299    match pass {
300        EncodePass::First(_) => {
301            args.extend(["-f".into(), "null".into()]);
302            args.push(null_output_path().into());
303        }
304        EncodePass::Single | EncodePass::Second(_) => args.push(job.output.clone()),
305    }
306
307    Ok(args)
308}
309
310fn make_passlog_prefix(output: &str) -> PathBuf {
311    let output_path = Path::new(output);
312    let parent =
313        output_path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
314    let stem = output_path.file_stem().and_then(|s| s.to_str()).unwrap_or("viser");
315    let unique = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0);
316    parent.join(format!(".{stem}.viser-passlog-{unique}-{}", std::process::id()))
317}
318
319fn make_concat_list_path(output: &str) -> PathBuf {
320    let output_path = Path::new(output);
321    let parent =
322        output_path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new("."));
323    let stem = output_path.file_stem().and_then(|s| s.to_str()).unwrap_or("viser");
324    let unique = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0);
325    parent.join(format!(".{stem}.viser-concat-{unique}-{}.txt", std::process::id()))
326}
327
328fn null_output_path() -> &'static str {
329    if cfg!(windows) { "NUL" } else { "/dev/null" }
330}
331
332struct PasslogCleanup {
333    parent: PathBuf,
334    prefix: String,
335}
336
337impl PasslogCleanup {
338    fn new(path: PathBuf) -> Self {
339        let parent = path.parent().unwrap_or(Path::new(".")).to_path_buf();
340        let prefix = path.file_name().and_then(|name| name.to_str()).unwrap_or_default().to_owned();
341        Self { parent, prefix }
342    }
343
344    fn run(&self) {
345        let Ok(entries) = std::fs::read_dir(&self.parent) else {
346            return;
347        };
348
349        for entry in entries.flatten() {
350            let path = entry.path();
351            let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
352                continue;
353            };
354            if !name.starts_with(&self.prefix) {
355                continue;
356            }
357            if let Err(err) = std::fs::remove_file(&path) {
358                tracing::debug!(?path, ?err, "failed to remove ffmpeg two-pass log file");
359            }
360        }
361    }
362}
363
364/// Returns true when a complete progress block is ready.
365fn parse_progress_line(line: &str, p: &mut Progress) -> bool {
366    let Some((key, value)) = line.split_once('=') else {
367        return false;
368    };
369
370    match key {
371        "frame" => {
372            p.frame = value.parse().unwrap_or(0);
373        }
374        "fps" => {
375            p.fps = value.parse().unwrap_or(0.0);
376        }
377        "bitrate" => {
378            let v = value.trim_end_matches("kbits/s");
379            p.bitrate = v.parse().unwrap_or(0.0);
380        }
381        "speed" => {
382            let v = value.trim_end_matches('x');
383            p.speed = v.parse().unwrap_or(0.0);
384        }
385        "out_time_us" => {
386            let us: u64 = value.parse().unwrap_or(0);
387            p.time = Duration::from_micros(us);
388        }
389        "progress" => return true,
390        _ => {}
391    }
392    false
393}
394
395#[cfg(test)]
396mod tests {
397    use super::*;
398    use crate::Codec;
399
400    fn sample_job(mode: RateControlMode) -> EncodeJob {
401        EncodeJob {
402            input: "input.mp4".into(),
403            output: "output.mp4".into(),
404            resolution: Some(crate::Resolution::new(1280, 720)),
405            codec: Codec::X264,
406            crf: 23,
407            rate_control: mode,
408            target_bitrate: 2500.0,
409            max_bitrate: 3000.0,
410            bufsize: 6000.0,
411            preset: "medium".into(),
412            extra_args: vec![],
413        }
414    }
415
416    #[test]
417    fn test_build_encode_args_crf_single_pass() {
418        let args =
419            build_encode_args(&sample_job(RateControlMode::Crf), EncodePass::Single).unwrap();
420        assert!(args.windows(2).any(|w| w == ["-crf", "23"]));
421        assert_eq!(args.last().unwrap(), "output.mp4");
422    }
423
424    #[test]
425    fn test_build_encode_args_vbr_first_pass_uses_null_output() {
426        let job = sample_job(RateControlMode::Vbr);
427        let passlog = Path::new("/tmp/viser-passlog");
428        let args = build_encode_args(&job, EncodePass::First(passlog)).unwrap();
429        assert!(args.windows(2).any(|w| w == ["-pass", "1"]));
430        assert!(args.windows(2).any(|w| w == ["-f", "null"]));
431        assert_eq!(args.last().unwrap(), null_output_path());
432    }
433
434    #[test]
435    fn test_build_encode_args_vbr_second_pass_writes_output() {
436        let job = sample_job(RateControlMode::Vbr);
437        let passlog = Path::new("/tmp/viser-passlog");
438        let args = build_encode_args(&job, EncodePass::Second(passlog)).unwrap();
439        assert!(args.windows(2).any(|w| w == ["-pass", "2"]));
440        assert_eq!(args.last().unwrap(), "output.mp4");
441    }
442
443    #[test]
444    fn test_build_encode_args_capped_crf_sets_vbv() {
445        let args =
446            build_encode_args(&sample_job(RateControlMode::CappedCrf), EncodePass::Single).unwrap();
447        assert!(args.windows(2).any(|w| w == ["-crf", "23"]));
448        assert!(args.windows(2).any(|w| w == ["-maxrate", "3000k"]));
449        assert!(args.windows(2).any(|w| w == ["-bufsize", "6000k"]));
450    }
451}