bumblebee/src/ffmpeg.rs

88 lines
2.3 KiB
Rust

use std::process::Command;
pub struct FFmpegCommandOptions {
pub input: String,
pub output: String,
pub video_codec: String,
pub video_bitrate: Option<u32>,
pub video_crf: Option<u8>,
pub audio_codec: String,
pub audio_bitrate: Option<u32>,
pub threads: Option<u8>,
pub niceness: Option<u8>
}
impl FFmpegCommandOptions {
pub fn to_args(&self) -> Vec<String> {
let mut args = Vec::new();
args.push("-i".to_string());
args.push(self.input.clone());
args.push("-c:v".to_string());
args.push(self.video_codec.clone());
args.push("-c:a".to_string());
args.push(self.audio_codec.clone());
if let Some(bitrate) = self.video_bitrate {
args.push("-b:v".to_string());
args.push(bitrate.to_string());
}
if let Some(crf) = self.video_crf {
args.push("-crf".to_string());
args.push(crf.to_string());
}
if let Some(bitrate) = self.audio_bitrate {
args.push("-b:a".to_string());
args.push(bitrate.to_string());
}
if let Some(threads) = self.threads {
args.push("-threads".to_string());
args.push(threads.to_string());
}
if let Some(niceness) = self.niceness {
args.push("-threads".to_string());
args.push(niceness.to_string());
}
args.push(self.output.clone());
args
}
}
pub fn build_command(program: &str, options: &FFmpegCommandOptions) -> Command {
let mut command = Command::new(program);
command.arg("-i").arg(&options.input);
command.arg("-c:v").arg(&options.video_codec);
command.arg("-c:a").arg(&options.audio_codec);
if let Some(bitrate) = options.video_bitrate {
command.arg("-b:v").arg(bitrate.to_string());
}
if let Some(crf) = options.video_crf {
command.arg("-crf").arg(crf.to_string());
}
if let Some(bitrate) = options.audio_bitrate {
command.arg("-b:a").arg(bitrate.to_string());
}
if let Some(threads) = options.threads {
command.arg("-threads").arg(threads.to_string());
}
if let Some(niceness) = options.niceness {
command.arg("-threads").arg(niceness.to_string());
}
command.arg(&options.output);
command
}