Create a list of audio files with Rust
The program creates a list of audio files in the current folder including subfolders, case-insensitive.
Supported audiofiles: mp3, flac, wav, ogg, m4a, mp2, mka.
Cargo.toml
[package]
name = "my_audio_finder"
version = "0.1.0"
edition = "2021"
[dependencies]
glob = "0.3"
unicode-segmentation = "1.10"
regex = "1.9"
main.rs
use my_audio_finder::audio_finder::find_audio_files;
fn main() -> Result<(), Box<dyn std::error::Error>> {
find_audio_files()?;
Ok(())
}
lib.rs
pub mod audio_finder {
use std::error::Error;
use glob::glob;
use unicode_segmentation::UnicodeSegmentation;
use regex::Regex;
pub fn find_audio_files() -> Result<(), Box<dyn Error>> {
let pattern = "**/*";
let audio_regex = Regex::new(r"(?i)\.(mp3|flac|wav|ogg|m4a|mp2|mka)$")?;
for entry in glob(pattern)? {
match entry {
Ok(path) => {
let path_str = path.to_str().unwrap_or("");
let decoded_path = UnicodeSegmentation::graphemes(path_str, true).collect::<String>();
if audio_regex.is_match(&decoded_path) && path.is_file() {
println!("{}", decoded_path);
}
}
Err(e) => eprintln!("Error: {:?}", e),
}
}
Ok(())
}
}