tsk-50: Create functionality to extract song properties #52

Merged
phoenix merged 8 commits from tsk-50 into main 2025-11-06 17:31:37 +00:00
2 changed files with 43 additions and 6 deletions
Showing only changes of commit 6df220ad73 - Show all commits

View File

@@ -1,13 +1,12 @@
use lofty::file::AudioFile; use lofty::file::AudioFile;
pub fn get_duration(song_path: &String) -> Result<std::time::Duration, std::io::Error> { pub fn get_properties(songpath: &str) -> Result<lofty::flac::FlacProperties, std::io::Error> {
match std::fs::File::open(song_path) { match std::fs::File::open(songpath) {
Ok(mut content) => { Ok(mut content) => {
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new()) match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
{ {
Ok(flac_file) => { Ok(flac_file) => {
let properties = flac_file.properties(); Ok(*flac_file.properties())
Ok(properties.duration())
} }
Err(err) => Err(std::io::Error::other(err.to_string())), 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)] #[cfg(test)]
mod tests { mod tests {
@@ -12,7 +50,7 @@ mod tests {
match test_util::util::file_exists(&dir, &filename) { match test_util::util::file_exists(&dir, &filename) {
Ok(_) => { Ok(_) => {
let filepath = test_util::util::get_full_path(&dir, &filename).unwrap(); let filepath = test_util::util::get_full_path(&dir, &filename).unwrap();
match super::properties::get_duration(&filepath) { match super::get_duration(&filepath) {
Ok(duration) => { Ok(duration) => {
let song_duration: u64 = 41; let song_duration: u64 = 41;
let fetched_song_duration = duration.as_secs(); let fetched_song_duration = duration.as_secs();