I've ended up with too many push commands in this code. Is it possible to simplify it somehow?
I want to generate a ffmpeg command to recursively find videos and encode them in H.265/HEVC and downscale them if they are above some max resolution.
This is the .env
file:
CRF=28
# Use the slowest preset you have patience for.
PRESET="slow"
AUDIO_CONVERSION=true
AUDIO_BITRATE=128
MAX_HEIGHT=720
And the main.rs
:
use dotenv;
struct Video {
name: String,
extension: String,
codec: String,
audio_bitrate: i32,
height: i32,
}
fn get_ffmpeg_command(video: Video) -> String {
let mut command = String::from("ffmpeg -i ");
command.push_str(&video.name);
command.push_str(&video.extension);
if video.codec != "x265" {
command.push_str("-c:v libx265 -crf ");
let crf = dotenv::var("CRF").unwrap();
command.push_str(&crf);
command.push_str(" -preset ");
let preset = dotenv::var("PRESET").unwrap();
command.push_str(&preset);
}
let audio_conversion = dotenv::var("AUDIO_CONVERSION").unwrap();
let audio_bitrate = dotenv::var("AUDIO_BITRATE").unwrap();
if audio_conversion == "true" &&
video.audio_bitrate > audio_bitrate.parse().unwrap() {
command.push_str(" -c:a aac -b:a ");
command.push_str(&audio_bitrate);
command.push_str("k ");
}
// Downscale the video if above MAX_HEIGHT
let max_height = dotenv::var("MAX_HEIGHT").unwrap();
if video.height > max_height.parse().unwrap() {
command.push_str("-vf scale=-1:");
command.push_str(&max_height);
command.push_str(" ");
}
command.push_str(&video.name);
command.push_str(".mkv");
return command;
}
fn main() {
dotenv::dotenv().ok();
// let videos = get_videos_recursively(Path::new("/tmp"));
// println!("{:?}", get_ffmepg_command(video));
println!("Hello World!");
}
/// Get every video under the given directory
//fn get_videos_recursively(dir: &Path) -> Vec<Video> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_main() {
let video = Video {
name: "test".to_string(),
extension: ".mp4".to_string(),
codec: "x264".to_string(),
audio_bitrate: 512,
height: 2160,
};
let result = "ffmpeg -i test.mp4-c:v libx265 -crf 28 -preset slow -c:a aac -b:a 128k -vf scale=-1:720 test.mkv".to_string();
assert_eq!(result, get_ffmpeg_command(video));
}
}