Skip to content

Commit

Permalink
Merge pull request #3 from martpie/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
martpie authored May 25, 2022
2 parents 0ac19f0 + edae1dc commit 04a5f71
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 36 deletions.
17 changes: 6 additions & 11 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
name: Rust

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ repository = "https://github.com/TianyiShi2001/audiotags"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
id3 = "0.5.1"
mp4ameta = "0.6"
id3 = "1.0.3"
mp4ameta = "0.11.0"
metaflac = "0.2"
thiserror = "1.0.21"
audiotags-dev-macro = {path = "./audiotags-dev-macro", version = "0.1.4"}

[dev-dependencies]
tempfile = "3.3.0"

[features]
defualt = ['from']
from = []
default = ['from']
from = []
11 changes: 11 additions & 0 deletions src/anytag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ pub struct AnyTag<'a> {
pub title: Option<&'a str>,
pub artists: Option<Vec<&'a str>>,
pub year: Option<i32>,
pub duration: Option<f64>,
pub album_title: Option<&'a str>,
pub album_artists: Option<Vec<&'a str>>,
pub album_cover: Option<Picture<'a>>,
pub track_number: Option<u16>,
pub total_tracks: Option<u16>,
pub disc_number: Option<u16>,
pub total_discs: Option<u16>,
pub genre:Option<&'a str>,
}

impl AudioTagConfig for AnyTag<'_> {
Expand Down Expand Up @@ -41,6 +43,12 @@ impl<'a> AnyTag<'a> {
pub fn set_year(&mut self, year: i32) {
self.year = Some(year);
}
pub fn duration(&self) -> Option<f64> {
self.duration
}
pub fn set_duration(&mut self, duration: f64) {
self.duration = Some(duration);
}
pub fn album_title(&self) -> Option<&str> {
self.album_title.as_deref()
}
Expand All @@ -59,6 +67,9 @@ impl<'a> AnyTag<'a> {
pub fn total_discs(&self) -> Option<u16> {
self.total_tracks
}
pub fn genre(&self) -> Option<&str> {
self.genre.as_deref()
}
}

impl AnyTag<'_> {
Expand Down
17 changes: 17 additions & 0 deletions src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl<'a> From<&'a FlacTag> for AnyTag<'a> {
t.title = inp.title();
t.artists = inp.artists();
t.year = inp.year();
t.duration = inp.duration();
t.album_title = inp.album_title();
t.album_artists = inp.album_artists();
t.album_cover = inp.album_cover();
Expand Down Expand Up @@ -90,6 +91,12 @@ impl AudioTagEdit for FlacTag {
self.set_first("YEAR", &year.to_string());
}

fn duration(&self) -> Option<f64> {
self.inner
.get_streaminfo()
.map(|s| s.total_samples as f64 / f64::from(s.sample_rate))
}

fn album_title(&self) -> Option<&str> {
self.get_first("ALBUM")
}
Expand Down Expand Up @@ -170,6 +177,13 @@ impl AudioTagEdit for FlacTag {
self.set_first("TOTALDISCS", &v.to_string())
}

fn genre(&self) -> Option<&str> {
self.get_first("GENRE")
}
fn set_genre(&mut self, v: &str) {
self.set_first("GENRE", v);
}

fn remove_title(&mut self) {
self.remove("TITLE");
}
Expand Down Expand Up @@ -202,6 +216,9 @@ impl AudioTagEdit for FlacTag {
fn remove_total_discs(&mut self) {
self.remove("TOTALDISCS");
}
fn remove_genre(&mut self) {
self.remove("GENRE");
}
}

impl AudioTagWrite for FlacTag {
Expand Down
23 changes: 19 additions & 4 deletions src/components/id3_tag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::*;
use id3;
use id3::{self, TagLike};

pub use id3::Tag as Id3v2InnerTag;

Expand All @@ -13,13 +13,15 @@ impl<'a> From<&'a Id3v2Tag> for AnyTag<'a> {
title: inp.title(),
artists: inp.artists(),
year: inp.year(),
duration: Some(inp.inner.duration().unwrap() as f64),
album_title: inp.album_title(),
album_artists: inp.album_artists(),
album_cover: inp.album_cover(),
track_number: inp.track_number(),
total_tracks: inp.total_tracks(),
disc_number: inp.disc_number(),
total_discs: inp.total_discs(),
genre: inp.genre(),
}
}
}
Expand All @@ -39,6 +41,7 @@ impl<'a> From<AnyTag<'a>> for Id3v2Tag {
inp.total_tracks().map(|v| t.set_total_tracks(v as u32));
inp.disc_number().map(|v| t.set_disc(v as u32));
inp.total_discs().map(|v| t.set_total_discs(v as u32));
inp.genre().map(|v| t.set_genre(v));
t
},
}
Expand Down Expand Up @@ -89,8 +92,10 @@ impl AudioTagEdit for Id3v2Tag {
self.inner.set_year(year)
}
fn remove_year(&mut self) {
self.inner.remove("TYER")
// self.inner.remove_year(); // TODO
self.inner.remove_year();
}
fn duration(&self) -> Option<f64> {
self.inner.duration().map(|d| f64::from(d))
}

