refactor: move actual transcoding into own fn
This commit is contained in:
71
src/main.rs
71
src/main.rs
@@ -1,7 +1,7 @@
|
||||
mod config;
|
||||
mod ui;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::config::{Config, Transcode};
|
||||
use anyhow::{Context, Error, Result};
|
||||
use futures::{pin_mut, prelude::*};
|
||||
use glib::{subclass::prelude::*, GBoxed, GString};
|
||||
@@ -68,7 +68,7 @@ fn gmake<T: IsA<Element>>(factory_name: &str) -> Result<T> {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConversionArgs {
|
||||
rel_from_path: PathBuf,
|
||||
transcode: config::Transcode,
|
||||
transcode: Transcode,
|
||||
}
|
||||
|
||||
fn get_conversion_args(config: &Config) -> impl Iterator<Item = Result<ConversionArgs>> + '_ {
|
||||
@@ -276,25 +276,49 @@ async fn transcode(
|
||||
// "whole" files to the intended file path, ignoring partial files in the mtime check
|
||||
let to_path_tmp = to_path.with_extension("tmp");
|
||||
|
||||
if let config::Transcode::Copy = args.transcode {
|
||||
rm_file_on_err(&to_path_tmp, async {
|
||||
match args.transcode {
|
||||
Transcode::Copy => {
|
||||
fs::copy(&from_path, &to_path_tmp).await.with_context(|| {
|
||||
format!(
|
||||
"could not copy file from {} to {}",
|
||||
from_path.display(),
|
||||
to_path_tmp.display()
|
||||
)
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
fs::rename(&to_path_tmp, &to_path).await?;
|
||||
|
||||
return Ok(());
|
||||
})?;
|
||||
}
|
||||
|
||||
_ => {
|
||||
to_path.set_extension(args.transcode.extension());
|
||||
|
||||
transcode_gstreamer(
|
||||
&from_path,
|
||||
&to_path_tmp,
|
||||
args.transcode.clone(),
|
||||
task_id,
|
||||
queue,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
fs::rename(&to_path_tmp, &to_path).await.with_context(|| {
|
||||
format!(
|
||||
"could not rename temporary file {} to {}",
|
||||
to_path_tmp.display(),
|
||||
to_path.display()
|
||||
)
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn transcode_gstreamer(
|
||||
from_path: &Path,
|
||||
to_path: &Path,
|
||||
transcode: Transcode,
|
||||
task_id: usize,
|
||||
queue: &ui::MsgQueue,
|
||||
) -> Result<()> {
|
||||
let file_src: Element = gmake("filesrc")?;
|
||||
file_src.set_property("location", &path_to_gstring(&from_path))?;
|
||||
|
||||
@@ -310,10 +334,7 @@ async fn transcode(
|
||||
// downgrade pipeline RC to a weak RC to break the reference cycle
|
||||
let pipeline_weak = pipeline.downgrade();
|
||||
|
||||
let transcode_args = args.transcode.clone();
|
||||
|
||||
let to_path_tmp_clone = to_path_tmp.clone();
|
||||
|
||||
let to_path_clone = to_path.to_owned();
|
||||
decodebin.connect_pad_added(move |decodebin, src_pad| {
|
||||
let insert_sink = || -> Result<()> {
|
||||
let pipeline = match pipeline_weak.upgrade() {
|
||||
@@ -354,8 +375,8 @@ async fn transcode(
|
||||
gmake("audioconvert")?,
|
||||
];
|
||||
|
||||
match &transcode_args {
|
||||
config::Transcode::Opus {
|
||||
match &transcode {
|
||||
Transcode::Opus {
|
||||
bitrate,
|
||||
bitrate_type,
|
||||
} => {
|
||||
@@ -378,13 +399,13 @@ async fn transcode(
|
||||
dest_elems.push(gmake("oggmux")?);
|
||||
}
|
||||
|
||||
config::Transcode::Flac { compression } => {
|
||||
Transcode::Flac { compression } => {
|
||||
let encoder: Element = gmake("flacenc")?;
|
||||
encoder.set_property_from_str("quality", &compression.to_string());
|
||||
dest_elems.push(encoder);
|
||||
}
|
||||
|
||||
config::Transcode::Mp3 {
|
||||
Transcode::Mp3 {
|
||||
bitrate,
|
||||
bitrate_type,
|
||||
} => {
|
||||
@@ -404,11 +425,14 @@ async fn transcode(
|
||||
dest_elems.push(gmake("id3v2mux")?);
|
||||
}
|
||||
|
||||
config::Transcode::Copy => unreachable!(),
|
||||
Transcode::Copy => {
|
||||
// already handled outside this fn
|
||||
unreachable!();
|
||||
}
|
||||
};
|
||||
|
||||
let file_dest: gstreamer_base::BaseSink = gmake("filesink")?;
|
||||
file_dest.set_property("location", &path_to_gstring(&to_path_tmp_clone))?;
|
||||
file_dest.set_property("location", &path_to_gstring(&to_path_clone))?;
|
||||
file_dest.set_sync(false);
|
||||
dest_elems.push(file_dest.upcast());
|
||||
|
||||
@@ -446,7 +470,6 @@ async fn transcode(
|
||||
|
||||
let bus = pipeline.get_bus().context("pipe get bus")?;
|
||||
|
||||
rm_file_on_err(&to_path_tmp, async {
|
||||
pipeline
|
||||
.set_state(gstreamer::State::Playing)
|
||||
.context("Unable to set the pipeline to the `Playing` state")?;
|
||||
@@ -560,11 +583,7 @@ async fn transcode(
|
||||
.set_state(gstreamer::State::Null)
|
||||
.context("Unable to set the pipeline to the `Null` state")?;
|
||||
|
||||
fs::rename(&to_path_tmp, &to_path).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn rm_file_on_err<F, T>(path: &Path, f: F) -> Result<T>
|
||||
|
||||
Reference in New Issue
Block a user