ffmpeg_sidecar/
version.rs

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
//! Utilities for checking local FFmpeg version string.

use anyhow::Context;

use crate::command::BackgroundCommand;
use crate::{event::FfmpegEvent, log_parser::FfmpegLogParser, paths::ffmpeg_path};
use std::ffi::OsStr;
use std::process::{Command, Stdio};

/// Alias for `ffmpeg -version`, parsing the version number and returning it.
pub fn ffmpeg_version() -> anyhow::Result<String> {
  ffmpeg_version_with_path(ffmpeg_path())
}

/// Lower level variant of `ffmpeg_version` that exposes a customized path
/// to the ffmpeg binary.
pub fn ffmpeg_version_with_path<S: AsRef<OsStr>>(path: S) -> anyhow::Result<String> {
  let mut cmd = Command::new(&path)
    .create_no_window()
    .arg("-version")
    .stdout(Stdio::piped()) // not stderr when calling `-version`
    .spawn()?;
  let stdout = cmd.stdout.take().context("No standard output channel")?;
  let mut parser = FfmpegLogParser::new(stdout);

  let mut version: Option<String> = None;
  while let Ok(event) = parser.parse_next_event() {
    match event {
      FfmpegEvent::ParsedVersion(v) => version = Some(v.version),
      FfmpegEvent::LogEOF => break,
      _ => {}
    }
  }
  let exit_status = cmd.wait()?;
  if !exit_status.success() {
    anyhow::bail!("ffmpeg -version exited with non-zero status");
  }
  version.context("Failed to parse ffmpeg version")
}