music_dump_lib/meta_data/
lofty_metadata.rs1use std::{io::Cursor, path::Path};
2
3use lofty::{config::{ParseOptions, WriteOptions}, file::AudioFile, flac::FlacFile, id3::v2::Id3v2Tag, mpeg::MpegFile, picture::Picture, tag::TagExt};
4
5pub struct LoftyMetaData {
6 pub tag: Id3v2Tag,
7}
8
9impl LoftyMetaData {
10 pub fn from(audio: &[u8], picture: &[u8]) -> Self {
11 let mut reader = Cursor::new(audio);
12 let mp3_file = MpegFile::read_from(&mut reader, ParseOptions::default());
13 if let Ok(mp3_file) = mp3_file {
14 let mut tag = mp3_file.id3v2().unwrap().clone();
15 let mut picture_bytes = Cursor::new(picture);
16 let _ = tag.insert_picture(Picture::from_reader(&mut picture_bytes).unwrap());
17 return Self {
18 tag,
19 }
20 }
21 let flac_file = FlacFile::read_from(&mut reader, ParseOptions::default());
22 if let Ok(flac_file) = flac_file {
23 let mut tag = flac_file.id3v2().unwrap().clone();
24 let mut picture_bytes = Cursor::new(picture);
25 let _ = tag.insert_picture(Picture::from_reader(&mut picture_bytes).unwrap());
26 return Self {
27 tag,
28 }
29 }
30 panic!("none of the supported formats found: (mp3, flac)")
31 }
32}
33
34impl LoftyMetaData {
35 pub fn inject_to_path<P: AsRef<Path>>(&self, path: P) {
36 self.tag.save_to_path(path, WriteOptions::default()).unwrap();
37 }
38}