tsk-50: Added properties
Some checks failed
Release Tagging / release (pull_request) Successful in 31s
Rust Build / Check (pull_request) Successful in 32s
Rust Build / Test Suite (pull_request) Failing after 34s
Rust Build / Clippy (pull_request) Failing after 31s
Rust Build / Rustfmt (pull_request) Failing after 37s
Rust Build / build (pull_request) Successful in 30s

This commit is contained in:
2025-11-06 11:47:10 -05:00
parent ff1c7c9a59
commit 6df220ad73
2 changed files with 43 additions and 6 deletions

View File

@@ -1,13 +1,12 @@
use lofty::file::AudioFile;
pub fn get_duration(song_path: &String) -> Result<std::time::Duration, std::io::Error> {
match std::fs::File::open(song_path) {
pub fn get_properties(songpath: &str) -> Result<lofty::flac::FlacProperties, std::io::Error> {
match std::fs::File::open(songpath) {
Ok(mut content) => {
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
{
Ok(flac_file) => {
let properties = flac_file.properties();
Ok(properties.duration())
Ok(*flac_file.properties())
}
Err(err) => Err(std::io::Error::other(err.to_string())),
}

View File

@@ -1,4 +1,42 @@
pub mod properties;
pub mod audio;
#[derive(Clone, Debug, Default)]
pub struct SongProperties {
pub duration: std::time::Duration,
pub sample_rate: u32,
pub bitrate: u32,
pub overall_bitrate: u32,
pub bit_depth: u8,
pub channels: u8,
}
pub fn get_song_properties(song_path: &str) -> Result<SongProperties, std::io::Error> {
match audio::get_properties(song_path) {
Ok(flac_properties) => {
Ok(SongProperties {
duration: flac_properties.duration(),
sample_rate: flac_properties.sample_rate(),
bitrate: flac_properties.audio_bitrate(),
overall_bitrate: flac_properties.overall_bitrate(),
bit_depth: flac_properties.bit_depth(),
channels: flac_properties.channels(),
})
}
Err(err) => Err(err),
}
}
pub fn get_duration(song_path: &String) -> Result<std::time::Duration, std::io::Error> {
match get_song_properties(song_path) {
Ok(song_properties) => {
Ok(song_properties.duration)
}
Err(err) => Err(err),
}
}
#[cfg(test)]
mod tests {
@@ -12,7 +50,7 @@ mod tests {
match test_util::util::file_exists(&dir, &filename) {
Ok(_) => {
let filepath = test_util::util::get_full_path(&dir, &filename).unwrap();
match super::properties::get_duration(&filepath) {
match super::get_duration(&filepath) {
Ok(duration) => {
let song_duration: u64 = 41;
let fetched_song_duration = duration.as_secs();