fn album_title(&self) -> Option<&str> {
Expand Down Expand Up @@ -127,7 +132,7 @@ impl AudioTagEdit for Id3v2Tag {
}
fn set_album_cover(&mut self, cover: Picture) {
self.remove_album_cover();
self.inner.add_picture(id3::frame::Picture {
self.inner.add_frame(id3::frame::Picture {
mime_type: String::from(cover.mime_type),
picture_type: id3::frame::PictureType::CoverFront,
description: "".to_owned(),
Expand Down Expand Up @@ -178,6 +183,16 @@ impl AudioTagEdit for Id3v2Tag {
fn remove_total_discs(&mut self) {
self.inner.remove_total_discs();
}

fn genre(&self) -> Option<&str> {
self.inner.genre()
}
fn set_genre(&mut self, v: &str) {
self.inner.set_genre(v);
}
fn remove_genre(&mut self) {
self.inner.remove_genre();
}
}

impl AudioTagWrite for Id3v2Tag {
Expand Down
45 changes: 34 additions & 11 deletions src/components/mp4_tag.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::*;
use mp4ameta;
use mp4ameta::{self, ImgFmt};

pub use mp4ameta::Tag as Mp4InnerTag;

Expand All @@ -10,6 +10,7 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
let title = inp.title();
let artists = inp.artists().map(|i| i.into_iter().collect::<Vec<_>>());
let year = inp.year();
let duration = inp.duration();
let album_title = inp.album_title();
let album_artists = inp
.album_artists()
Expand All @@ -21,18 +22,21 @@ impl<'a> From<&'a Mp4Tag> for AnyTag<'a> {
let (a, b) = inp.disc();
let disc_number = a;
let total_discs = b;
let genre = inp.genre();
Self {
config: inp.config.clone(),
title,
artists,
year,
duration,
album_title,
album_cover,
album_artists,
track_number,
total_tracks,
disc_number,
total_discs,
genre,
}
}
}
Expand Down Expand Up @@ -113,6 +117,11 @@ impl AudioTagEdit for Mp4Tag {
self.inner.set_year(year.to_string())
}

// Return Option with duration in second
fn duration(&self) -> Option<f64> {
self.inner.duration().map(|d| d.as_secs_f64())
}

fn album_title(&self) -> Option<&str> {
self.inner.album()
}
Expand Down Expand Up @@ -143,14 +152,13 @@ impl AudioTagEdit for Mp4Tag {
}

fn album_cover(&self) -> Option<Picture> {
use mp4ameta::Data::*;
self.inner.artwork().and_then(|data| match data {
Jpeg(d) => Some(Picture {
data: d,
self.inner.artwork().and_then(|data| match data.fmt {
ImgFmt::Jpeg => Some(Picture {
data: data.data,
mime_type: MimeType::Jpeg,
}),
Png(d) => Some(Picture {
data: d,
ImgFmt::Png => Some(Picture {
data: data.data,
mime_type: MimeType::Png,
}),
_ => None,
Expand All @@ -159,8 +167,14 @@ impl AudioTagEdit for Mp4Tag {
fn set_album_cover(&mut self, cover: Picture) {
self.remove_album_cover();
self.inner.add_artwork(match cover.mime_type {
MimeType::Png => mp4ameta::Data::Png(cover.data.to_owned()),
MimeType::Jpeg => mp4ameta::Data::Jpeg(cover.data.to_owned()),
MimeType::Png => mp4ameta::Img {
fmt: ImgFmt::Png,
data: cover.data.to_owned(),
},
MimeType::Jpeg => mp4ameta::Img {
fmt: ImgFmt::Jpeg,
data: cover.data.to_owned(),
},
_ => panic!("Only png and jpeg are supported in m4a"),
});
}
Expand Down Expand Up @@ -191,6 +205,13 @@ impl AudioTagEdit for Mp4Tag {
self.inner.set_total_discs(total_discs)
}

fn genre(&self) -> Option<&str> {
self.inner.genre()
}
fn set_genre(&mut self, genre: &str) {
self.inner.set_genre(genre);
}

fn remove_title(&mut self) {
self.inner.remove_title();
}
Expand All @@ -204,11 +225,10 @@ impl AudioTagEdit for Mp4Tag {
self.inner.remove_album();
}
fn remove_album_artist(&mut self) {
self.inner.remove_data(mp4ameta::atom::ALBUM_ARTIST);
self.inner.remove_album_artists();
}
fn remove_album_cover(&mut self) {
self.inner.remove_artwork();
self.inner.remove_artworks();
}
fn remove_track(&mut self) {
self.inner.remove_track(); // faster than removing separately
Expand All @@ -228,6 +248,9 @@ impl AudioTagEdit for Mp4Tag {
fn remove_total_discs(&mut self) {
self.inner.remove_total_discs();
}
fn remove_genre(&mut self) {
self.inner.remove_genres();
}
}

impl AudioTagWrite for Mp4Tag {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub use std::convert::{TryFrom, TryInto};
///
/// # Examples
///
/// ```
/// ```no_run
/// use audiotags::{Tag, TagType};
/// // Guess the format by default
/// let mut tag = Tag::new().read_from_path("assets/a.mp3").unwrap();
Expand Down
6 changes: 6 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub trait AudioTagEdit: AudioTagConfig {
fn set_year(&mut self, year: i32);
fn remove_year(&mut self);

fn duration(&self) -> Option<f64>;

fn album(&self) -> Option<Album<'_>> {
self.album_title().map(|title| Album {
title,
Expand Down Expand Up @@ -124,6 +126,10 @@ pub trait AudioTagEdit: AudioTagConfig {
fn total_discs(&self) -> Option<u16>;
fn set_total_discs(&mut self, total_discs: u16);
fn remove_total_discs(&mut self);

fn genre(&self) -> Option<&str>;
fn set_genre(&mut self, genre:&str);
fn remove_genre(&mut self);
}

pub trait AudioTagWrite {
Expand Down
Loading

0 comments on commit 04a5f71

Please sign in to comment.