26 Commits
v1.2.0 ... main

Author SHA1 Message Date
5aedb0d14a chore: bump version (v1.3.2) 2026-02-15 00:18:59 +01:00
4b9cc3acd3 chore: update to 2024 rust edition 2026-02-15 00:16:19 +01:00
d746fbb152 chore: update deps 2026-02-14 23:49:53 +01:00
6f762742bd chore: bump version (v1.3.1) 2024-07-25 22:44:40 +02:00
1c8ff6d2e9 docs: fix required gstreamer version in readme 2024-07-25 22:42:07 +02:00
ec9188fbaf chore: use new edition 2024-07-25 22:41:27 +02:00
5c11600c64 chore: remove nix files due to lack of maintenance 2024-07-25 22:31:46 +02:00
05ad75e20d chore: update deps 2024-07-25 22:27:33 +02:00
a10345fbbe build: bump version (v1.3.0) 2022-04-07 21:12:25 +02:00
f3423eea53 fix: fix "from", "to" & "config" cli argument processing 2022-04-07 21:10:37 +02:00
80e3d02cb4 refactor: simplify config processing 2022-04-07 20:58:05 +02:00
b7a7abbf61 refactor: fmt 2022-04-07 19:36:49 +02:00
5abadc3131 build: update deps 2022-04-07 19:35:37 +02:00
9c86cdcc62 build: update deps 2022-02-24 21:45:52 +01:00
605fa5c15b build: update deps 2022-02-04 23:29:19 +01:00
9c1d39ba5f fix: allow multiple values for tag "musicbrainz-artistid" & "musicbrainz-albumartistid"
issue #2
2021-12-14 19:08:05 +01:00
c6c9da2f27 build: update deps 2021-12-14 18:49:01 +01:00
6872e7897b build: bump version (v1.2.2) 2021-08-25 17:11:23 +02:00
2d1497cb36 build: update deps 2021-08-25 17:09:33 +02:00
f1fb3506b5 build: update deps 2021-07-25 14:37:08 +02:00
f4050fe645 refactor: switch from space to tab indentation 2021-07-10 10:07:30 +02:00
b533f059d7 build: bump version (v1.2.1) 2021-07-05 20:02:49 +02:00
00a25e168d refactor: use more readable clamp method 2021-07-05 19:53:49 +02:00
b51c9939c1 build: upgrade deps 2021-07-05 19:46:06 +02:00
c22d45818e build: update deps 2021-06-18 11:46:48 +02:00
18cc852e6b build: update deps 2021-05-27 22:52:48 +02:00
12 changed files with 1672 additions and 1498 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,26 @@
# Changelog
## v1.3.2
* dependencies upgraded
## v1.3.1
* dependencies upgraded
## 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

