Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
Prevent calls to ffmpeg spawning CMD windows if one does not already …
Browse files Browse the repository at this point in the history
…exist
  • Loading branch information
_ committed Feb 1, 2022
1 parent be85588 commit 4056875
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ rayon = "1.5"
thiserror = "1.0"
image = "0.23"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = "1.0"

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
40 changes: 19 additions & 21 deletions src/ffmpeg_ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
ffi::OsStr,
io::prelude::*,
os::windows::process::CommandExt,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
};
Expand Down Expand Up @@ -131,7 +132,7 @@ impl FfmpegFrameReaderBuilder {
args.extend(num_frames_arg);

#[rustfmt::skip]
args.extend([
args.extend(&[
OsStr::new("-pix_fmt"), OsStr::new("rgb24"),
OsStr::new("-c:v"), OsStr::new("rawvideo"),
OsStr::new("-f"), OsStr::new("image2pipe"),
Expand All @@ -157,7 +158,7 @@ impl FfmpegFrameReaderBuilder {
}

pub fn get_video_stats<P: AsRef<Path>>(src_path: P) -> Result<String, FfmpegErrorKind> {
let args = [
let args = &[
OsStr::new("-v"),
OsStr::new("quiet"),
OsStr::new("-show_format"),
Expand All @@ -167,7 +168,7 @@ pub fn get_video_stats<P: AsRef<Path>>(src_path: P) -> Result<String, FfmpegErro
OsStr::new(src_path.as_ref()),
];

let stdout = run_ffmpeg_command(Ffprobe, &args, true)?.stdout;
let stdout = run_ffmpeg_command(Ffprobe, args, true)?.stdout;

String::from_utf8(stdout).map_err(|_| Utf8Conversion)
}
Expand All @@ -177,19 +178,15 @@ pub fn is_video_file<P: AsRef<Path>>(src_path: P) -> Result<bool, FfmpegErrorKin
//"ffprobe -v error -select_streams v -show_entries stream=codec_type,codec_name,duration -of compact=p=0:nk=1 {}"

#[rustfmt::skip]
let args = [
let args = &[
OsStr::new("-v"), OsStr::new("error"),
OsStr::new("-select_streams"), OsStr::new("v"),
OsStr::new("-show_entries"), OsStr::new("stream=codec_type,codec_name,duration"),
OsStr::new("-of"), OsStr::new("compact=p=0:nk=1"),
OsStr::new(src_path.as_ref())
];

run_ffmpeg_command(Ffprobe, &args, true).and_then(|output| {
String::from_utf8(output.stdout)
.map_err(|_| Utf8Conversion)
.map(|s| s.trim().to_string())
})
run_ffmpeg_command(Ffprobe, args, true).and_then(|output| String::from_utf8(output.stdout).map_err(|_| Utf8Conversion).map(|s| s.trim().to_string()))
}

let streams_string = get_ffprobe_output(src_path.as_ref())?;
Expand Down Expand Up @@ -255,18 +252,19 @@ fn spawn_ffmpeg_command(name: FfmpegCommandName, args: &[&OsStr], stderr_null: b

let stderr_cfg = if stderr_null { Stdio::null() } else { Stdio::piped() };

Command::new(name.as_os_str())
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(stderr_cfg)
.spawn()
.map_err(|e| match e.kind() {
//shell failed to execute the command. Separate out FileNotFound from all other errors
//as by far the most likely cause is ffmpeg is not installed.
std::io::ErrorKind::NotFound => FfmpegNotFound,
_ => Io(format!("{:?}", e.kind())),
})
let mut command = Command::new(name.as_os_str());
command.args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(stderr_cfg);

//do not spawn a command window on windows when when in a gui application
#[cfg(target_family = "windows")]
command.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);

command.spawn().map_err(|e| match e.kind() {
//shell failed to execute the command. Separate out FileNotFound from all other errors
//as by far the most likely cause is ffmpeg is not installed.
std::io::ErrorKind::NotFound => FfmpegNotFound,
_ => Io(format!("{:?}", e.kind())),
})
}

struct FfmpegOutput {
Expand Down

0 comments on commit 4056875

Please sign in to comment.