-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use id3::TagLike; | ||
use metaflac; | ||
|
||
use crate::{error::MetadataError, thumbnails::generate_thumbnail}; | ||
|
||
pub struct AudioMetadata { | ||
pub artist: Option<String>, | ||
pub title: Option<String>, | ||
pub album: Option<String>, | ||
pub duration: u32, | ||
pub disc: Option<u32>, | ||
pub position: Option<u32>, | ||
pub year: Option<u32>, | ||
pub thumbnail: Option<String>, | ||
} | ||
|
||
impl AudioMetadata { | ||
pub fn new() -> Self { | ||
Self { | ||
artist: None, | ||
title: None, | ||
album: None, | ||
duration: 0, | ||
disc: None, | ||
position: None, | ||
year: None, | ||
thumbnail: None, | ||
} | ||
} | ||
} | ||
|
||
pub trait MetadataExtractor { | ||
fn extract_metadata( | ||
&self, | ||
path: &str, | ||
thumbnails_dir: &str, | ||
) -> Result<AudioMetadata, MetadataError>; | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
|
||
pub struct Mp3MetadataExtractor; | ||
impl MetadataExtractor for Mp3MetadataExtractor { | ||
fn extract_metadata( | ||
&self, | ||
path: &str, | ||
thumbnails_dir: &str, | ||
) -> Result<AudioMetadata, MetadataError> { | ||
let tag = id3::Tag::read_from_path(path).unwrap(); | ||
let mut metadata = AudioMetadata::new(); | ||
|
||
metadata.artist = tag.artist().map(|s| s.to_string()); | ||
metadata.title = tag.title().map(|s| s.to_string()); | ||
metadata.album = tag.album().map(|s| s.to_string()); | ||
metadata.duration = tag.duration().unwrap_or(0); | ||
metadata.position = tag.track(); | ||
metadata.disc = tag.disc(); | ||
metadata.year = tag.year().map(|s| s as u32); | ||
metadata.thumbnail = generate_thumbnail(&path, thumbnails_dir); | ||
|
||
Ok(metadata) | ||
} | ||
} | ||
|
||
pub struct FlacMetadataExtractor; | ||
impl FlacMetadataExtractor { | ||
fn extract_string_metadata( | ||
tag: &metaflac::Tag, | ||
key: &str, | ||
fallback_key: Option<&str>, | ||
) -> Option<String> { | ||
tag.get_vorbis(key) | ||
.and_then(|mut iter| iter.next()) | ||
.map(|s| s.to_string()) | ||
.or_else(|| { | ||
fallback_key.and_then(|key| { | ||
tag.get_vorbis(key) | ||
.and_then(|mut iter| iter.next()) | ||
.map(|s| s.to_string()) | ||
}) | ||
}) | ||
} | ||
|
||
fn extract_numeric_metadata<T: std::str::FromStr>(tag: &metaflac::Tag, key: &str) -> Option<T> { | ||
tag.get_vorbis(key) | ||
.and_then(|mut iter| iter.next()) | ||
.and_then(|s| s.parse::<T>().ok()) | ||
} | ||
} | ||
|
||
impl MetadataExtractor for FlacMetadataExtractor { | ||
fn extract_metadata( | ||
&self, | ||
path: &str, | ||
thumbnails_dir: &str, | ||
) -> Result<AudioMetadata, MetadataError> { | ||
// Extract metadata from a FLAC file. | ||
let tag = metaflac::Tag::read_from_path(path).unwrap(); | ||
let mut metadata = AudioMetadata::new(); | ||
metadata.artist = Self::extract_string_metadata(&tag, "ARTIST", Some("ALBUMARTIST")); | ||
metadata.title = Self::extract_string_metadata(&tag, "TITLE", None); | ||
metadata.album = Self::extract_string_metadata(&tag, "ALBUM", None); | ||
metadata.duration = Self::extract_numeric_metadata(&tag, "LENGTH").unwrap_or(0); | ||
metadata.position = Self::extract_numeric_metadata(&tag, "TRACKNUMBER"); | ||
metadata.disc = Self::extract_numeric_metadata(&tag, "DISCNUMBER"); | ||
metadata.year = Self::extract_numeric_metadata(&tag, "DATE"); | ||
let thumbnail_content = tag.pictures().next().map(|p| p.data.clone()).unwrap(); | ||
|
||
//TODO: add thumbnail generation | ||
|
||
Ok(metadata) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters