18 Commits

10 changed files with 1302 additions and 1239 deletions

View File

@@ -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
View File

@@ -0,0 +1 @@
hard_tabs = true

View File

@@ -1,5 +1,18 @@
# 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

506
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package]
name = "audio-conv"
version = "1.2.0"
version = "1.3.0"
edition = "2018"
description = "Copies directory structure and converts audio files in it"
authors = ["Thomas Heck <t@b128.net>"]
@@ -21,22 +21,22 @@ include = [
]
[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.14", default-features = false, features = ["crossterm"] }
tui = { version = "0.17", default-features = false, features = ["crossterm"] }
[dependencies.tokio]
version = "1"

12
flake.lock generated
View File

@@ -2,11 +2,11 @@
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1618868421,
"narHash": "sha256-vyoJhLV6cJ8/tWz+l9HZLIkb9Rd9esE7p+0RL6zDR6Y=",
"lastModified": 1648297722,
"narHash": "sha256-W+qlPsiZd8F3XkzXOzAoR+mpFqzm3ekQkJNa+PIh1BQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "eed214942bcfb3a8cc09eb3b28ca7d7221e44a94",
"rev": "0f8662f1319ad6abf89b3380dd2722369fc51ade",
"type": "github"
},
"original": {
@@ -32,11 +32,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1619118726,
"narHash": "sha256-eEB4bIcl/REE5c9rMl4k3FmI9clTfjoFky5dm73gthY=",
"lastModified": 1649352661,
"narHash": "sha256-6IO5W02HKY6pj4uRgStJ2EjIENlpvbb99OlDBBzJMDQ=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "14c8ae6efbf38dd4fd5fedaeeea2641c9c0e1e97",
"rev": "2bc410afc423de1fd7ce1d84da9f294eee866b3f",
"type": "github"
},
"original": {

View File

@@ -117,44 +117,47 @@ 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\""),
)
.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"),
)
.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"),
)
.arg(
Arg::with_name("jobs")
.short("j")
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(SubCommand::with_name("init").about("writes an example config"))
.subcommand(Command::new("init").about("writes an example config"))
.get_matches();
let current_dir = std::env::current_dir().context("Could not get current directory")?;
@@ -288,15 +291,8 @@ pub fn config() -> Result<Config> {
.context("Could not canonicalize \"to\" path")?,
matches: transcode_matches,
jobs: arg_matches
.value_of_os("jobs")
.map(|jobs_os_str| {
let jobs_str = jobs_os_str.to_str().with_context(|| {
// TODO: use `OsStr.display` when it lands
// https://github.com/rust-lang/rust/pull/80841
format!(
"Could not convert \"jobs\" argument to string due to invalid characters",
)
})?;
.value_of("jobs")
.map(|jobs_str| {
jobs_str.parse().with_context(|| {
format!(
"Could not parse \"jobs\" argument \"{}\" to a number",

View File

@@ -1,11 +1,12 @@
mod config;
mod tag;
mod ui;
use crate::config::{Config, Transcode};
use anyhow::{Context, Error, Result};
use futures::{pin_mut, prelude::*};
use glib::{subclass::prelude::*, GBoxed, GString};
use gstreamer::{gst_element_error, prelude::*, Element};
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 {
@@ -171,6 +172,9 @@ async fn main() -> Result<()> {
async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
let (config, conv_args) = task::spawn_blocking(|| -> Result<_> {
gstreamer::init()?;
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)
@@ -325,7 +329,7 @@ async fn transcode_gstreamer(
queue: &ui::MsgQueue,
) -> Result<()> {
let file_src: Element = gmake("filesrc")?;
file_src.set_property("location", &path_to_gstring(&from_path))?;
file_src.try_set_property("location", &path_to_gstring(&from_path))?;
let decodebin: Element = gmake("decodebin")?;
@@ -350,9 +354,9 @@ async fn transcode_gstreamer(
}
};
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/")
})
});
@@ -360,7 +364,7 @@ async fn transcode_gstreamer(
None => {
return Err(Error::msg(format!(
"Failed to get media type from pad {}",
src_pad.get_name()
src_pad.name()
)));
}
Some(false) => {
@@ -372,7 +376,7 @@ async fn transcode_gstreamer(
let resample: Element = gmake("audioresample")?;
// quality from 0 to 10
resample.set_property("quality", &10)?;
resample.try_set_property("quality", &10i32)?;
let mut dest_elems = vec![
resample,
@@ -386,7 +390,7 @@ async fn transcode_gstreamer(
bitrate_type,
} => {
let encoder: Element = gmake("opusenc")?;
encoder.set_property(
encoder.try_set_property(
"bitrate",
&i32::from(*bitrate)
.checked_mul(1_000)
@@ -417,8 +421,8 @@ async fn transcode_gstreamer(
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,
@@ -437,7 +441,7 @@ async fn transcode_gstreamer(
};
let file_dest: gstreamer_base::BaseSink = gmake("filesink")?;
file_dest.set_property("location", &path_to_gstring(&to_path_clone))?;
file_dest.try_set_property("location", &path_to_gstring(&to_path_clone))?;
file_dest.set_sync(false);
dest_elems.push(file_dest.upcast());
@@ -452,7 +456,7 @@ async fn transcode_gstreamer(
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)?;
@@ -464,7 +468,7 @@ async fn transcode_gstreamer(
.field("error", &GBoxErrorWrapper::new(err))
.build();
gst_element_error!(
element_error!(
decodebin,
gstreamer::LibraryError::Failed,
("Failed to insert sink"),
@@ -473,9 +477,7 @@ async fn transcode_gstreamer(
}
});
let bus = pipeline
.get_bus()
.context("Could not get bus for pipeline")?;
let bus = pipeline.bus().context("Could not get bus for pipeline")?;
pipeline
.set_state(gstreamer::State::Playing)
@@ -499,28 +501,28 @@ async fn transcode_gstreamer(
let pipe_stop_res = pipeline.set_state(gstreamer::State::Null);
let err: Error = err
.get_details()
.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()
});
@@ -560,7 +562,7 @@ async fn transcode_gstreamer(
let dur = decodebin
.query_duration::<ClockTime>()
.and_then(|time| time.nanoseconds());
.map(|time| time.nseconds());
let ratio = dur.and_then(|dur| {
if dur == 0 {
@@ -569,11 +571,11 @@ async fn transcode_gstreamer(
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)
})
});

45
src/tag.rs Normal file
View 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)
}
}