Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
a10345fbbe
|
|||
|
f3423eea53
|
|||
|
80e3d02cb4
|
|||
|
b7a7abbf61
|
|||
|
5abadc3131
|
|||
|
9c86cdcc62
|
|||
|
605fa5c15b
|
|||
|
9c1d39ba5f
|
|||
|
c6c9da2f27
|
|||
|
6872e7897b
|
|||
|
2d1497cb36
|
|||
|
f1fb3506b5
|
|||
|
f4050fe645
|
|||
|
b533f059d7
|
|||
|
00a25e168d
|
|||
|
b51c9939c1
|
|||
|
c22d45818e
|
|||
|
18cc852e6b
|
|||
| 65b4f398d9 | |||
| 7f40cb0581 | |||
| bc15a4449d | |||
| 1cf7cec8bd | |||
| 5cf98b3c17 | |||
| 54e174eb0a | |||
| 803860cce5 | |||
| f2bfddd76e | |||
| d073ef10b5 | |||
| 399c4b8a2c | |||
| 3188d074b7 | |||
| 5242aac566 | |||
| 49003c9983 | |||
| 530446bcd6 | |||
| aa65b30873 | |||
| f1a71189c8 | |||
| cc2ac9cb37 | |||
| 511f5d1237 | |||
| ff06358268 | |||
| 183e34c217 | |||
| 755f5dbd1d | |||
| 762064efd6 | |||
| 9959a26e48 |
@@ -1,7 +1,7 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
1
.rustfmt.toml
Normal file
1
.rustfmt.toml
Normal file
@@ -0,0 +1 @@
|
||||
hard_tabs = true
|
||||
24
CHANGELOG.md
Normal file
24
CHANGELOG.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Changelog
|
||||
|
||||
## v1.3.0
|
||||
|
||||
* allow multiple values for the tags "musicbrainz-artistid" and "musicbrainz-albumartistid"
|
||||
* fix "from", "to" & "config" cli argument processing
|
||||
|
||||
## v1.2.2
|
||||
|
||||
* dependencies upgraded
|
||||
|
||||
## v1.2.1
|
||||
|
||||
* dependencies upgraded
|
||||
|
||||
## v1.2.0
|
||||
|
||||
* "copy" encoding format added
|
||||
* "jobs" cli argument added, that lets you set the number of concurrent transcodes
|
||||
|
||||
## v1.1.0
|
||||
|
||||
* "flac" encoding format added
|
||||
* resampling quality set to highest/"10"
|
||||
587
Cargo.lock
generated
587
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
20
Cargo.toml
20
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "audio-conv"
|
||||
version = "1.0.0"
|
||||
version = "1.3.0"
|
||||
edition = "2018"
|
||||
description = "Copies directory structure and converts audio files in it"
|
||||
authors = ["Thomas Heck <t@b128.net>"]
|
||||
@@ -13,24 +13,30 @@ categories = [
|
||||
"multimedia::encoding",
|
||||
]
|
||||
keywords = ["audio", "conversion", "opus", "flac"]
|
||||
include = [
|
||||
"/src/**/*",
|
||||
"/example.audio-conv.yaml",
|
||||
"/README.md",
|
||||
"/CHANGELOG.md",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
gstreamer-audio = { version = "0.16", features = ["v1_10"] }
|
||||
gstreamer = { version = "0.16", features = ["v1_10"] }
|
||||
gstreamer-base = { version = "0.16", features = ["v1_10"] }
|
||||
glib = "0.10"
|
||||
gstreamer-audio = { version = "0.18", features = ["v1_10"] }
|
||||
gstreamer = { version = "0.18", features = ["v1_10"] }
|
||||
gstreamer-base = { version = "0.18", features = ["v1_10"] }
|
||||
glib = "0.15"
|
||||
futures = "0.3"
|
||||
num_cpus = "1"
|
||||
walkdir = "2"
|
||||
libc = "0.2"
|
||||
anyhow = "1"
|
||||
clap = "2"
|
||||
clap = { version = "3", features = ["cargo"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.8"
|
||||
regex = "1"
|
||||
globset = "0.4"
|
||||
derive_more = "0.99"
|
||||
tui = { version = "0.13", default-features = false, features = ["crossterm"] }
|
||||
tui = { version = "0.17", default-features = false, features = ["crossterm"] }
|
||||
|
||||
[dependencies.tokio]
|
||||
version = "1"
|
||||
|
||||
34
README.md
Normal file
34
README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# audio-conv
|
||||
|
||||
Takes two paths, all audio files encountered in the first path are transcoded and stored in the
|
||||
second path. The directory structure from the first path gets also copied to the second path.
|
||||
|
||||
## Dependencies
|
||||
|
||||
Requires *gstreamer* version 1.10 or higher with the *base* plugin.
|
||||
|
||||
The supported source audio formats (or even other media that is able to contain audio) depend on
|
||||
the installed *gstreamer* plugins.
|
||||
|
||||
## Installation via nix flakes
|
||||
|
||||
*audio-conv* can be easily installed via *nix flakes*:
|
||||
|
||||
```bash
|
||||
$ nix profile install gitlab:chpio/audio-conv/release
|
||||
```
|
||||
|
||||
## Generate example config
|
||||
|
||||
*audio-conv* is able to write an example config to your current directory:
|
||||
|
||||
```bash
|
||||
$ audio-conv init
|
||||
```
|
||||
|
||||
Now you need to edit the generated *audio-conv.yaml* file. And let it convert your audio files
|
||||
by running it:
|
||||
|
||||
```bash
|
||||
$ audio-conv
|
||||
```
|
||||
@@ -13,9 +13,19 @@ matches:
|
||||
bitrate: 160
|
||||
bitrate_type: vbr # or cbr
|
||||
|
||||
# for copy (copies file without transcoding it):
|
||||
# to:
|
||||
# codec: copy
|
||||
|
||||
# for mp3:
|
||||
# to:
|
||||
# codec: mp3
|
||||
# # one of: 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256 or 320
|
||||
# bitrate: 256
|
||||
# bitrate_type: vbr # or cbr
|
||||
|
||||
# for flac:
|
||||
# to:
|
||||
# codec: flac
|
||||
# # effort spend for the compression. 0 (fastes compression) to 9 (highest compression)
|
||||
# compression: 8
|
||||
|
||||
12
flake.lock
generated
12
flake.lock
generated
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1605370193,
|
||||
"narHash": "sha256-YyMTf3URDL/otKdKgtoMChu4vfVL3vCMkRqpGifhUn0=",
|
||||
"lastModified": 1648297722,
|
||||
"narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "5021eac20303a61fafe17224c087f5519baed54d",
|
||||
"rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -32,11 +32,11 @@
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1608633860,
|
||||
"narHash": "sha256-AGJfdJCR5jfIt8PqGiENXRqhthrS3Gxy8Wzb3Z2GsS4=",
|
||||
"lastModified": 1649352661,
|
||||
"narHash": "sha256-6IO5W02HKY6pj4uRgStJ2EjIENlpvbb99OlDBBzJMDQ=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "da1b28ab8f361fbe14dc539cd69ce1bfd015fd68",
|
||||
"rev": "2bc410afc423de1fd7ce1d84da9f294eee866b3f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -12,6 +12,7 @@ pub struct Config {
|
||||
pub from: PathBuf,
|
||||
pub to: PathBuf,
|
||||
pub matches: Vec<TranscodeMatch>,
|
||||
pub jobs: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -32,6 +33,12 @@ pub enum Transcode {
|
||||
bitrate_type: BitrateType,
|
||||
},
|
||||
|
||||
#[serde(rename = "flac")]
|
||||
Flac {
|
||||
#[serde(default = "default_flac_compression")]
|
||||
compression: u8,
|
||||
},
|
||||
|
||||
#[serde(rename = "mp3")]
|
||||
Mp3 {
|
||||
#[serde(default = "default_mp3_bitrate")]
|
||||
@@ -40,13 +47,18 @@ pub enum Transcode {
|
||||
#[serde(default = "bitrate_type_vbr")]
|
||||
bitrate_type: BitrateType,
|
||||
},
|
||||
|
||||
#[serde(rename = "copy")]
|
||||
Copy,
|
||||
}
|
||||
|
||||
impl Transcode {
|
||||
pub fn extension(&self) -> &'static str {
|
||||
match self {
|
||||
Transcode::Opus { .. } => "opus",
|
||||
Transcode::Flac { .. } => "flac",
|
||||
Transcode::Mp3 { .. } => "mp3",
|
||||
Transcode::Copy => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +67,10 @@ fn default_opus_bitrate() -> u16 {
|
||||
160
|
||||
}
|
||||
|
||||
fn default_flac_compression() -> u8 {
|
||||
5
|
||||
}
|
||||
|
||||
fn bitrate_type_vbr() -> BitrateType {
|
||||
BitrateType::Vbr
|
||||
}
|
||||
@@ -101,39 +117,50 @@ struct TranscodeMatchFile {
|
||||
}
|
||||
|
||||
pub fn config() -> Result<Config> {
|
||||
use clap::{App, Arg, SubCommand};
|
||||
use clap::{Arg, Command};
|
||||
|
||||
let arg_matches = App::new("audio-conv")
|
||||
let arg_matches = Command::new("audio-conv")
|
||||
.version(clap::crate_version!())
|
||||
.about("Converts audio files")
|
||||
.arg(
|
||||
Arg::with_name("config")
|
||||
.short("c")
|
||||
Arg::new("config")
|
||||
.short('c')
|
||||
.long("config")
|
||||
.allow_invalid_utf8(true)
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("path to an audio-conv config file, defaults to \"audio-conv.yaml\""),
|
||||
.help("Path to an audio-conv config file, defaults to \"audio-conv.yaml\""),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("from")
|
||||
.short("f")
|
||||
Arg::new("from")
|
||||
.short('f')
|
||||
.long("from")
|
||||
.allow_invalid_utf8(true)
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("from directory path"),
|
||||
.help("\"from\" directory path"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("to")
|
||||
.short("t")
|
||||
Arg::new("to")
|
||||
.short('t')
|
||||
.long("to")
|
||||
.allow_invalid_utf8(true)
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("to directory path"),
|
||||
.help("\"to\" directory path"),
|
||||
)
|
||||
.subcommand(SubCommand::with_name("init").about("writes an example config"))
|
||||
.arg(
|
||||
Arg::new("jobs")
|
||||
.short('j')
|
||||
.long("jobs")
|
||||
.required(false)
|
||||
.takes_value(true)
|
||||
.help("Allow N jobs/transcodes at once. Defaults to number of logical cores"),
|
||||
)
|
||||
.subcommand(Command::new("init").about("writes an example config"))
|
||||
.get_matches();
|
||||
|
||||
let current_dir = std::env::current_dir().context("could not get current directory")?;
|
||||
let current_dir = std::env::current_dir().context("Could not get current directory")?;
|
||||
|
||||
let config_path = arg_matches.value_of_os("config");
|
||||
let force_load = config_path.is_some();
|
||||
@@ -148,17 +175,17 @@ pub fn config() -> Result<Config> {
|
||||
.create_new(true)
|
||||
.open(&config_path)
|
||||
.and_then(|mut f| f.write_all(std::include_bytes!("../example.audio-conv.yaml")))
|
||||
.with_context(|| format!("unable to write config file to {}", config_path.display()))?;
|
||||
.with_context(|| format!("Unable to write config file to {}", config_path.display()))?;
|
||||
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
let config_dir = config_path
|
||||
.parent()
|
||||
.context("could not get parent directory of the config file")?;
|
||||
.context("Could not get parent directory of the config file")?;
|
||||
|
||||
let config_file = load_config_file(&config_path)
|
||||
.with_context(|| format!("failed loading config file \"{}\"", config_path.display()))?;
|
||||
.with_context(|| format!("Failed loading config file {}", config_path.display()))?;
|
||||
|
||||
if force_load && config_file.is_none() {
|
||||
return Err(Error::msg(format!(
|
||||
@@ -170,7 +197,7 @@ pub fn config() -> Result<Config> {
|
||||
let default_regex = RegexBuilder::new("\\.(flac|wav)$")
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
.expect("failed compiling default match regex");
|
||||
.expect("Failed compiling default match regex");
|
||||
|
||||
let transcode_matches = config_file
|
||||
.as_ref()
|
||||
@@ -183,8 +210,8 @@ pub fn config() -> Result<Config> {
|
||||
let glob = GlobBuilder::new(glob)
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
.context("failed building glob")?;
|
||||
let regex = Regex::new(glob.regex()).context("failed compiling regex")?;
|
||||
.context("Failed building glob")?;
|
||||
let regex = Regex::new(glob.regex()).context("Failed compiling regex")?;
|
||||
Ok(regex)
|
||||
});
|
||||
|
||||
@@ -192,7 +219,7 @@ pub fn config() -> Result<Config> {
|
||||
let regex = RegexBuilder::new(regex)
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
.context("failed compiling regex")?;
|
||||
.context("Failed compiling regex")?;
|
||||
Ok(regex)
|
||||
});
|
||||
|
||||
@@ -204,7 +231,7 @@ pub fn config() -> Result<Config> {
|
||||
let regex = RegexBuilder::new(&ext)
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
.context("failed compiling regex")?;
|
||||
.context("Failed compiling regex")?;
|
||||
Ok(regex)
|
||||
});
|
||||
|
||||
@@ -247,7 +274,7 @@ pub fn config() -> Result<Config> {
|
||||
})
|
||||
.ok_or_else(|| Error::msg("\"from\" not configured"))?
|
||||
.canonicalize()
|
||||
.context("could not canonicalize \"from\" path")?
|
||||
.context("Could not canonicalize \"from\" path")?
|
||||
},
|
||||
to: arg_matches
|
||||
.value_of_os("to")
|
||||
@@ -261,8 +288,19 @@ pub fn config() -> Result<Config> {
|
||||
})
|
||||
.ok_or_else(|| Error::msg("\"to\" not configured"))?
|
||||
.canonicalize()
|
||||
.context("could not canonicalize \"to\" path")?,
|
||||
.context("Could not canonicalize \"to\" path")?,
|
||||
matches: transcode_matches,
|
||||
jobs: arg_matches
|
||||
.value_of("jobs")
|
||||
.map(|jobs_str| {
|
||||
jobs_str.parse().with_context(|| {
|
||||
format!(
|
||||
"Could not parse \"jobs\" argument \"{}\" to a number",
|
||||
&jobs_str
|
||||
)
|
||||
})
|
||||
})
|
||||
.transpose()?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -273,6 +311,6 @@ fn load_config_file(path: &Path) -> Result<Option<ConfigFile>> {
|
||||
Err(err) => return Err(Error::new(err)),
|
||||
};
|
||||
let config: ConfigFile =
|
||||
serde_yaml::from_reader(&mut file).context("could not parse config file")?;
|
||||
serde_yaml::from_reader(&mut file).context("Could not parse config file")?;
|
||||
Ok(Some(config))
|
||||
}
|
||||
|
||||
214
src/main.rs
214
src/main.rs
@@ -1,11 +1,12 @@
|
||||
mod config;
|
||||
mod tag;
|
||||
mod ui;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::config::{Config, Transcode};
|
||||
use anyhow::{Context, Error, Result};
|
||||
use futures::{future, pin_mut, prelude::*};
|
||||
use glib::{subclass::prelude::*, GBoxed, GString};
|
||||
use gstreamer::{gst_element_error, prelude::*, Element};
|
||||
use futures::{pin_mut, prelude::*};
|
||||
use glib::{Boxed, GString};
|
||||
use gstreamer::{element_error, prelude::*, Element};
|
||||
use gstreamer_base::prelude::*;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
@@ -19,8 +20,8 @@ use std::{
|
||||
};
|
||||
use tokio::{fs, io::AsyncWriteExt, task, time::interval};
|
||||
|
||||
#[derive(Clone, Debug, GBoxed)]
|
||||
#[gboxed(type_name = "GBoxErrorWrapper")]
|
||||
#[derive(Clone, Debug, Boxed)]
|
||||
#[boxed_type(name = "GBoxErrorWrapper")]
|
||||
struct GBoxErrorWrapper(Arc<Error>);
|
||||
|
||||
impl GBoxErrorWrapper {
|
||||
@@ -52,12 +53,12 @@ struct GErrorMessage {
|
||||
|
||||
fn gmake<T: IsA<Element>>(factory_name: &str) -> Result<T> {
|
||||
let res = gstreamer::ElementFactory::make(factory_name, None)
|
||||
.with_context(|| format!("could not make gstreamer Element \"{}\"", factory_name))?
|
||||
.with_context(|| format!("Could not make gstreamer Element \"{}\"", factory_name))?
|
||||
.downcast()
|
||||
.ok()
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"could not cast gstreamer Element \"{}\" into `{}`",
|
||||
"Could not cast gstreamer Element \"{}\" into `{}`",
|
||||
factory_name,
|
||||
std::any::type_name::<T>()
|
||||
)
|
||||
@@ -68,7 +69,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>> + '_ {
|
||||
@@ -97,7 +98,7 @@ fn get_conversion_args(config: &Config) -> impl Iterator<Item = Result<Conversio
|
||||
|
||||
let rel_path = e.path().strip_prefix(&config.from).with_context(|| {
|
||||
format!(
|
||||
"unable to get relative path for {} from {}",
|
||||
"Unable to get relative path for {} from {}",
|
||||
e.path().display(),
|
||||
config.from.display()
|
||||
)
|
||||
@@ -112,7 +113,10 @@ fn get_conversion_args(config: &Config) -> impl Iterator<Item = Result<Conversio
|
||||
.map_err(Error::new)
|
||||
.and_then(|md| md.modified().map_err(Error::new))
|
||||
.with_context(|| {
|
||||
format!("unable to get mtime for from file {}", e.path().display())
|
||||
format!(
|
||||
"Unable to get mtime for \"from\" file {}",
|
||||
e.path().display()
|
||||
)
|
||||
})?;
|
||||
let to_mtime = to.metadata().and_then(|md| md.modified());
|
||||
match to_mtime {
|
||||
@@ -120,7 +124,7 @@ fn get_conversion_args(config: &Config) -> impl Iterator<Item = Result<Conversio
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => true,
|
||||
Err(err) => {
|
||||
return Err(err).with_context(|| {
|
||||
format!("unable to get mtime for to file {}", to.display())
|
||||
format!("Unable to get mtime for \"to\" file {}", to.display())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -147,15 +151,15 @@ async fn main() -> Result<()> {
|
||||
let main_handle = async move {
|
||||
let ok = task::spawn_local(main_loop(ui_queue))
|
||||
.await
|
||||
.context("main task failed")??;
|
||||
.context("Main task failed")??;
|
||||
Result::<_>::Ok(ok)
|
||||
};
|
||||
|
||||
let ui_handle = async move {
|
||||
let ok = task::spawn_local(ui_fut)
|
||||
.await
|
||||
.context("ui task failed")?
|
||||
.context("ui failed")?;
|
||||
.context("Ui task failed")?
|
||||
.context("Ui failed")?;
|
||||
Result::<_>::Ok(ok)
|
||||
};
|
||||
|
||||
@@ -168,20 +172,23 @@ async fn main() -> Result<()> {
|
||||
async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
|
||||
let (config, conv_args) = task::spawn_blocking(|| -> Result<_> {
|
||||
gstreamer::init()?;
|
||||
let config = config::config().context("could not get the config")?;
|
||||
gstreamer::tags::register::<tag::MbArtistId>();
|
||||
gstreamer::tags::register::<tag::MbAlbumArtistId>();
|
||||
|
||||
let config = config::config().context("Could not get the config")?;
|
||||
|
||||
let conv_args = get_conversion_args(&config)
|
||||
.collect::<Result<Vec<_>>>()
|
||||
.context("failed loading dir structure")?;
|
||||
.context("Failed loading dir structure")?;
|
||||
|
||||
Ok((config, conv_args))
|
||||
})
|
||||
.await
|
||||
.context("init task failed")??;
|
||||
.context("Init task failed")??;
|
||||
|
||||
let log_path = Path::new(".")
|
||||
.canonicalize()
|
||||
.context("unable to canonicalize path to log file")?
|
||||
.context("Unable to canonicalize path to log file")?
|
||||
.join("audio-conv.log");
|
||||
|
||||
ui_queue.push(ui::Msg::Init {
|
||||
@@ -189,9 +196,11 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
|
||||
log_path: log_path.clone(),
|
||||
});
|
||||
|
||||
let concurrent_jobs = config.jobs.unwrap_or_else(|| num_cpus::get());
|
||||
|
||||
stream::iter(conv_args.into_iter().enumerate())
|
||||
.map(Ok)
|
||||
.try_for_each_concurrent(num_cpus::get(), |(i, args)| {
|
||||
.try_for_each_concurrent(concurrent_jobs, |(i, args)| {
|
||||
let config = &config;
|
||||
let ui_queue = &ui_queue;
|
||||
let log_path = &log_path;
|
||||
@@ -206,7 +215,7 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
|
||||
Ok(()) => ui_queue.push(ui::Msg::TaskEnd { id: i }),
|
||||
Err(err) => {
|
||||
let err = err.context(format!(
|
||||
"failed transcoding \"{}\"",
|
||||
"Transcoding failed for {}",
|
||||
args.rel_from_path.display()
|
||||
));
|
||||
|
||||
@@ -264,15 +273,64 @@ async fn transcode(
|
||||
) -> Result<()> {
|
||||
let from_path = config.from.join(&args.rel_from_path);
|
||||
let mut to_path = config.to.join(&args.rel_from_path);
|
||||
to_path.set_extension(args.transcode.extension());
|
||||
|
||||
let file_src: Element = gmake("filesrc")?;
|
||||
file_src.set_property("location", &path_to_gstring(&from_path))?;
|
||||
fs::create_dir_all(
|
||||
to_path
|
||||
.parent()
|
||||
.with_context(|| format!("Could not get parent dir for {}", to_path.display()))?,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// encode into a tmp file first, then rename to actuall file name, that way we're writing
|
||||
// "whole" files to the intended file path, ignoring partial files in the mtime check
|
||||
let to_path_tmp = to_path.with_extension("tmp");
|
||||
|
||||
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()
|
||||
)
|
||||
})?;
|
||||
}
|
||||
_ => {
|
||||
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.try_set_property("location", &path_to_gstring(&from_path))?;
|
||||
|
||||
let decodebin: Element = gmake("decodebin")?;
|
||||
|
||||
let src_elems: &[&Element] = &[&file_src, &decodebin];
|
||||
@@ -285,10 +343,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() {
|
||||
@@ -299,9 +354,9 @@ async fn transcode(
|
||||
}
|
||||
};
|
||||
|
||||
let is_audio = src_pad.get_current_caps().and_then(|caps| {
|
||||
caps.get_structure(0).map(|s| {
|
||||
let name = s.get_name();
|
||||
let is_audio = src_pad.current_caps().and_then(|caps| {
|
||||
caps.structure(0).map(|s| {
|
||||
let name = s.name();
|
||||
name.starts_with("audio/")
|
||||
})
|
||||
});
|
||||
@@ -309,7 +364,7 @@ async fn transcode(
|
||||
None => {
|
||||
return Err(Error::msg(format!(
|
||||
"Failed to get media type from pad {}",
|
||||
src_pad.get_name()
|
||||
src_pad.name()
|
||||
)));
|
||||
}
|
||||
Some(false) => {
|
||||
@@ -321,7 +376,7 @@ async fn transcode(
|
||||
|
||||
let resample: Element = gmake("audioresample")?;
|
||||
// quality from 0 to 10
|
||||
resample.set_property("quality", &7)?;
|
||||
resample.try_set_property("quality", &10i32)?;
|
||||
|
||||
let mut dest_elems = vec![
|
||||
resample,
|
||||
@@ -329,17 +384,17 @@ async fn transcode(
|
||||
gmake("audioconvert")?,
|
||||
];
|
||||
|
||||
match &transcode_args {
|
||||
config::Transcode::Opus {
|
||||
match &transcode {
|
||||
Transcode::Opus {
|
||||
bitrate,
|
||||
bitrate_type,
|
||||
} => {
|
||||
let encoder: Element = gmake("opusenc")?;
|
||||
encoder.set_property(
|
||||
encoder.try_set_property(
|
||||
"bitrate",
|
||||
&i32::from(*bitrate)
|
||||
.checked_mul(1_000)
|
||||
.context("bitrate overflowed")?,
|
||||
.context("Bitrate overflowed")?,
|
||||
)?;
|
||||
encoder.set_property_from_str(
|
||||
"bitrate-type",
|
||||
@@ -352,15 +407,22 @@ async fn transcode(
|
||||
dest_elems.push(encoder);
|
||||
dest_elems.push(gmake("oggmux")?);
|
||||
}
|
||||
config::Transcode::Mp3 {
|
||||
|
||||
Transcode::Flac { compression } => {
|
||||
let encoder: Element = gmake("flacenc")?;
|
||||
encoder.set_property_from_str("quality", &compression.to_string());
|
||||
dest_elems.push(encoder);
|
||||
}
|
||||
|
||||
Transcode::Mp3 {
|
||||
bitrate,
|
||||
bitrate_type,
|
||||
} => {
|
||||
let encoder: Element = gmake("lamemp3enc")?;
|
||||
// target: "1" = "bitrate"
|
||||
encoder.set_property_from_str("target", "1");
|
||||
encoder.set_property("bitrate", &i32::from(*bitrate))?;
|
||||
encoder.set_property(
|
||||
encoder.try_set_property("bitrate", &i32::from(*bitrate))?;
|
||||
encoder.try_set_property(
|
||||
"cbr",
|
||||
match bitrate_type {
|
||||
config::BitrateType::Vbr => &false,
|
||||
@@ -371,10 +433,15 @@ async fn transcode(
|
||||
dest_elems.push(encoder);
|
||||
dest_elems.push(gmake("id3v2mux")?);
|
||||
}
|
||||
|
||||
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.try_set_property("location", &path_to_gstring(&to_path_clone))?;
|
||||
file_dest.set_sync(false);
|
||||
dest_elems.push(file_dest.upcast());
|
||||
|
||||
@@ -389,7 +456,7 @@ async fn transcode(
|
||||
let sink_pad = dest_elems
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.get_static_pad("sink")
|
||||
.static_pad("sink")
|
||||
.expect("1. dest element has no sinkpad");
|
||||
src_pad.link(&sink_pad)?;
|
||||
|
||||
@@ -401,7 +468,7 @@ async fn transcode(
|
||||
.field("error", &GBoxErrorWrapper::new(err))
|
||||
.build();
|
||||
|
||||
gst_element_error!(
|
||||
element_error!(
|
||||
decodebin,
|
||||
gstreamer::LibraryError::Failed,
|
||||
("Failed to insert sink"),
|
||||
@@ -410,16 +477,8 @@ async fn transcode(
|
||||
}
|
||||
});
|
||||
|
||||
let bus = pipeline.get_bus().context("pipe get bus")?;
|
||||
let bus = pipeline.bus().context("Could not get bus for pipeline")?;
|
||||
|
||||
fs::create_dir_all(
|
||||
to_path
|
||||
.parent()
|
||||
.with_context(|| format!("could not get parent dir for {}", to_path.display()))?,
|
||||
)
|
||||
.await?;
|
||||
|
||||
rm_file_on_err(&to_path_tmp, async {
|
||||
pipeline
|
||||
.set_state(gstreamer::State::Playing)
|
||||
.context("Unable to set the pipeline to the `Playing` state")?;
|
||||
@@ -439,37 +498,43 @@ async fn transcode(
|
||||
Ok(false)
|
||||
}
|
||||
MessageView::Error(err) => {
|
||||
pipeline.set_state(gstreamer::State::Null).context(
|
||||
"Unable to set the pipeline to the `Null` state, after error",
|
||||
)?;
|
||||
let pipe_stop_res = pipeline.set_state(gstreamer::State::Null);
|
||||
|
||||
let err = err
|
||||
.get_details()
|
||||
let err: Error = err
|
||||
.details()
|
||||
.and_then(|details| {
|
||||
if details.get_name() != "error-details" {
|
||||
if details.name() != "error-details" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let err = details
|
||||
.get::<&GBoxErrorWrapper>("error")
|
||||
.unwrap()
|
||||
.map(|err| err.clone().into())
|
||||
.expect("error-details message without actual error");
|
||||
.clone()
|
||||
.into();
|
||||
Some(err)
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
GErrorMessage {
|
||||
src: msg
|
||||
.get_src()
|
||||
.map(|s| String::from(s.get_path_string()))
|
||||
.src()
|
||||
.map(|s| String::from(s.path_string()))
|
||||
.unwrap_or_else(|| String::from("None")),
|
||||
error: err.get_error().to_string(),
|
||||
debug: err.get_debug(),
|
||||
source: err.get_error(),
|
||||
error: err.error().to_string(),
|
||||
debug: err.debug(),
|
||||
source: err.error(),
|
||||
}
|
||||
.into()
|
||||
});
|
||||
|
||||
if let Err(pipe_err) = pipe_stop_res {
|
||||
let err = err.context(pipe_err).context(
|
||||
"Unable to set the pipeline to the `Null` state, after error",
|
||||
);
|
||||
Err(err)
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
_ => Ok(true),
|
||||
}
|
||||
@@ -482,8 +547,7 @@ async fn transcode(
|
||||
}
|
||||
})
|
||||
.try_for_each(|_| futures::future::ready(Ok(())))
|
||||
.await
|
||||
.context("failed converting")?;
|
||||
.await?;
|
||||
|
||||
Result::<_>::Ok(())
|
||||
};
|
||||
@@ -498,7 +562,7 @@ async fn transcode(
|
||||
|
||||
let dur = decodebin
|
||||
.query_duration::<ClockTime>()
|
||||
.and_then(|time| time.nanoseconds());
|
||||
.map(|time| time.nseconds());
|
||||
|
||||
let ratio = dur.and_then(|dur| {
|
||||
if dur == 0 {
|
||||
@@ -507,11 +571,11 @@ async fn transcode(
|
||||
|
||||
let pos = decodebin
|
||||
.query_position::<ClockTime>()
|
||||
.and_then(|time| time.nanoseconds());
|
||||
.map(|time| time.nseconds());
|
||||
|
||||
pos.map(|pos| {
|
||||
let ratio = pos as f64 / dur as f64;
|
||||
ratio.max(0.0).min(1.0)
|
||||
ratio.clamp(0.0, 1.0)
|
||||
})
|
||||
});
|
||||
|
||||
@@ -533,25 +597,21 @@ 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) -> F::Output
|
||||
async fn rm_file_on_err<F, T>(path: &Path, f: F) -> Result<T>
|
||||
where
|
||||
F: Future<Output = Result<T>>,
|
||||
{
|
||||
match f.await {
|
||||
Err(err) => match fs::remove_file(path).await {
|
||||
Ok(..) => Err(err),
|
||||
Ok(()) => Err(err),
|
||||
Err(fs_err) if fs_err.kind() == std::io::ErrorKind::NotFound => Err(err),
|
||||
Err(fs_err) => {
|
||||
let err = err
|
||||
.context(fs_err)
|
||||
.context(format!("removing {} failed", path.display()));
|
||||
.context(format!("Removing file {} failed", path.display()));
|
||||
Err(err)
|
||||
}
|
||||
},
|
||||
|
||||
45
src/tag.rs
Normal file
45
src/tag.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use glib::Value;
|
||||
use gstreamer::{
|
||||
tags::{merge_strings_with_comma, CustomTag},
|
||||
Tag, TagFlag,
|
||||
};
|
||||
|
||||
pub struct MbArtistId;
|
||||
|
||||
impl<'a> Tag<'a> for MbArtistId {
|
||||
type TagType = &'a str;
|
||||
|
||||
fn tag_name<'b>() -> &'b str {
|
||||
"musicbrainz-artistid"
|
||||
}
|
||||
}
|
||||
|
||||
impl CustomTag<'_> for MbArtistId {
|
||||
const FLAG: TagFlag = TagFlag::Meta;
|
||||
const NICK: &'static str = "artist ID";
|
||||
const DESCRIPTION: &'static str = "MusicBrainz artist ID";
|
||||
|
||||
fn merge_func(src: &Value) -> Value {
|
||||
merge_strings_with_comma(src)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MbAlbumArtistId;
|
||||
|
||||
impl<'a> Tag<'a> for MbAlbumArtistId {
|
||||
type TagType = &'a str;
|
||||
|
||||
fn tag_name<'b>() -> &'b str {
|
||||
"musicbrainz-albumartistid"
|
||||
}
|
||||
}
|
||||
|
||||
impl CustomTag<'_> for MbAlbumArtistId {
|
||||
const FLAG: TagFlag = TagFlag::Meta;
|
||||
const NICK: &'static str = "album artist ID";
|
||||
const DESCRIPTION: &'static str = "MusicBrainz album artist ID";
|
||||
|
||||
fn merge_func(src: &Value) -> Value {
|
||||
merge_strings_with_comma(src)
|
||||
}
|
||||
}
|
||||
12
src/ui.rs
12
src/ui.rs
@@ -88,7 +88,7 @@ impl State {
|
||||
Msg::TaskEnd { id } => {
|
||||
self.running_tasks
|
||||
.remove(&id)
|
||||
.context("unable to remove finished task; could't find task")?;
|
||||
.context("Unable to remove finished task; could't find task")?;
|
||||
self.ended_tasks += 1;
|
||||
}
|
||||
Msg::TaskProgress { id, ratio } => {
|
||||
@@ -102,7 +102,7 @@ impl State {
|
||||
// TODO
|
||||
self.running_tasks
|
||||
.remove(&id)
|
||||
.context("unable to remove errored task; could't find task")?;
|
||||
.context("Unable to remove errored task; could't find task")?;
|
||||
self.ended_tasks += 1;
|
||||
self.has_errored = true;
|
||||
}
|
||||
@@ -136,7 +136,7 @@ impl State {
|
||||
running_tasks.sort_by_key(|task| task.id);
|
||||
|
||||
if !self.has_rendered {
|
||||
self.terminal.clear().context("cleaning ui failed")?;
|
||||
self.terminal.clear().context("Clearing ui failed")?;
|
||||
self.has_rendered = true;
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ impl State {
|
||||
chunks[1],
|
||||
);
|
||||
})
|
||||
.context("rendering ui failed")?;
|
||||
.context("Rendering ui failed")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -267,8 +267,8 @@ pub fn init() -> (MsgQueue, impl Future<Output = Result<()>>) {
|
||||
}
|
||||
})
|
||||
.await
|
||||
.context("ui update task failed")?
|
||||
.context("ui update failed")?;
|
||||
.context("Ui update task failed")?
|
||||
.context("Ui update failed")?;
|
||||
|
||||
match render_res {
|
||||
Some(s) => wrapped = Some(s),
|
||||
|
||||
Reference in New Issue
Block a user