987
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
[package]
name = "audio-conv"
version = "1.2.0"
edition = "2018"
version = "1.3.2"
edition = "2024"
description = "Copies directory structure and converts audio files in it"
authors = ["Thomas Heck <t@b128.net>"]
repository = "https://gitlab.com/chpio/audio-conv"
@@ -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 = { version = "0.24", features = ["v1_16"] }
gstreamer-base = { version = "0.24", features = ["v1_16"] }
gstreamer-audio = { version = "0.24", features = ["v1_16"] }
glib = "0.21"
futures = "0.3"
num_cpus = "1"
walkdir = "2"
libc = "0.2"
anyhow = "1"
clap = "2"
clap = { version = "4", features = ["cargo"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
serde_yaml = "0.9"
regex = "1"
globset = "0.4"
derive_more = "0.99"
tui = { version = "0.14", default-features = false, features = ["crossterm"] }
derive_more = { version = "2", features = ["full"] }
tui = { version = "0.19", default-features = false, features = ["crossterm"] }
[dependencies.tokio]
version = "1"

View File

@@ -5,7 +5,7 @@ second path. The directory structure from the first path gets also copied to the
## Dependencies
Requires *gstreamer* version 1.10 or higher with the *base* plugin.
Requires *gstreamer* version 1.16 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.

58
flake.lock generated
View File

@@ -1,58 +0,0 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1618868421,
"narHash": "sha256-vyoJhLV6cJ8/tWz+l9HZLIkb9Rd9esE7p+0RL6zDR6Y=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "eed214942bcfb3a8cc09eb3b28ca7d7221e44a94",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"import-cargo": {
"locked": {
"lastModified": 1594305518,
"narHash": "sha256-frtArgN42rSaEcEOYWg8sVPMUK+Zgch3c+wejcpX3DY=",
"owner": "edolstra",
"repo": "import-cargo",
"rev": "25d40be4a73d40a2572e0cc233b83253554f06c5",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "import-cargo",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1619118726,
"narHash": "sha256-eEB4bIcl/REE5c9rMl4k3FmI9clTfjoFky5dm73gthY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "14c8ae6efbf38dd4fd5fedaeeea2641c9c0e1e97",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"import-cargo": "import-cargo",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

View File

@@ -1,63 +0,0 @@
{
description = "Converts audio files";
inputs = {
nixpkgs.url = github:NixOS/nixpkgs;
flake-utils.url = "github:numtide/flake-utils";
import-cargo.url = github:edolstra/import-cargo;
};
outputs = { self, flake-utils, nixpkgs, import-cargo }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
buildtimeDeps = with pkgs; [
cargo
rustc
pkg-config
];
runtimeDeps = with pkgs; [
gst_all_1.gstreamer
# needed for opus, resample, ...
gst_all_1.gst-plugins-base
# needed for flac
gst_all_1.gst-plugins-good
];
inherit (import-cargo.builders) importCargo;
in {
defaultPackage = pkgs.stdenv.mkDerivation {
name = "audio-conv";
src = self;
nativeBuildInputs = [
# setupHook which makes sure that a CARGO_HOME with vendored dependencies
# exists
(importCargo { lockFile = ./Cargo.lock; inherit pkgs; }).cargoHome
]
++ buildtimeDeps;
buildInputs = runtimeDeps;
buildPhase = ''
cargo build --release --offline
'';
installPhase = ''
install -Dm775 ./target/release/audio-conv $out/bin/audio-conv
'';
};
devShell = pkgs.stdenv.mkDerivation {
name = "audio-conv";
buildInputs = [ pkgs.rustfmt pkgs.rust-analyzer ]
++ buildtimeDeps
++ runtimeDeps;
};
}
);
}

View File

@@ -1,4 +1,5 @@
use anyhow::{Context, Error, Result};
use clap::{ArgAction, builder::ValueParser};
use globset::GlobBuilder;
use regex::bytes::{Regex, RegexBuilder};
use serde::Deserialize;
@@ -103,6 +104,8 @@ struct ConfigFile {
#[serde(default)]
matches: Vec<TranscodeMatchFile>,
jobs: Option<usize>,
}
#[derive(Debug, Deserialize)]
@@ -117,50 +120,54 @@ 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")
.required(false)
.takes_value(true)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.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")
.required(false)
.takes_value(true)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.help("\"from\" directory path"),
)
.arg(
Arg::with_name("to")
.short("t")
Arg::new("to")
.short('t')
.long("to")
.required(false)
.takes_value(true)
.value_parser(ValueParser::path_buf())
.action(ArgAction::Set)
.help("\"to\" directory path"),
)
.arg(
Arg::with_name("jobs")
.short("j")
Arg::new("jobs")
.short('j')
.long("jobs")
.required(false)
.takes_value(true)
.value_parser(clap::value_parser!(usize))
.action(ArgAction::Set)
.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")?;
let config_path = arg_matches.value_of_os("config");
let force_load = config_path.is_some();
let config_path = arg_matches.get_one::<PathBuf>("config");
let enforce_config_load = config_path.is_some();
let config_path = config_path
.map(AsRef::<Path>::as_ref)
.unwrap_or_else(|| AsRef::<Path>::as_ref("audio-conv.yaml"));
@@ -184,7 +191,7 @@ pub fn config() -> Result<Config> {
let config_file = load_config_file(&config_path)
.with_context(|| format!("Failed loading config file {}", config_path.display()))?;
if force_load && config_file.is_none() {
if enforce_config_load && config_file.is_none() {
return Err(Error::msg(format!(
"could not find config file \"{}\"",
config_path.display()
@@ -260,7 +267,7 @@ pub fn config() -> Result<Config> {
Ok(Config {
from: {
arg_matches
.value_of_os("from")
.get_one::<PathBuf>("from")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
@@ -274,7 +281,7 @@ pub fn config() -> Result<Config> {
.context("Could not canonicalize \"from\" path")?
},
to: arg_matches
.value_of_os("to")
.get_one::<PathBuf>("to")
.map(|p| current_dir.join(p))
.or_else(|| {
config_file
@@ -288,23 +295,18 @@ 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",
)
})?;
jobs_str.parse().with_context(|| {
format!(
"Could not parse \"jobs\" argument \"{}\" to a number",
&jobs_str
)
})
})
.transpose()?,
.get_one("jobs")
.copied()
.or_else(|| config_file.as_ref().map(|c| c.jobs).flatten()),
// .map(|jobs_str| {
// jobs_str.parse().with_context(|| {
// format!(
// "Could not parse \"jobs\" argument \"{}\" to a number",
// &jobs_str
// )
// })
// })
// .transpose()?,
})
}

View File

@@ -1,16 +1,17 @@
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;
use gstreamer::{Element, element_error, prelude::*};
use gstreamer_base::prelude::*;
use std::{
borrow::Cow,
error::Error as StdError,
ffi, fmt,
fmt,
fmt::Write as FmtWrite,
path::{Path, PathBuf},
result::Result as StdResult,
@@ -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 {
@@ -42,7 +43,7 @@ impl fmt::Display for GBoxErrorWrapper {
}
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[display(fmt = "Received error from {}: {} (debug: {:?})", src, error, debug)]
#[display("Received error from {}: {} (debug: {:?})", src, error, debug)]
struct GErrorMessage {
src: String,
error: String,
@@ -50,8 +51,15 @@ struct GErrorMessage {
source: glib::Error,
}
fn gmake<T: IsA<Element>>(factory_name: &str) -> Result<T> {
let res = gstreamer::ElementFactory::make(factory_name, None)
fn gmake<T: IsA<Element>>(factory_name: &str, properties: &[(&str, &dyn ToValue)]) -> Result<T> {
let builder = gstreamer::ElementFactory::make(factory_name);
let builder = properties
.into_iter()
.fold(builder, |builder, (name, value)| {
builder.property(name, value.to_value())
});
let res = builder
.build()
.with_context(|| format!("Could not make gstreamer Element \"{}\"", factory_name))?
.downcast()
.ok()
@@ -124,7 +132,7 @@ fn get_conversion_args(config: &Config) -> impl Iterator<Item = Result<Conversio
Err(err) => {
return Err(err).with_context(|| {
format!("Unable to get mtime for \"to\" file {}", to.display())
})
});
}
}
};
@@ -171,6 +179,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)
@@ -324,14 +335,13 @@ async fn transcode_gstreamer(
task_id: usize,
queue: &ui::MsgQueue,
) -> Result<()> {
let file_src: Element = gmake("filesrc")?;
file_src.set_property("location", &path_to_gstring(&from_path))?;
let file_src: Element = gmake("filesrc", &[("location", &from_path)])?;
let decodebin: Element = gmake("decodebin")?;
let decodebin: Element = gmake("decodebin", &[])?;
let src_elems: &[&Element] = &[&file_src, &decodebin];
let pipeline = gstreamer::Pipeline::new(None);
let pipeline = gstreamer::Pipeline::new();
pipeline.add_many(src_elems)?;
Element::link_many(src_elems)?;
@@ -350,9 +360,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 +370,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) => {
@@ -370,14 +380,18 @@ async fn transcode_gstreamer(
Some(true) => {}
}
let resample: Element = gmake("audioresample")?;
let resample: Element = gmake(
"audioresample",
&[
// quality from 0 to 10
resample.set_property("quality", &10)?;
("quality", &10i32),
],
)?;
let mut dest_elems = vec![
resample,
// `audioconvert` converts audio format, bitdepth, ...
gmake("audioconvert")?,
gmake("audioconvert", &[])?,
];
match &transcode {
@@ -385,28 +399,32 @@ async fn transcode_gstreamer(
bitrate,
bitrate_type,
} => {
let encoder: Element = gmake("opusenc")?;
encoder.set_property(
let encoder: Element = gmake(
"opusenc",
&[
(
"bitrate",
&i32::from(*bitrate)
.checked_mul(1_000)
.context("Bitrate overflowed")?,
)?;
encoder.set_property_from_str(
),
(
"bitrate-type",
match bitrate_type {
config::BitrateType::Vbr => "1",
config::BitrateType::Cbr => "0",
config::BitrateType::Vbr => &"1",
config::BitrateType::Cbr => &"0",
},
);
),
],
)?;
dest_elems.push(encoder);
dest_elems.push(gmake("oggmux")?);
dest_elems.push(gmake("oggmux", &[])?);
}
Transcode::Flac { compression } => {
let encoder: Element = gmake("flacenc")?;
encoder.set_property_from_str("quality", &compression.to_string());
let encoder: Element =
gmake("flacenc", &[("quality", &compression.to_string())])?;
dest_elems.push(encoder);
}
@@ -414,20 +432,24 @@ async fn transcode_gstreamer(
bitrate,
bitrate_type,
} => {
let encoder: Element = gmake("lamemp3enc")?;
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(
("target", &"1"),
("bitrate", &i32::from(*bitrate)),
(
"cbr",
match bitrate_type {
config::BitrateType::Vbr => &false,
config::BitrateType::Cbr => &true,
},
),
],
)?;
dest_elems.push(encoder);
dest_elems.push(gmake("id3v2mux")?);
dest_elems.push(gmake("id3v2mux", &[])?);
}
Transcode::Copy => {
@@ -436,8 +458,8 @@ async fn transcode_gstreamer(
}
};
let file_dest: gstreamer_base::BaseSink = gmake("filesink")?;
file_dest.set_property("location", &path_to_gstring(&to_path_clone))?;
let file_dest: gstreamer_base::BaseSink =
gmake("filesink", &[("location", &to_path_clone)])?;
file_dest.set_sync(false);
dest_elems.push(file_dest.upcast());
@@ -452,7 +474,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 +486,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 +495,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 +519,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().map(|gstring| gstring.into()),
source: err.error(),
}
.into()
});
@@ -560,7 +580,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 +589,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)
})
});
@@ -639,10 +659,3 @@ fn path_to_bytes(path: &Path) -> Cow<'_, [u8]> {
Cow::Owned(buf)
}
}
fn path_to_gstring(path: &Path) -> GString {
let buf = path_to_bytes(path);
ffi::CString::new(buf)
.expect("Path contained null byte")
.into()
}

