This commit is contained in:
Tianyi 2020-10-25 21:39:38 +00:00
parent 5dffc0ee55
commit 8d4d3fa4a5
4 changed files with 489 additions and 121 deletions

1
.gitignore vendored
View file

@ -11,5 +11,6 @@ Cargo.lock
*.mp3
*.m4a
*.flac
src/main.rs
.lib1.rs

View file

@ -1,6 +1,6 @@
[package]
name = "audiotags"
version = "0.0.2"
version = "0.1.0"
authors = ["Tianyi <ShiTianyi2001@outlook.com>"]
edition = "2018"
description = "Unified IO for different types of audio metadata"
@ -13,3 +13,4 @@ repository = "https://github.com/TianyiShi2001/audiotags"
id3 = "0.5.1"
mp4ameta = "0.5.1"
strum = {version = "0.19.5", features = ["derive"]}
metaflac = "0.2"

123
README.md
View file

@ -7,7 +7,7 @@
This crate makes it easier to parse tags/metadata in audio files of different file types.
This crate aims to provide a unified trait for parsers and writers of different audio file formats. This means that you can parse tags in mp3 and m4a files with a single function: `audiotags::from_path()` and get fields by directly calling `.album()`, `.artist()` on its result. Without this crate, you would otherwise need to learn different APIs in **id3**, **mp4ameta** crates in order to parse metadata in different file foramts.
This crate aims to provide a unified trait for parsers and writers of different audio file formats. This means that you can parse tags in mp3 and m4a files with a single function: `audiotags::from_path()` and get fields by directly calling `.album()`, `.artist()` on its result. Without this crate, you would otherwise need to learn different APIs in **id3**, **mp4ameta** etc. in order to parse metadata in different file formats.
## Example
@ -15,11 +15,10 @@ This crate aims to provide a unified trait for parsers and writers of different
```rust
use audiotags;
fn main() {
const MP3: &'static str = "お願い!シンデレラ.mp3";
let mut tags = audiotags::from_path(MP3).unwrap();
// without this crate you would call id3::Tag::from_path()
const MP3: &'static str = "a.mp3";
let mut tags = audiotags::read_from_path(MP3).unwrap();
// without this crate you would call id3::Tag::read_from_path()
println!("Title: {:?}", tags.title());
println!("Artist: {:?}", tags.artist());
tags.set_album_artist("CINDERELLA PROJECT");
@ -27,14 +26,13 @@ fn main() {
println!("Album title and artist: {:?}", (album.title, album.artist));
println!("Track: {:?}", tags.track());
tags.write_to_path(MP3).unwrap();
// Title: Some("お願い!シンデレラ")
// Artist: Some("高垣楓、城ヶ崎美嘉、小日向美穂、十時愛梨、川島瑞樹、日野茜、輿水幸子、佐久間まゆ、白坂小梅")
// Album title and artist: ("THE IDOLM@STER CINDERELLA GIRLS ANIMATION PROJECT 01 Star!!", Some("CINDERELLA PROJECT"))
// Track: (Some(2), Some(4))
const M4A: &'static str = "ふわふわ時間.m4a";
let mut tags = audiotags::from_path(M4A).unwrap();
// without this crate you would call mp4ameta::Tag::from_path()
// Title: Some("お願い!シンデレラ")
// Artist: Some("高垣楓、城ヶ崎美嘉、小日向美穂、十時愛梨、川島瑞樹、日野茜、輿水幸子、佐久間まゆ、白坂小梅")
// Album title and artist: ("THE IDOLM@STER CINDERELLA GIRLS ANIMATION PROJECT 01 Star!!", Some("CINDERELLA PROJECT"))
// Track: (Some(2), Some(4))
const M4A: &'static str = "b.m4a";
let mut tags = audiotags::read_from_path(M4A).unwrap();
// without this crate you would call mp4ameta::Tag::read_from_path()
println!("Title: {:?}", tags.title());
println!("Artist: {:?}", tags.artist());
let album = tags.album().unwrap();
@ -42,39 +40,118 @@ fn main() {
tags.set_total_tracks(4);
println!("Track: {:?}", tags.track());
tags.write_to_path(M4A).unwrap();
// Title: Some("ふわふわ時間")
// Artist: Some("桜高軽音部 [平沢唯・秋山澪・田井中律・琴吹紬(CV:豊崎愛生、日笠陽子、佐藤聡美、寿美菜子)]")
// Album title and artist: ("ふわふわ時間", Some("桜高軽音部 [平沢唯・秋山澪・田井中律・琴吹紬(CV:豊崎愛生、日笠陽子、佐藤聡美、寿美菜子)]"))
// Track: (Some(1), Some(4))
// Title: Some("ふわふわ時間")
// Artist: Some("桜高軽音部 [平沢唯・秋山澪・田井中律・琴吹紬(CV:豊崎愛生、日笠陽子、佐藤聡美、寿美菜子)]")
// Album title and artist: ("ふわふわ時間", Some("桜高軽音部 [平沢唯・秋山澪・田井中律・琴吹紬(CV:豊崎愛生、日笠陽子、佐藤聡美、寿美菜子)]"))
// Track: (Some(1), Some(4))
const FLAC: &'static str = "c.flac";
let mut tags = audiotags::read_from_path(FLAC).unwrap();
// without this crate you would call metaflac::Tag::read_from_path()
println!("Title: {:?}", tags.title());
println!("Artist: {:?}", tags.artist());
let album = tags.album().unwrap();
println!("Album title and artist: {:?}", (album.title, album.artist));
tags.set_year(2017);
println!("Year: {:?}", tags.year());
tags.write_to_path(FLAC).unwrap();
// Title: Some("意味/無/ジュニーク・ニコール")
// Artist: Some("岡部啓一")
// Album title and artist: ("NieR:Automata Original Soundtrack", Some("SQUARE ENIX"))
// Year: Some(2017)
}
```
## Supported Formats
| File Fomat | Metadata Format | backend |
| ------------- | --------------------- | ----------------------------------------------------------- |
| `mp3` | id3v2.4 | [**id3**](https://github.com/polyfloyd/rust-id3) |
| `m4a/mp4/...` | MPEG-4 audio metadata | [**mp4ameta**](https://github.com/Saecki/rust-mp4ameta) |
| `flac` | Vorbis comment | [**metaflac**](https://github.com/jameshurst/rust-metaflac) |
## Supported Methods
```rust
pub trait AudioTagsIo {
fn title(&self) -> Option<&str>;
fn set_title(&mut self, title: &str);
fn remove_title(&mut self);
fn artist(&self) -> Option<&str>;
fn remove_artist(&mut self);
fn set_artist(&mut self, artist: &str);
fn year(&self) -> Option<i32>;
fn set_year(&mut self, year: i32);
fn album(&self) -> Option<Album>;
fn remove_year(&mut self);
fn album(&self) -> Option<Album> {
self.album_title().map(|title| Album {
title: title.to_owned(),
artist: self.album_artist().map(|x| x.to_owned()),
cover: self.album_cover(),
})
}
fn remove_album(&mut self) {
self.remove_album_title();
self.remove_album_artist();
self.remove_album_cover();
}
fn album_title(&self) -> Option<&str>;
fn remove_album_title(&mut self);
fn album_artist(&self) -> Option<&str>;
fn remove_album_artist(&mut self);
fn album_cover(&self) -> Option<Picture>;
fn set_album(&mut self, album: Album);
fn remove_album_cover(&mut self);
fn set_album(&mut self, album: Album) {
self.set_album_title(&album.title);
if let Some(artist) = album.artist {
self.set_album_artist(&artist)
} else {
self.remove_album_artist()
}
if let Some(pic) = album.cover {
self.set_album_cover(pic)
} else {
self.remove_album_cover()
}
}
fn set_album_title(&mut self, v: &str);
fn set_album_artist(&mut self, v: &str);
fn set_album_cover(&mut self, cover: Picture);
fn track(&self) -> (Option<u16>, Option<u16>);
fn track(&self) -> (Option<u16>, Option<u16>) {
(self.track_number(), self.total_tracks())
}
fn set_track(&mut self, track: (u16, u16)) {
self.set_track_number(track.0);
self.set_total_tracks(track.1);
}
fn remove_track(&mut self) {
self.remove_track_number();
self.remove_total_tracks();
}
fn track_number(&self) -> Option<u16>;
fn set_track_number(&mut self, track_number: u16);
fn remove_track_number(&mut self);
fn total_tracks(&self) -> Option<u16>;
fn set_total_tracks(&mut self, total_track: u16);
fn disc(&self) -> (Option<u16>, Option<u16>);
fn remove_total_tracks(&mut self);
fn disc(&self) -> (Option<u16>, Option<u16>) {
(self.disc_number(), self.total_discs())
}
fn set_disc(&mut self, disc: (u16, u16)) {
self.set_disc_number(disc.0);
self.set_total_discs(disc.1);
}
fn remove_disc(&mut self) {
self.remove_disc_number();
self.remove_total_discs();
}
fn disc_number(&self) -> Option<u16>;
fn set_disc_number(&mut self, disc_number: u16);
fn remove_disc_number(&mut self);
fn total_discs(&self) -> Option<u16>;
fn set_total_discs(&mut self, total_discs: u16);
fn write_to(&self, file: &File) -> Result<(), BoxedError>;
fn remove_total_discs(&mut self);
fn write_to(&mut self, file: &mut File) -> Result<(), BoxedError>;
// cannot use impl AsRef<Path>
fn write_to_path(&self, path: &str) -> Result<(), BoxedError>;
fn write_to_path(&mut self, path: &str) -> Result<(), BoxedError>;
}
```

View file

@ -1,6 +1,6 @@
//! This crate makes it easier to parse tags/metadata in audio files of different file types.
//!
//! This crate aims to provide a unified trait for parsers and writers of different audio file formats. This means that you can parse tags in mp3 and m4a files with a single function: `audiotags::from_path()` and get fields by directly calling `.album()`, `.artist()` on its result. Without this crate, you would otherwise need to learn different APIs in **id3**, **mp4ameta** crates in order to parse metadata in different file foramts.
//! This crate aims to provide a unified trait for parsers and writers of different audio file formats. This means that you can parse tags in mp3 and m4a files with a single function: `audiotags::read_from_path()` and get fields by directly calling `.album()`, `.artist()` on its result. Without this crate, you would otherwise need to learn different APIs in **id3**, **mp4ameta** crates in order to parse metadata in different file foramts.
//!
//! ## Example
//!
@ -9,8 +9,8 @@
//!
//! fn main() {
//! const MP3: &'static str = "a.mp3";
//! let mut tags = audiotags::from_path(MP3).unwrap();
//! // without this crate you would call id3::Tag::from_path()
//! let mut tags = audiotags::read_from_path(MP3).unwrap();
//! // without this crate you would call id3::Tag::read_from_path()
//! println!("Title: {:?}", tags.title());
//! println!("Artist: {:?}", tags.artist());
//! tags.set_album_artist("CINDERELLA PROJECT");
@ -24,8 +24,8 @@
//! // Track: (Some(2), Some(4))
//!
//! const M4A: &'static str = "b.m4a";
//! let mut tags = audiotags::from_path(M4A).unwrap();
//! // without this crate you would call mp4ameta::Tag::from_path()
//! let mut tags = audiotags::read_from_path(M4A).unwrap();
//! // without this crate you would call mp4ameta::Tag::read_from_path()
//! println!("Title: {:?}", tags.title());
//! println!("Artist: {:?}", tags.artist());
//! let album = tags.album().unwrap();
@ -41,7 +41,10 @@
//! ```
use id3;
use metaflac;
use mp4ameta;
use std::collections::HashMap;
use std::convert::From;
use std::fs::File;
use std::path::Path;
use strum::Display;
@ -56,7 +59,7 @@ pub enum Error {
impl std::error::Error for Error {}
/// Guesses the audio metadata handler from the file extension, and returns the `Box`ed IO handler.
pub fn from_path(path: impl AsRef<Path>) -> Result<Box<dyn AudioTagsIo>, BoxedError> {
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Box<dyn AudioTagsIo>, BoxedError> {
match path
.as_ref()
.extension()
@ -66,14 +69,17 @@ pub fn from_path(path: impl AsRef<Path>) -> Result<Box<dyn AudioTagsIo>, BoxedEr
.to_lowercase()
.as_str()
{
"mp3" => Ok(Box::new(Id3Tags::from_path(path)?)),
"m4a" | "m4b" | "m4p" | "m4v" | "isom" | "mp4" => Ok(Box::new(M4aTags::from_path(path)?)),
"mp3" => Ok(Box::new(Id3Tags::read_from_path(path)?)),
"m4a" | "m4b" | "m4p" | "m4v" | "isom" | "mp4" => {
Ok(Box::new(M4aTags::read_from_path(path)?))
}
"flac" => Ok(Box::new(FlacTags::read_from_path(path)?)),
p @ _ => Err(Box::new(Error::UnsupportedFormat(p.to_owned()))),
}
}
#[derive(Debug, Clone, Copy)]
pub enum PictureType {
pub enum MimeType {
Png,
Jpeg,
Tiff,
@ -81,23 +87,35 @@ pub enum PictureType {
Gif,
}
impl From<MimeType> for String {
fn from(mt: MimeType) -> Self {
match mt {
MimeType::Jpeg => "image/jpeg".to_owned(),
MimeType::Png => "image/png".to_owned(),
MimeType::Tiff => "image/tiff".to_owned(),
MimeType::Bmp => "image/bmp".to_owned(),
MimeType::Gif => "image/gif".to_owned(),
}
}
}
#[derive(Debug, Clone)]
pub struct Picture {
pub data: Vec<u8>,
pub picture_type: PictureType,
pub mime_type: MimeType,
}
impl Picture {
pub fn try_with_mime(data: Vec<u8>, mime: &str) -> Result<Self, ()> {
let picture_type = match mime {
"image/jpeg" => PictureType::Jpeg,
"image/png" => PictureType::Png,
"image/tiff" => PictureType::Tiff,
"image/bmp" => PictureType::Bmp,
"image/gif" => PictureType::Gif,
let mime_type = match mime {
"image/jpeg" => MimeType::Jpeg,
"image/png" => MimeType::Png,
"image/tiff" => MimeType::Tiff,
"image/bmp" => MimeType::Bmp,
"image/gif" => MimeType::Gif,
_ => return Err(()),
};
Ok(Self { data, picture_type })
Ok(Self { data, mime_type })
}
}
@ -114,27 +132,84 @@ pub struct Album {
pub trait AudioTagsIo {
fn title(&self) -> Option<&str>;
fn set_title(&mut self, title: &str);
fn remove_title(&mut self);
fn artist(&self) -> Option<&str>;
fn remove_artist(&mut self);
fn set_artist(&mut self, artist: &str);
fn year(&self) -> Option<i32>;
fn set_year(&mut self, year: i32);
fn album(&self) -> Option<Album>;
fn remove_year(&mut self);
fn album(&self) -> Option<Album> {
self.album_title().map(|title| Album {
title: title.to_owned(),
artist: self.album_artist().map(|x| x.to_owned()),
cover: self.album_cover(),
})
}
fn remove_album(&mut self) {
self.remove_album_title();
self.remove_album_artist();
self.remove_album_cover();
}
fn album_title(&self) -> Option<&str>;
fn remove_album_title(&mut self);
fn album_artist(&self) -> Option<&str>;
fn remove_album_artist(&mut self);
fn album_cover(&self) -> Option<Picture>;
fn set_album(&mut self, album: Album);
fn remove_album_cover(&mut self);
fn set_album(&mut self, album: Album) {
self.set_album_title(&album.title);
if let Some(artist) = album.artist {
self.set_album_artist(&artist)
} else {
self.remove_album_artist()
}
if let Some(pic) = album.cover {
self.set_album_cover(pic)
} else {
self.remove_album_cover()
}
}
fn set_album_title(&mut self, v: &str);
fn set_album_artist(&mut self, v: &str);
fn set_album_cover(&mut self, cover: Picture);
fn track(&self) -> (Option<u16>, Option<u16>);
fn track(&self) -> (Option<u16>, Option<u16>) {
(self.track_number(), self.total_tracks())
}
fn set_track(&mut self, track: (u16, u16)) {
self.set_track_number(track.0);
self.set_total_tracks(track.1);
}
fn remove_track(&mut self) {
self.remove_track_number();
self.remove_total_tracks();
}
fn track_number(&self) -> Option<u16>;
fn set_track_number(&mut self, track_number: u16);
fn remove_track_number(&mut self);
fn total_tracks(&self) -> Option<u16>;
fn set_total_tracks(&mut self, total_track: u16);
fn disc(&self) -> (Option<u16>, Option<u16>);
fn remove_total_tracks(&mut self);
fn disc(&self) -> (Option<u16>, Option<u16>) {
(self.disc_number(), self.total_discs())
}
fn set_disc(&mut self, disc: (u16, u16)) {
self.set_disc_number(disc.0);
self.set_total_discs(disc.1);
}
fn remove_disc(&mut self) {
self.remove_disc_number();
self.remove_total_discs();
}
fn disc_number(&self) -> Option<u16>;
fn set_disc_number(&mut self, disc_number: u16);
fn remove_disc_number(&mut self);
fn total_discs(&self) -> Option<u16>;
fn set_total_discs(&mut self, total_discs: u16);
fn write_to(&self, file: &File) -> Result<(), BoxedError>;
fn remove_total_discs(&mut self);
fn write_to(&mut self, file: &mut File) -> Result<(), BoxedError>;
// cannot use impl AsRef<Path>
fn write_to_path(&self, path: &str) -> Result<(), BoxedError>;
fn write_to_path(&mut self, path: &str) -> Result<(), BoxedError>;
}
pub struct Id3Tags {
@ -142,7 +217,7 @@ pub struct Id3Tags {
}
impl Id3Tags {
pub fn from_path(path: impl AsRef<Path>) -> Result<Self, BoxedError> {
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, BoxedError> {
Ok(Self {
inner: id3::Tag::read_from_path(path)?,
})
@ -156,24 +231,26 @@ impl AudioTagsIo for Id3Tags {
fn set_title(&mut self, title: &str) {
self.inner.set_title(title)
}
fn remove_title(&mut self) {
self.inner.remove_title();
}
fn artist(&self) -> Option<&str> {
self.inner.artist()
}
fn set_artist(&mut self, artist: &str) {
self.inner.set_title(artist)
}
fn remove_artist(&mut self) {
self.inner.remove_artist();
}
fn year(&self) -> Option<i32> {
self.inner.year()
}
fn set_year(&mut self, year: i32) {
self.inner.set_year(year)
}
fn album(&self) -> Option<Album> {
self.inner.album().map(|title| Album {
title: title.to_owned(),
artist: self.inner.album_artist().map(|x| x.to_owned()),
cover: self.album_cover(),
})
fn remove_year(&mut self) {
// self.inner.remove_year(); // TODO
}
fn album_title(&self) -> Option<&str> {
self.inner.album()
@ -194,20 +271,6 @@ impl AudioTagsIo for Id3Tags {
None
}
}
fn set_album(&mut self, album: Album) {
self.inner.set_album(album.title);
if let Some(artist) = album.artist {
self.inner.set_album_artist(artist)
} else {
self.inner.remove_album_artist()
}
if let Some(pic) = album.cover {
self.set_album_cover(pic)
} else {
self.inner
.remove_picture_by_type(id3::frame::PictureType::CoverFront);
}
}
fn set_album_title(&mut self, v: &str) {
self.inner.set_album(v)
}
@ -215,34 +278,33 @@ impl AudioTagsIo for Id3Tags {
self.inner.set_album_artist(v)
}
fn set_album_cover(&mut self, cover: Picture) {
self.inner
.remove_picture_by_type(id3::frame::PictureType::CoverFront);
self.inner.add_picture(match cover.picture_type {
PictureType::Jpeg => id3::frame::Picture {
self.remove_album_cover();
self.inner.add_picture(match cover.mime_type {
MimeType::Jpeg => id3::frame::Picture {
mime_type: "jpeg".to_owned(),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
data: cover.data,
},
PictureType::Png => id3::frame::Picture {
MimeType::Png => id3::frame::Picture {
mime_type: "png".to_owned(),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
data: cover.data,
},
PictureType::Tiff => id3::frame::Picture {
MimeType::Tiff => id3::frame::Picture {
mime_type: "tiff".to_owned(),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
data: cover.data,
},
PictureType::Bmp => id3::frame::Picture {
MimeType::Bmp => id3::frame::Picture {
mime_type: "bmp".to_owned(),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
data: cover.data,
},
PictureType::Gif => id3::frame::Picture {
MimeType::Gif => id3::frame::Picture {
mime_type: "gif".to_owned(),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
@ -250,35 +312,57 @@ impl AudioTagsIo for Id3Tags {
},
});
}
fn track(&self) -> (Option<u16>, Option<u16>) {
(
self.inner.track().map(|x| x as u16),
self.inner.total_tracks().map(|x| x as u16),
)
fn remove_album_title(&mut self) {
self.inner.remove_album();
}
fn remove_album_artist(&mut self) {
self.inner.remove_album_artist();
}
fn remove_album_cover(&mut self) {
self.inner
.remove_picture_by_type(id3::frame::PictureType::CoverFront);
}
fn track_number(&self) -> Option<u16> {
self.inner.track().map(|x| x as u16)
}
fn total_tracks(&self) -> Option<u16> {
self.inner.total_tracks().map(|x| x as u16)
}
fn set_track_number(&mut self, track: u16) {
self.inner.set_track(track as u32);
}
fn remove_track_number(&mut self) {
self.inner.remove_track();
}
fn set_total_tracks(&mut self, total_track: u16) {
self.inner.set_total_tracks(total_track as u32);
}
fn disc(&self) -> (Option<u16>, Option<u16>) {
(
self.inner.disc().map(|x| x as u16),
self.inner.total_discs().map(|x| x as u16),
)
fn remove_total_tracks(&mut self) {
self.inner.remove_total_tracks();
}
fn disc_number(&self) -> Option<u16> {
self.inner.disc().map(|x| x as u16)
}
fn total_discs(&self) -> Option<u16> {
self.inner.total_discs().map(|x| x as u16)
}
fn set_disc_number(&mut self, disc_number: u16) {
self.inner.set_disc(disc_number as u32)
}
fn remove_disc_number(&mut self) {
self.inner.remove_disc();
}
fn set_total_discs(&mut self, total_discs: u16) {
self.inner.set_total_discs(total_discs as u32)
}
fn write_to(&self, file: &File) -> Result<(), BoxedError> {
fn remove_total_discs(&mut self) {
self.inner.remove_total_discs();
}
fn write_to(&mut self, file: &mut File) -> Result<(), BoxedError> {
self.inner.write_to(file, id3::Version::Id3v24)?;
Ok(())
}
fn write_to_path(&self, path: &str) -> Result<(), BoxedError> {
fn write_to_path(&mut self, path: &str) -> Result<(), BoxedError> {
self.inner.write_to_path(path, id3::Version::Id3v24)?;
Ok(())
}
@ -289,7 +373,7 @@ pub struct M4aTags {
}
impl M4aTags {
pub fn from_path(path: impl AsRef<Path>) -> Result<Self, BoxedError> {
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, BoxedError> {
Ok(Self {
inner: mp4ameta::Tag::read_from_path(path)?,
})
@ -318,23 +402,16 @@ impl AudioTagsIo for M4aTags {
fn set_year(&mut self, year: i32) {
self.inner.set_year(year.to_string())
}
fn album(&self) -> Option<Album> {
self.inner.album().map(|title| Album {
title: title.to_owned(),
artist: self.inner.album_artist().map(|x| x.to_owned()),
cover: self.album_cover(),
})
}
fn album_cover(&self) -> Option<Picture> {
use mp4ameta::Data::*;
if let Some(Some(pic)) = self.inner.artwork().map(|data| match data {
Jpeg(d) => Some(Picture {
data: d.clone(),
picture_type: PictureType::Jpeg,
mime_type: MimeType::Jpeg,
}),
Png(d) => Some(Picture {
data: d.clone(),
picture_type: PictureType::Png,
mime_type: MimeType::Png,
}),
_ => None,
}) {
@ -349,24 +426,11 @@ impl AudioTagsIo for M4aTags {
fn album_artist(&self) -> Option<&str> {
self.inner.album_artist()
}
fn set_album(&mut self, album: Album) {
self.inner.set_album(album.title);
if let Some(artist) = album.artist {
self.inner.set_album_artist(artist)
} else {
// self.inner.remove_album_artist(artist)
}
if let Some(pic) = album.cover {
self.set_album_cover(pic)
} else {
self.inner.remove_artwork();
}
}
fn set_album_cover(&mut self, cover: Picture) {
self.inner.remove_artwork();
self.inner.add_artwork(match cover.picture_type {
PictureType::Png => mp4ameta::Data::Png(cover.data),
PictureType::Jpeg => mp4ameta::Data::Jpeg(cover.data),
self.remove_album_cover();
self.inner.add_artwork(match cover.mime_type {
MimeType::Png => mp4ameta::Data::Png(cover.data),
MimeType::Jpeg => mp4ameta::Data::Jpeg(cover.data),
_ => panic!("Only png and jpeg are supported in m4a"),
});
}
@ -376,8 +440,11 @@ impl AudioTagsIo for M4aTags {
fn set_album_artist(&mut self, v: &str) {
self.inner.set_album_artist(v)
}
fn track(&self) -> (Option<u16>, Option<u16>) {
self.inner.track()
fn track_number(&self) -> Option<u16> {
self.inner.track_number()
}
fn total_tracks(&self) -> Option<u16> {
self.inner.total_tracks()
}
fn set_track_number(&mut self, track: u16) {
self.inner.set_track_number(track);
@ -385,8 +452,11 @@ impl AudioTagsIo for M4aTags {
fn set_total_tracks(&mut self, total_track: u16) {
self.inner.set_total_tracks(total_track);
}
fn disc(&self) -> (Option<u16>, Option<u16>) {
self.inner.disc()
fn disc_number(&self) -> Option<u16> {
self.inner.disc_number()
}
fn total_discs(&self) -> Option<u16> {
self.inner.total_discs()
}
fn set_disc_number(&mut self, disc_number: u16) {
self.inner.set_disc_number(disc_number)
@ -394,11 +464,230 @@ impl AudioTagsIo for M4aTags {
fn set_total_discs(&mut self, total_discs: u16) {
self.inner.set_total_discs(total_discs)
}
fn write_to(&self, file: &File) -> Result<(), BoxedError> {
fn remove_title(&mut self) {
self.inner.remove_title();
}
fn remove_artist(&mut self) {
// self.inner.remove_artist(); // TODO:
}
fn remove_year(&mut self) {
self.inner.remove_year();
}
fn remove_album_title(&mut self) {
self.inner.remove_album();
}
fn remove_album_artist(&mut self) {
// self.inner.remove_album_artist(); // TODO:
}
fn remove_album_cover(&mut self) {
self.inner.remove_artwork();
}
fn remove_track(&mut self) {
self.inner.remove_track();
}
fn remove_track_number(&mut self) {
// TODO: self.inner.remove_track_number();
}
fn remove_total_tracks(&mut self) {
// TODO: self.inner.remove_total_tracks();
}
fn remove_disc(&mut self) {
self.inner.remove_disc();
}
fn remove_disc_number(&mut self) {
// TODO: self.inner.remove_disc_number();
}
fn remove_total_discs(&mut self) {
// TODO: self.inner.remove_total_discs();
}
fn write_to(&mut self, file: &mut File) -> Result<(), BoxedError> {
self.inner.write_to(file)?;
Ok(())
}
fn write_to_path(&self, path: &str) -> Result<(), BoxedError> {
fn write_to_path(&mut self, path: &str) -> Result<(), BoxedError> {
self.inner.write_to_path(path)?;
Ok(())
}
}
struct FlacTags {
inner: metaflac::Tag,
//vorbis_comments: std::collections::HashMap<String, Vec<String>>,
}
impl FlacTags {
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, BoxedError> {
// let tag = metaflac::Tag::read_from_path(path)?;
// let vorbis_comments = tag
// .vorbis_comments()
// .map(|x| x.comments)
// .unwrap_or(HashMap::default());
// Ok(Self {
// tag,
// vorbis_comments,
// })
Ok(Self {
inner: metaflac::Tag::read_from_path(path)?,
})
}
pub fn get_first(&self, key: &str) -> Option<&str> {
if let Some(Some(v)) = self.inner.vorbis_comments().map(|c| c.get(key)) {
if !v.is_empty() {
Some(v[0].as_str())
} else {
None
}
} else {
None
}
}
pub fn set_first(&mut self, key: &str, val: &str) {
self.inner.vorbis_comments_mut().set(key, vec![val]);
}
pub fn remove(&mut self, k: &str) {
self.inner.vorbis_comments_mut().comments.remove(k);
}
}
impl AudioTagsIo for FlacTags {
fn title(&self) -> Option<&str> {
self.get_first("TITLE")
}
fn set_title(&mut self, title: &str) {
self.set_first("TITLE", title);
}
fn artist(&self) -> Option<&str> {
self.get_first("ARTIST")
}
fn set_artist(&mut self, artist: &str) {
self.set_first("ARTIST", artist)
}
fn year(&self) -> Option<i32> {
if let Some(Ok(y)) = self
.get_first("DATE")
.map(|s| s.chars().take(4).collect::<String>().parse::<i32>())
{
Some(y)
} else if let Some(Ok(y)) = self.get_first("YEAR").map(|s| s.parse::<i32>()) {
Some(y)
} else {
None
}
}
fn set_year(&mut self, year: i32) {
self.set_first("DATE", &year.to_string());
self.set_first("YEAR", &year.to_string());
}
fn album_title(&self) -> Option<&str> {
self.get_first("ALBUM")
}
fn set_album_title(&mut self, title: &str) {
self.set_first("ALBUM", title)
}
fn album_artist(&self) -> Option<&str> {
self.get_first("ALBUMARTIST")
}
fn set_album_artist(&mut self, v: &str) {
self.set_first("ALBUMARTIST", v)
}
fn album_cover(&self) -> Option<Picture> {
if let Some(Ok(pic)) = self
.inner
.pictures()
.filter(|&pic| matches!(pic.picture_type, metaflac::block::PictureType::CoverFront))
.next()
.map(|pic| Picture::try_with_mime(pic.data.clone(), &pic.mime_type))
{
Some(pic)
} else {
None
}
}
fn set_album_cover(&mut self, cover: Picture) {
self.remove_album_cover();
let mime = String::from(cover.mime_type);
let picture_type = metaflac::block::PictureType::CoverFront;
self.inner.add_picture(mime, picture_type, cover.data);
}
fn track_number(&self) -> Option<u16> {
if let Some(Ok(n)) = self.get_first("TRACKNUMBER").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
}
}
fn set_track_number(&mut self, v: u16) {
self.set_first("TRACKNUMBER", &v.to_string())
}
// ! not standard
fn total_tracks(&self) -> Option<u16> {
if let Some(Ok(n)) = self.get_first("TOTALTRACKS").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
}
}
fn set_total_tracks(&mut self, v: u16) {
self.set_first("TOTALTRACKS", &v.to_string())
}
fn disc_number(&self) -> Option<u16> {
if let Some(Ok(n)) = self.get_first("DISCNUMBER").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
}
}
fn set_disc_number(&mut self, v: u16) {
self.set_first("DISCNUMBER", &v.to_string())
}
// ! not standard
fn total_discs(&self) -> Option<u16> {
if let Some(Ok(n)) = self.get_first("TOTALDISCS").map(|x| x.parse::<u16>()) {
Some(n)
} else {
None
}
}
fn set_total_discs(&mut self, v: u16) {
self.set_first("TOTALDISCS", &v.to_string())
}
fn remove_title(&mut self) {
self.remove("TITLE");
}
fn remove_artist(&mut self) {
self.remove("ARTIST");
}
fn remove_year(&mut self) {
self.remove("YEAR");
self.remove("DATE");
}
fn remove_album_title(&mut self) {
self.remove("ALBUM");
}
fn remove_album_artist(&mut self) {
self.remove("ALBUMARTIST");
}
fn remove_album_cover(&mut self) {
self.inner
.remove_picture_type(metaflac::block::PictureType::CoverFront)
}
fn remove_track_number(&mut self) {
self.remove("TRACKNUMBER");
}
fn remove_total_tracks(&mut self) {
self.remove("TOTALTRACKS");
}
fn remove_disc_number(&mut self) {
self.remove("DISCNUMBER");
}
fn remove_total_discs(&mut self) {
self.remove("TOTALDISCS");
}
fn write_to(&mut self, file: &mut File) -> Result<(), BoxedError> {
self.inner.write_to(file)?;
Ok(())
}
fn write_to_path(&mut self, path: &str) -> Result<(), BoxedError> {
self.inner.write_to_path(path)?;
Ok(())
}