use log::{debug, error};
use std::io::BufReader;
#[cfg(windows)]
use std::os::windows::process::CommandExt;
use std::process::Command;
pub fn decode_with_ffmpeg(file_path: &str) -> Option<rodio::Decoder<BufReader<std::fs::File>>> {
let mut possible_ffmpeg_paths: Vec<&str> = vec!["ffmpeg", "ffmpeg.exe"];
let mut current_dir_ffmpeg_path = std::env::current_exe().unwrap();
current_dir_ffmpeg_path.pop();
current_dir_ffmpeg_path.push("ffmpeg.exe");
possible_ffmpeg_paths.push(current_dir_ffmpeg_path.to_str().unwrap());
let mut actual_ffmpeg_path: Option<&str> = None;
for possible_path in possible_ffmpeg_paths {
let mut command = Command::new(possible_path);
let command = command.arg("-version");
#[cfg(windows)]
let command = command.creation_flags(0x00000008);
if let Ok(process) = command.output()
&& process.status.success()
{
actual_ffmpeg_path = Some(possible_path);
break;
}
}
if let Some(ffmpeg_path) = actual_ffmpeg_path {
let sink_file = tempfile::Builder::new().suffix(".wav").tempfile().unwrap();
let sink_file_path = sink_file.into_temp_path();
let mut command = Command::new(ffmpeg_path);
let command = command.args(["-y", "-i", file_path, sink_file_path.to_str().unwrap()]);
debug!("Spawning ffmpeg: {:?}", command);
#[cfg(windows)]
let command = command.creation_flags(0x00000008);
match command.output() {
Ok(process) => {
if process.status.success() {
return Some(
rodio::Decoder::new(BufReader::new(
std::fs::File::open(sink_file_path.to_str().unwrap()).unwrap(),
))
.unwrap(),
);
} else {
error!("ffmpeg returned an error: {:?}", process.status);
}
}
Err(err) => {
error!("Could not spawn ffmpeg: {:?}", err);
}
}
}
None
}