39
src/tag.rs Normal file
View File

@@ -0,0 +1,39 @@
use glib::{GStr, Value, gstr};
use gstreamer::{
Tag, TagFlag,
tags::{CustomTag, merge_strings_with_comma},
};
pub struct MbArtistId;
impl<'a> Tag<'a> for MbArtistId {
type TagType = &'a str;
const TAG_NAME: &'static GStr = gstr!("musicbrainz-artistid");
}
impl CustomTag<'_> for MbArtistId {
const FLAG: TagFlag = TagFlag::Meta;
const NICK: &'static GStr = gstr!("artist ID");
const DESCRIPTION: &'static GStr = gstr!("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;
const TAG_NAME: &'static GStr = gstr!("musicbrainz-albumartistid");
}
impl CustomTag<'_> for MbAlbumArtistId {
const FLAG: TagFlag = TagFlag::Meta;
const NICK: &'static GStr = gstr!("album artist ID");
const DESCRIPTION: &'static GStr = gstr!("MusicBrainz album artist ID");
fn merge_func(src: &Value) -> Value {
merge_strings_with_comma(src)
}
}

View File

@@ -6,7 +6,7 @@ use std::{
time::Duration,
};
use tokio::{task, time::interval};
use tui::{backend::CrosstermBackend, Terminal};
use tui::{Terminal, backend::CrosstermBackend};
pub const UPDATE_INTERVAL_MILLIS: u64 = 100;
@@ -92,7 +92,7 @@ impl State {
self.ended_tasks += 1;
}
Msg::TaskProgress { id, ratio } => {
let mut task = self
let task = self
.running_tasks
.get_mut(&id)
.context("Unable to update task progress; could't find task")?;