lofty-rs/tests/files/mpeg.rs

354 lines
9.1 KiB
Rust
Raw Normal View History

2021-12-05 22:02:22 +00:00
use crate::{set_artist, temp_file, verify_artist};
2023-07-04 16:53:22 +00:00
use lofty::id3::v2::{Frame, FrameFlags, FrameValue, Id3v2Tag, KeyValueFrame};
use lofty::mpeg::MpegFile;
2022-09-24 06:34:22 +00:00
use lofty::{
2023-07-04 16:53:22 +00:00
Accessor, AudioFile, FileType, ItemKey, ItemValue, ParseOptions, Probe, Tag, TagExt, TagItem,
TagType, TaggedFileExt,
2022-09-24 06:34:22 +00:00
};
2023-07-04 16:53:22 +00:00
use std::io::{Seek, Write};
2021-09-27 02:36:20 +00:00
#[test]
fn read() {
2021-09-27 02:36:20 +00:00
// Here we have an MP3 file with an ID3v2, ID3v1, and an APEv2 tag
2022-09-24 06:34:22 +00:00
let file = Probe::open("tests/files/assets/minimal/full_test.mp3")
.unwrap()
.options(ParseOptions::new().read_properties(false))
.read()
.unwrap();
2021-09-27 02:36:20 +00:00
assert_eq!(file.file_type(), FileType::Mpeg);
2021-09-27 02:36:20 +00:00
// Verify the ID3v2 tag first
crate::verify_artist!(file, primary_tag, "Foo artist", 1);
// Now verify ID3v1
crate::verify_artist!(file, tag, TagType::Id3v1, "Bar artist", 1);
2021-09-27 02:36:20 +00:00
// Finally, verify APEv2
crate::verify_artist!(file, tag, TagType::Ape, "Baz artist", 1);
2021-09-27 02:36:20 +00:00
}
#[test]
fn read_with_junk_bytes_between_frames() {
// Read a file that includes an ID3v2.3 data block followed by four bytes of junk data (0x20)
2022-09-24 06:34:22 +00:00
let file = Probe::open("tests/files/assets/junk_between_id3_and_mp3.mp3")
.unwrap()
.read()
.unwrap();
// note that the file contains ID3v2 and ID3v1 data
assert_eq!(file.file_type(), FileType::Mpeg);
let id3v2_tag = &file.tags()[0];
assert_eq!(id3v2_tag.artist().as_deref(), Some("artist test"));
assert_eq!(id3v2_tag.album().as_deref(), Some("album test"));
assert_eq!(id3v2_tag.title().as_deref(), Some("title test"));
assert_eq!(
id3v2_tag.get_string(&ItemKey::EncoderSettings),
Some("Lavf58.62.100")
);
let id3v1_tag = &file.tags()[1];
assert_eq!(id3v1_tag.artist().as_deref(), Some("artist test"));
assert_eq!(id3v1_tag.album().as_deref(), Some("album test"));
assert_eq!(id3v1_tag.title().as_deref(), Some("title test"));
}
#[test]
fn issue_82_solidus_in_tag() {
let file = Probe::open("tests/files/assets/issue_82_solidus_in_tag.mp3")
.unwrap()
.read()
.unwrap();
assert_eq!(file.file_type(), FileType::Mpeg);
let id3v2_tag = &file.tags()[0];
assert_eq!(id3v2_tag.title().as_deref(), Some("Foo / title"));
}
#[test]
fn issue_87_duplicate_id3v2() {
// The first tag has a bunch of information: An album, artist, encoder, and a title.
// This tag is immediately followed by another the contains an artist.
// We expect that the title from the first tag has been replaced by this second tag, while
// retaining the rest of the information from the first tag.
let file = Probe::open("tests/files/assets/issue_87_duplicate_id3v2.mp3")
.unwrap()
.read()
.unwrap();
assert_eq!(file.file_type(), FileType::Mpeg);
let id3v2_tag = &file.tags()[0];
assert_eq!(id3v2_tag.album().as_deref(), Some("album test"));
assert_eq!(id3v2_tag.artist().as_deref(), Some("Foo artist")); // Original tag has "artist test"
assert_eq!(
id3v2_tag.get_string(&ItemKey::EncoderSettings),
Some("Lavf58.62.100")
);
assert_eq!(id3v2_tag.title().as_deref(), Some("title test"));
}
2021-09-27 02:36:20 +00:00
#[test]
fn write() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
2021-09-27 02:36:20 +00:00
2022-09-24 06:34:22 +00:00
let mut tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
2021-09-27 02:36:20 +00:00
assert_eq!(tagged_file.file_type(), FileType::Mpeg);
2021-09-27 02:36:20 +00:00
// ID3v2
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 1 => file, "Bar artist");
// ID3v1
crate::set_artist!(tagged_file, tag_mut, TagType::Id3v1, "Bar artist", 1 => file, "Baz artist");
2021-09-27 02:36:20 +00:00
// APEv2
crate::set_artist!(tagged_file, tag_mut, TagType::Ape, "Baz artist", 1 => file, "Qux artist");
2021-09-27 02:36:20 +00:00
// Now reread the file
2022-04-13 17:28:48 +00:00
file.rewind().unwrap();
2022-09-24 06:34:22 +00:00
let mut tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
2021-09-27 02:36:20 +00:00
crate::set_artist!(tagged_file, primary_tag_mut, "Bar artist", 1 => file, "Foo artist");
crate::set_artist!(tagged_file, tag_mut, TagType::Id3v1, "Baz artist", 1 => file, "Bar artist");
2021-09-27 02:36:20 +00:00
crate::set_artist!(tagged_file, tag_mut, TagType::Ape, "Qux artist", 1 => file, "Baz artist");
2021-09-27 02:36:20 +00:00
}
#[test]
fn save_to_id3v2() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
assert_eq!(tagged_file.file_type(), FileType::Mpeg);
let mut tag = Tag::new(TagType::Id3v2);
// Set title to save this tag.
tag.set_title("title".to_string());
file.rewind().unwrap();
tag.save_to(&mut file).unwrap();
// Now reread the file
file.rewind().unwrap();
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
let tag = tagged_file.tag(TagType::Id3v2).unwrap();
assert!(tag.track().is_none());
assert!(tag.track_total().is_none());
assert!(tag.disk().is_none());
assert!(tag.disk_total().is_none());
}
#[test]
fn save_number_of_track_and_disk_to_id3v2() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
assert_eq!(tagged_file.file_type(), FileType::Mpeg);
let mut tag = Tag::new(TagType::Id3v2);
let track = 1;
let disk = 2;
tag.set_track(track);
tag.set_disk(disk);
file.rewind().unwrap();
tag.save_to(&mut file).unwrap();
// Now reread the file
file.rewind().unwrap();
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
let tag = tagged_file.tag(TagType::Id3v2).unwrap();
assert_eq!(tag.track().unwrap(), track);
assert!(tag.track_total().is_none());
assert_eq!(tag.disk().unwrap(), disk);
assert!(tag.disk_total().is_none());
}
#[test]
fn save_total_of_track_and_disk_to_id3v2() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
assert_eq!(tagged_file.file_type(), FileType::Mpeg);
let mut tag = Tag::new(TagType::Id3v2);
let track_total = 2;
let disk_total = 3;
tag.set_track_total(track_total);
tag.set_disk_total(disk_total);
file.rewind().unwrap();
tag.save_to(&mut file).unwrap();
// Now reread the file
file.rewind().unwrap();
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
let tag = tagged_file.tag(TagType::Id3v2).unwrap();
assert_eq!(tag.track().unwrap(), 0);
assert_eq!(tag.track_total().unwrap(), track_total);
assert_eq!(tag.disk().unwrap(), 0);
assert_eq!(tag.disk_total().unwrap(), disk_total);
}
#[test]
fn save_number_pair_of_track_and_disk_to_id3v2() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
assert_eq!(tagged_file.file_type(), FileType::Mpeg);
let mut tag = Tag::new(TagType::Id3v2);
let track = 1;
let track_total = 2;
let disk = 3;
let disk_total = 4;
tag.set_track(track);
tag.set_track_total(track_total);
tag.set_disk(disk);
tag.set_disk_total(disk_total);
file.rewind().unwrap();
tag.save_to(&mut file).unwrap();
// Now reread the file
file.rewind().unwrap();
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.unwrap();
let tag = tagged_file.tag(TagType::Id3v2).unwrap();
assert_eq!(tag.track().unwrap(), track);
assert_eq!(tag.track_total().unwrap(), track_total);
assert_eq!(tag.disk().unwrap(), disk);
assert_eq!(tag.disk_total().unwrap(), disk_total);
}
#[test]
fn remove_id3v2() {
crate::remove_tag!("tests/files/assets/minimal/full_test.mp3", TagType::Id3v2);
}
#[test]
fn remove_id3v1() {
crate::remove_tag!("tests/files/assets/minimal/full_test.mp3", TagType::Id3v1);
}
#[test]
fn remove_ape() {
crate::remove_tag!("tests/files/assets/minimal/full_test.mp3", TagType::Ape);
}
2023-07-02 20:02:41 +00:00
#[test]
fn read_and_write_tpil_frame() {
let key_value_pairs = vec![
2023-07-04 16:53:22 +00:00
("engineer".to_string(), "testperson".to_string()),
("vocalist".to_string(), "testhuman".to_string()),
];
2023-07-02 20:02:41 +00:00
2023-07-04 16:49:26 +00:00
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
2023-07-02 20:02:41 +00:00
2023-07-04 16:49:26 +00:00
let mut mpeg_file = MpegFile::read_from(&mut file, ParseOptions::new()).unwrap();
2023-07-02 20:02:41 +00:00
2023-07-04 16:49:26 +00:00
let tag: &mut Id3v2Tag = mpeg_file.id3v2_mut().unwrap();
2023-07-04 16:53:22 +00:00
2023-07-02 20:02:41 +00:00
tag.insert(
Frame::new(
2023-07-04 16:53:22 +00:00
"TIPL",
2023-07-02 20:02:41 +00:00
KeyValueFrame {
encoding: lofty::TextEncoding::UTF8,
2023-07-04 16:53:22 +00:00
key_value_pairs: key_value_pairs.clone(),
2023-07-02 20:02:41 +00:00
},
2023-07-04 16:53:22 +00:00
FrameFlags::default(),
)
.unwrap(),
2023-07-02 20:02:41 +00:00
);
file.rewind().unwrap();
tag.save_to(&mut file).unwrap();
// Now reread the file
file.rewind().unwrap();
2023-07-04 16:49:26 +00:00
let mpeg_file = MpegFile::read_from(&mut file, ParseOptions::new()).unwrap();
2023-07-02 20:02:41 +00:00
2023-07-04 16:49:26 +00:00
let tag: &Id3v2Tag = mpeg_file.id3v2().unwrap();
2023-07-02 20:02:41 +00:00
let content = match tag.get("TIPL").unwrap().content() {
FrameValue::KeyValue(content) => content,
2023-07-04 16:53:22 +00:00
_ => panic!("Wrong Frame Value Type for TIPL"),
2023-07-02 20:02:41 +00:00
};
assert_eq!(key_value_pairs, content.key_value_pairs);
2023-07-04 16:53:22 +00:00
}