pub enum Tag {
Id3Tag {
inner: Tag,
},
VorbisFlacTag {
inner: Tag,
},
Mp4Tag {
inner: Tag,
},
OpusTag {
inner: Tag,
},
}Expand description
An object containing tags of one of the supported formats.
Variants§
Implementations§
Source§impl Tag
impl Tag
Sourcepub fn read_from_path<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn read_from_path<P: AsRef<Path>>(path: P) -> Result<Self>
Attempts to read a set of tags from the given path.
§Errors
This function could error if the given path has a file extension which contains invalid unicode or if the given path does not have a file extension at all.
This function could also error if the given path has a valid extension but the extension is not among the types supported by this crate.
Lastly, an error will be raised if the file type is supported but the reading the tags fails for some reason other than missing tags.
Examples found in repository?
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10
11 // Option 1: read from path
12 let tag = Tag::read_from_path(&path).unwrap();
13 println!("{:#?}", tag.title());
14
15 // Option 2: read from reader
16 let mut f = File::open(&path).unwrap();
17
18 let mut data = Vec::new();
19 f.read_to_end(&mut data).unwrap();
20
21 let cursor = Cursor::new(data);
22 // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23 let extension = path.extension().unwrap().to_str().unwrap();
24 let tag = Tag::read_from(extension, cursor).unwrap();
25 println!("{:#?}", tag.title());
26}Sourcepub fn read_from<R: Read + Seek>(extension: &str, f_in: R) -> Result<Self>
pub fn read_from<R: Read + Seek>(extension: &str, f_in: R) -> Result<Self>
Attempts to read a set of tags from the given reader.
The extension is necessary to determine which backend to use to decode the tags.
extension must be one of [mp3, wav, aiff, flac, mp4, m4a, m4p, m4b, m4r, m4v, opus]
§Errors
This function can error if the given extension is not supported by this crate.
Lastly, an error will be raised if the file type is supported but the reading the tags fails for some reason other than missing tags. This could be, for example, that the given reader ended too early or that the tags were encoded improperly. Please inspect the debug output of the error for more information.
Examples found in repository?
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10 let mut file = OpenOptions::new()
11 .read(true)
12 .write(true)
13 // .truncate(true)
14 .open(&path)
15 .unwrap();
16
17 let extension = path.extension().unwrap().to_str().unwrap();
18
19 let mut tag = Tag::read_from(extension, &file).unwrap();
20
21 file.rewind().unwrap();
22
23 let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25 tag.set_title(&title);
26 tag.write_to_file(&mut file).unwrap();
27}More examples
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10
11 // Option 1: read from path
12 let tag = Tag::read_from_path(&path).unwrap();
13 println!("{:#?}", tag.title());
14
15 // Option 2: read from reader
16 let mut f = File::open(&path).unwrap();
17
18 let mut data = Vec::new();
19 f.read_to_end(&mut data).unwrap();
20
21 let cursor = Cursor::new(data);
22 // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23 let extension = path.extension().unwrap().to_str().unwrap();
24 let tag = Tag::read_from(extension, cursor).unwrap();
25 println!("{:#?}", tag.title());
26}10fn main() {
11 let path = PathBuf::from(args().nth(1).unwrap());
12 let mut file = OpenOptions::new()
13 .read(true)
14 .write(true)
15 // .truncate(true)
16 .open(&path)
17 .unwrap();
18
19 let extension = path.extension().unwrap().to_str().unwrap();
20
21 let mut data = Vec::new();
22 file.read_to_end(&mut data).unwrap();
23 let mut cursor = Cursor::new(&mut data);
24
25 let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27 cursor.rewind().unwrap();
28
29 let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31 tag.set_title(&title);
32 tag.write_to_vec(&mut data).unwrap();
33
34 file.rewind().unwrap();
35 file.write_all(&data).unwrap();
36}Sourcepub fn write_to_path<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
pub fn write_to_path<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
Attempts to write the tags to the indicated path.
§Errors
This function will error if writing the tags fails in any way.
Sourcepub fn write_to_file(&mut self, file: &mut File) -> Result<()>
pub fn write_to_file(&mut self, file: &mut File) -> Result<()>
Write to a file. The file should already contain valid data of the correct type (e.g. the file should already contain an opus stream in order to correctly write opus tags).
The file’s cursor should be at the beginning of the file, and it should be opened with
read and write modes set (See OpenOptions for more info).
§Errors
This method can error if writing the tags fails, or if accessing the file fails (for example, if the modes are set wrong).
Examples found in repository?
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10 let mut file = OpenOptions::new()
11 .read(true)
12 .write(true)
13 // .truncate(true)
14 .open(&path)
15 .unwrap();
16
17 let extension = path.extension().unwrap().to_str().unwrap();
18
19 let mut tag = Tag::read_from(extension, &file).unwrap();
20
21 file.rewind().unwrap();
22
23 let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25 tag.set_title(&title);
26 tag.write_to_file(&mut file).unwrap();
27}Sourcepub fn write_to_vec(&mut self, vec: &mut Vec<u8>) -> Result<()>
pub fn write_to_vec(&mut self, vec: &mut Vec<u8>) -> Result<()>
Write to a byte vector. The vector should already contain valid data of the correct type (e.g. the vector should already contain an opus stream in order to correctly write opus tags).
§Errors
This method can error if one of the internal write methods fails. If that happens, the inner error will contain more information.
Examples found in repository?
10fn main() {
11 let path = PathBuf::from(args().nth(1).unwrap());
12 let mut file = OpenOptions::new()
13 .read(true)
14 .write(true)
15 // .truncate(true)
16 .open(&path)
17 .unwrap();
18
19 let extension = path.extension().unwrap().to_str().unwrap();
20
21 let mut data = Vec::new();
22 file.read_to_end(&mut data).unwrap();
23 let mut cursor = Cursor::new(&mut data);
24
25 let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27 cursor.rewind().unwrap();
28
29 let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31 tag.set_title(&title);
32 tag.write_to_vec(&mut data).unwrap();
33
34 file.rewind().unwrap();
35 file.write_all(&data).unwrap();
36}Sourcepub fn new_empty_id3() -> Self
pub fn new_empty_id3() -> Self
Creates an empty set of tags in the ID3 format.
Sourcepub fn new_empty_flac() -> Self
pub fn new_empty_flac() -> Self
Creates an empty set of tags in the FLAC format.
Sourcepub fn new_empty_mp4() -> Self
pub fn new_empty_mp4() -> Self
Creates an empty set of tags in the MP4 format.
Sourcepub fn new_empty_opus() -> Self
pub fn new_empty_opus() -> Self
Creates an empty set of tags in the Opus format.
Source§impl Tag
impl Tag
Sourcepub fn get_album_info(&self) -> Option<Album>
pub fn get_album_info(&self) -> Option<Album>
Gets the album information. If the album or album_artist fields are not present in the
audio file, this method returns None.
Sourcepub fn set_album_info(&mut self, album: Album) -> Result<()>
pub fn set_album_info(&mut self, album: Album) -> Result<()>
Sets the album information of the audio track.
§Errors
This function will error if album.cover has an invalid or unsupported MIME type.
Supported MIME types are: image/bmp, image/jpeg, image/png
Sourcepub fn remove_all_album_info(&mut self)
pub fn remove_all_album_info(&mut self)
Removes all album infofrom the audio track.
Sourcepub fn title(&self) -> Option<&str>
pub fn title(&self) -> Option<&str>
Gets the title.
Examples found in repository?
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10
11 // Option 1: read from path
12 let tag = Tag::read_from_path(&path).unwrap();
13 println!("{:#?}", tag.title());
14
15 // Option 2: read from reader
16 let mut f = File::open(&path).unwrap();
17
18 let mut data = Vec::new();
19 f.read_to_end(&mut data).unwrap();
20
21 let cursor = Cursor::new(data);
22 // You can also just pass in f instead of creating a cursor since Files are Read + Seek
23 let extension = path.extension().unwrap().to_str().unwrap();
24 let tag = Tag::read_from(extension, cursor).unwrap();
25 println!("{:#?}", tag.title());
26}Sourcepub fn set_title(&mut self, title: &str)
pub fn set_title(&mut self, title: &str)
Sets the title.
Examples found in repository?
8fn main() {
9 let path = PathBuf::from(args().nth(1).unwrap());
10 let mut file = OpenOptions::new()
11 .read(true)
12 .write(true)
13 // .truncate(true)
14 .open(&path)
15 .unwrap();
16
17 let extension = path.extension().unwrap().to_str().unwrap();
18
19 let mut tag = Tag::read_from(extension, &file).unwrap();
20
21 file.rewind().unwrap();
22
23 let title = args().skip(2).collect::<Vec<String>>().join(" ");
24
25 tag.set_title(&title);
26 tag.write_to_file(&mut file).unwrap();
27}More examples
10fn main() {
11 let path = PathBuf::from(args().nth(1).unwrap());
12 let mut file = OpenOptions::new()
13 .read(true)
14 .write(true)
15 // .truncate(true)
16 .open(&path)
17 .unwrap();
18
19 let extension = path.extension().unwrap().to_str().unwrap();
20
21 let mut data = Vec::new();
22 file.read_to_end(&mut data).unwrap();
23 let mut cursor = Cursor::new(&mut data);
24
25 let mut tag = Tag::read_from(extension, &mut cursor).unwrap();
26
27 cursor.rewind().unwrap();
28
29 let title = args().skip(2).collect::<Vec<String>>().join(" ");
30
31 tag.set_title(&title);
32 tag.write_to_vec(&mut data).unwrap();
33
34 file.rewind().unwrap();
35 file.write_all(&data).unwrap();
36}Sourcepub fn remove_title(&mut self)
pub fn remove_title(&mut self)
Removes any title fields from the file.
Sourcepub fn artist(&self) -> Option<String>
pub fn artist(&self) -> Option<String>
Gets the artist (note: NOT the album artist!)
If multiple ARTIST tags are present, they will be joined with a ;
Sourcepub fn set_artist(&mut self, artist: &str)
pub fn set_artist(&mut self, artist: &str)
Sets the artist (note: NOT the album artist!)
Sourcepub fn remove_artist(&mut self)
pub fn remove_artist(&mut self)
Removes the artist (note: NOT the album artist!)