From 88318e5b4ffecf88159acb24d995c3d405035a8c Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Wed, 16 Nov 2016 17:27:53 -0800 Subject: [PATCH] Added support for Flac metadata --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + src/error.rs | 7 +++++++ src/main.rs | 1 + src/metadata.rs | 22 ++++++++++++++++++++-- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6f9548..15f7423 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,6 +10,7 @@ dependencies = [ "iron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (git+https://github.com/retep998/winapi-rs)", "lewton 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "metaflac 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "mount 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ogg 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "oven 1.0.0 (git+https://github.com/agersant/oven?branch=remove_cookie_dep)", @@ -454,6 +455,17 @@ dependencies = [ "libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "metaflac" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.2.2" @@ -1131,6 +1143,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" "checksum matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "15305656809ce5a4805b1ff2946892810992197ce1270ff79baded852187942e" "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" +"checksum metaflac 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "66d5865b6103db7bccad008e6d50195236116a44ef67879645bcbc1ce1e9c58b" "checksum mime 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b5c93a4bd787ddc6e7833c519b73a50883deb5863d76d9b71eb8216fb7f94e66" "checksum mime_guess 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a7d89cb3bce9145b0d0339a0588b044e3e3e3faa1dcd74822ebdc36bfac020" "checksum miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d1f4d337a01c32e1f2122510fed46393d53ca35a7f429cb0450abaedfa3ed54" diff --git a/Cargo.toml b/Cargo.toml index 648e060..432b878 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ id3 = { git = "https://github.com/jameshurst/rust-id3" } image = "0.10.3" iron = "0.4.0" lewton = "0.4.0" +metaflac = "0.1.4" mount = "0.2.1" ogg = "0.4.0" oven = { git = "https://github.com/agersant/oven", branch = "remove_cookie_dep" } diff --git a/src/error.rs b/src/error.rs index 93c35e9..84b399e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,7 @@ use std::io; use id3; use image; use lewton; +use metaflac; #[derive(Debug)] pub enum PError { @@ -58,6 +59,12 @@ impl From for PError { } } +impl From for PError { + fn from(_: metaflac::Error) -> PError { + PError::MetadataDecodingError + } +} + impl error::Error for PError { fn description(&self) -> &str { match *self { diff --git a/src/main.rs b/src/main.rs index e5f2f24..f8125ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate id3; extern crate image; extern crate iron; extern crate lewton; +extern crate metaflac; extern crate mount; extern crate ogg; extern crate oven; diff --git a/src/metadata.rs b/src/metadata.rs index 1759b5d..5171500 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -1,6 +1,7 @@ use ape; -use id3::Tag; +use id3; use lewton::inside_ogg::OggStreamReader; +use metaflac; use ogg::PacketReader; use regex::Regex; use std::fs; @@ -23,6 +24,7 @@ pub struct SongTags { impl SongTags { pub fn read(path: &Path) -> Result { match utils::get_audio_format(path) { + Some(AudioFormat::FLAC) => SongTags::read_flac(path), Some(AudioFormat::MP3) => SongTags::read_id3(path), Some(AudioFormat::MPC) => SongTags::read_ape(path), Some(AudioFormat::OGG) => SongTags::read_vorbis(path), @@ -31,7 +33,7 @@ impl SongTags { } fn read_id3(path: &Path) -> Result { - let tag = try!(Tag::read_from_path(path)); + let tag = try!(id3::Tag::read_from_path(path)); let artist = tag.artist().map(|s| s.to_string()); let album_artist = tag.album_artist().map(|s| s.to_string()); @@ -133,4 +135,20 @@ impl SongTags { Ok(tags) } + + fn read_flac(path: &Path) -> Result { + let tag = try!(metaflac::Tag::read_from_path(path)); + let vorbis = try!(tag.vorbis_comments().ok_or(PError::MetadataDecodingError)); + let disc_number = vorbis.get("DISCNUMBER").and_then(|d| d[0].parse::().ok()); + let year = vorbis.get("DATE").and_then(|d| d[0].parse::().ok()); + Ok(SongTags { + artist: vorbis.artist().map(|v| v[0].clone()), + album_artist: vorbis.album_artist().map(|v| v[0].clone()), + album: vorbis.album().map(|v| v[0].clone()), + title: vorbis.title().map(|v| v[0].clone()), + disc_number: disc_number, + track_number: vorbis.track(), + year: year, + }) + } } \ No newline at end of file