From 12b0b687aed20c0bd9a2106f0e1de4e498f808f6 Mon Sep 17 00:00:00 2001 From: chaos Date: Mon, 30 Oct 2023 22:49:09 +0000 Subject: [PATCH] move ffprobe stuff to its own crate/module --- Cargo.lock | 9 +++++++++ Cargo.toml | 3 +-- flake.nix | 7 +++++++ modules/ascii_reduce/src/lib.rs | 8 ++++++++ modules/ffprobe/Cargo.toml | 8 ++++++++ {src/utils/ffprobe => modules/ffprobe/src}/errors.rs | 0 .../ffprobe => modules/ffprobe/src}/ffprobe_output.rs | 0 src/utils/ffprobe/mod.rs => modules/ffprobe/src/lib.rs | 10 +++++----- modules/ffprobe/src/meta.rs | 0 {src/utils/ffprobe => modules/ffprobe/src}/types.rs | 0 src/utils/formats/handlers/ffprobe.rs | 9 +++------ src/utils/mod.rs | 1 - 12 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 modules/ffprobe/Cargo.toml rename {src/utils/ffprobe => modules/ffprobe/src}/errors.rs (100%) rename {src/utils/ffprobe => modules/ffprobe/src}/ffprobe_output.rs (100%) rename src/utils/ffprobe/mod.rs => modules/ffprobe/src/lib.rs (77%) create mode 100644 modules/ffprobe/src/meta.rs rename {src/utils/ffprobe => modules/ffprobe/src}/types.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 989ce93..a8f0cc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -366,6 +366,14 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "ffprobe" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "filetime" version = "0.2.22" @@ -668,6 +676,7 @@ dependencies = [ "ascii_reduce", "bytes", "clap", + "ffprobe", "html-escape", "id3", "infer", diff --git a/Cargo.toml b/Cargo.toml index 9179f7b..93dba9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,6 @@ clap = { version = "4", features = ["derive"] } # useful for when storing music files on mp3 players with broken unicode support ascii_reduce = { path = "./modules/ascii_reduce" } - - # transcode presets & format handlers lazy_static = "1" @@ -41,6 +39,7 @@ bytes = "1" id3 = { version = "1", optional = true } metaflac = { version = "0.2", optional = true } taglib = { path = "./modules/taglib", optional = true } +ffprobe = { path = "./modules/ffprobe" } # for genhtml command html-escape = { version = "0.2", optional = true } diff --git a/flake.nix b/flake.nix index 3e24cc4..4d9425c 100644 --- a/flake.nix +++ b/flake.nix @@ -42,6 +42,13 @@ " ''; + postFixup = '' + wrapProgram $out/bin/some-script \ + --set PATH ${lib.makeBinPath [ + pkgs.ffmpeg + ]} + ''; + postPatch = '' substituteInPlace src/meta.rs --replace 'ffmpeg' '${pkgs.ffmpeg}/bin/ffmpeg' substituteInPlace src/meta.rs --replace 'ffprobe' '${pkgs.ffmpeg}/bin/ffprobe' diff --git a/modules/ascii_reduce/src/lib.rs b/modules/ascii_reduce/src/lib.rs index 9ba1bdb..7c114bb 100644 --- a/modules/ascii_reduce/src/lib.rs +++ b/modules/ascii_reduce/src/lib.rs @@ -1,5 +1,13 @@ include!(concat!(env!("OUT_DIR"), "/codegen.rs")); +#[cfg(test)] +mod tests { + #[test] + fn test() { + assert_eq!(crate::reduce("öwo owö 😊".to_string()), "owo owo "); + } +} + pub fn reduce(input: String) -> String { if input.is_ascii() { return input; diff --git a/modules/ffprobe/Cargo.toml b/modules/ffprobe/Cargo.toml new file mode 100644 index 0000000..f7e6923 --- /dev/null +++ b/modules/ffprobe/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ffprobe" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/utils/ffprobe/errors.rs b/modules/ffprobe/src/errors.rs similarity index 100% rename from src/utils/ffprobe/errors.rs rename to modules/ffprobe/src/errors.rs diff --git a/src/utils/ffprobe/ffprobe_output.rs b/modules/ffprobe/src/ffprobe_output.rs similarity index 100% rename from src/utils/ffprobe/ffprobe_output.rs rename to modules/ffprobe/src/ffprobe_output.rs diff --git a/src/utils/ffprobe/mod.rs b/modules/ffprobe/src/lib.rs similarity index 77% rename from src/utils/ffprobe/mod.rs rename to modules/ffprobe/src/lib.rs index 94b0d1a..64f38b7 100644 --- a/src/utils/ffprobe/mod.rs +++ b/modules/ffprobe/src/lib.rs @@ -1,13 +1,13 @@ pub mod errors; -mod ffprobe_output; pub mod types; +mod ffprobe_output; use std::{convert::Into, path::Path, process::Command}; use self::errors::{AnalyzeError, FFProbeError}; -fn extract(path: &Path) -> Result { - let output = Command::new(crate::meta::FFPROBE) +fn extract(path: &Path, ffprobe_command: Option<&str>) -> Result { + let output = Command::new(ffprobe_command.unwrap_or("ffprobe")) .args([ "-v", "quiet", @@ -39,8 +39,8 @@ fn extract(path: &Path) -> Result { Ok(ffprobe_out.unwrap()) } -pub fn analyze(path: &Path) -> Result { - let raw_data = extract(path)?; +pub fn analyze(path: &Path, ffprobe_command: Option<&str>) -> Result { + let raw_data = extract(path, ffprobe_command)?; let mut data = types::FFProbeData { tags: raw_data.format.tags.into(), diff --git a/modules/ffprobe/src/meta.rs b/modules/ffprobe/src/meta.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/ffprobe/types.rs b/modules/ffprobe/src/types.rs similarity index 100% rename from src/utils/ffprobe/types.rs rename to modules/ffprobe/src/types.rs diff --git a/src/utils/formats/handlers/ffprobe.rs b/src/utils/formats/handlers/ffprobe.rs index 2632933..5fc6e2e 100644 --- a/src/utils/formats/handlers/ffprobe.rs +++ b/src/utils/formats/handlers/ffprobe.rs @@ -7,11 +7,8 @@ use string_error::into_err; use crate::{ types::{AudioFileInfo, ReplayGainData, ReplayGainRawData, Tags}, - utils::formats::{AudioFormatError, BoxedError, FormatHandler}, - utils::{ - ffprobe, - format_detection::{detect_format, FileFormat}, - }, + utils::format_detection::{detect_format, FileFormat}, + utils::formats::{AudioFormatError, BoxedError, FormatHandler}, meta, }; #[derive(Default)] @@ -36,7 +33,7 @@ pub struct GenericFFMpegAudioFormat { impl GenericFFMpegAudioFormat { fn analyze(&mut self) -> Result<(), BoxedError> { - let output = ffprobe::analyze(&self.path); + let output = ffprobe::analyze(&self.path, Some(meta::FFPROBE)); if let Err(err) = output { return Err(into_err(format!("{}", err))); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 13dfa0e..e715016 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,3 @@ -pub mod ffprobe; pub mod format_detection; #[cfg(feature = "replaygain")] pub mod replaygain;