1
0
Fork 0
musicutil/src/commands/transcode.rs

81 lines
2.4 KiB
Rust
Raw Normal View History

2022-10-22 14:16:02 +01:00
use std::path::PathBuf;
use std::process::exit;
2022-08-16 09:05:18 +01:00
use std::sync::mpsc;
use std::thread;
use crate::args::CLIArgs;
use crate::args::TranscodeCommandArgs;
use crate::types::File;
2022-10-22 14:16:02 +01:00
use crate::utils::transcoder::presets::print_presets;
use crate::utils::transcoder::presets::transcode_preset_or_config;
use crate::utils::transcoder::transcode;
2022-08-16 09:05:18 +01:00
pub fn transcode_command(
_args: &CLIArgs,
transcode_args: &TranscodeCommandArgs,
) -> Result<(), Box<dyn std::error::Error>> {
2022-10-22 14:16:02 +01:00
if transcode_args.transcode_config.is_none() && transcode_args.transcode_preset.is_none() {
panic!("Please provide Transcode Preset/Config");
}
if let Some(preset) = &transcode_args.transcode_preset {
if preset == "help" {
print_presets();
exit(0);
}
}
2022-08-16 09:05:18 +01:00
println!("Transcoding");
2022-10-22 14:16:02 +01:00
let input_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.source));
let output_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.source));
2022-08-16 09:05:18 +01:00
let transcode_config = transcode_preset_or_config(
2022-10-22 14:16:02 +01:00
transcode_args.transcode_preset.as_ref(),
transcode_args.transcode_config.as_ref(),
2022-08-16 09:05:18 +01:00
);
2022-10-22 14:16:02 +01:00
2022-08-16 09:05:18 +01:00
if transcode_config.is_err() {
panic!("Please provide Transcode Preset/Config");
}
let transcode_config = transcode_config.unwrap();
if !transcode_args.ignore_extension {
2022-10-22 14:16:02 +01:00
if let Some(ref file_extension) = transcode_config.file_extension {
if file_extension != &output_file.extension {
2022-08-16 09:05:18 +01:00
panic!(
concat!(
"{} is not the recommended ",
"extension for specified transcode config ",
"please change it to {} ",
"or run with --ignore-extension"
),
2022-10-22 21:01:36 +01:00
output_file.extension, file_extension
2022-08-16 09:05:18 +01:00
);
}
}
}
let (tx, rx): (mpsc::Sender<String>, mpsc::Receiver<String>) = mpsc::channel();
let child = thread::spawn(move || loop {
let progress = rx.recv();
2022-10-22 14:16:02 +01:00
if let Ok(progress_str) = progress {
2022-08-16 09:05:18 +01:00
println!("Transcode Progress: {}", progress_str);
2022-10-22 14:16:02 +01:00
} else {
break;
2022-08-16 09:05:18 +01:00
}
});
transcode(
input_file,
transcode_args.dest.clone(),
transcode_config,
Some(tx),
)?;
child.join().expect("oops! the child thread panicked");
println!("Transcode Finished");
2022-10-22 14:16:02 +01:00
Ok(())
2022-08-16 09:05:18 +01:00
}