Basic usage
This can be used by adding rtag
to your dependencies in your project's Cargo.toml
.
[]
= "0.3.5"
and this to your crate root:
extern crate rtag;
Example: reading and filtering
When you read a frame information, you use [Unit](metadata/enum.Unit.html)
.
Example: reading V1 frame.
use Unit;
use MetadataReader;
for m in new.unwrap
Example: reading V2 frame.
``rust use rtag::frame::*; use rtag::metadata::Unit; use rtag::metadata::MetadataReader;
for m in MetadataReader::new("./test-resources/240-pcnt.mp3").unwrap() { match m { Unit::FrameV2(head, FrameBody::PCNT(frame)) => { assert_eq!(256, frame.counter); }, _ => () } }
``
Example: modifying a frame.
``rust use std::fs; use rtag::frame::*; use rtag::metadata::Unit; use rtag::metadata::MetadataReader; use rtag::metadata::MetadataWriter;
let path = "./test-resources/240.test.mp3"; fs::copy("./test-resources/240.mp3", path).unwrap();
let new_data = MetadataReader::new(path) .unwrap() .fold(Vec::new(), |mut vec, unit| { if let Unit::FrameV2(frame_head, frame_body) = unit { let new_frame_body = if let FrameBody::TALB(ref frame) = frame_body { let mut new_frame = frame.clone(); new_frame.text = "Album!".to_string(); FrameBody::TALB(new_frame) } else { frame_body.clone() };
vec.push(Unit::FrameV2(frame_head, new_frame_body));
} else {
vec.push(unit);
}
vec
});
let writer = MetadataWriter::new(path).unwrap(); let _ = writer.write(new_data, false); let _ = fs::remove_file(path).unwrap(); ``
Example: rewriting all the frame to version 4.
``rust use std::fs; use rtag::frame::*; use rtag::metadata::Unit; use rtag::metadata::MetadataReader; use rtag::metadata::MetadataWriter;
let path = "./test-resources/v2.2.test.mp3"; fs::copy("./test-resources/v2.2.mp3", path).unwrap();
let frames2_2 = MetadataReader::new(path).unwrap().collect::<Vec>(); let _ = MetadataWriter::new(path).unwrap().write(frames2_2, true); let i = MetadataReader::new(path) .unwrap() .filter(|unit| match unit { &Unit::FrameV2(FrameHeader::V22(_), _) => true, _ => false, });
assert_eq!(i.count(), 0);
let i = MetadataReader::new(path) .unwrap() .filter(|unit| match unit { &Unit::FrameV2(FrameHeader::V24(_), _) => true, _ => false, });
assert_eq!(i.count(), 5); let _ = fs::remove_file(path).unwrap(); ``
Example: Looking into frame without property name of frame.
``rust se rtag::frame::*; se rtag::metadata::{Unit, MetadataReader};
or m in MetadataReader::new("./test-resources/240.mp3").unwrap() { match m { Unit::FrameV2(_, ref frame) => {
let _ = frame.to_map(); // frame to map
frame.inside(|key, value| {
println!("{}: {}", key, value);
true // continue or not
});
},
_ => {}
}
``