summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs50
1 files changed, 41 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 327daa0..5af6eb1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use crate::options::Options;
mod encoding;
mod error;
+mod id3v1;
mod id3v2;
mod options;
@@ -22,6 +23,10 @@ fn parse_options_into(opt: &mut Options) {
.add_option(&["--assume-utf8"], StoreTrue,
"Assume that all strings specified in the MP3 as Latin-1 are really UTF-8.");
+ ap.refer(&mut opt.remove_v1)
+ .add_option(&["--remove-v1"], StoreTrue,
+ "Remove any existing ID3v1.* tags.");
+
ap.refer(&mut opt.set_tags.album) .add_option(&["-A", "--album"], StoreOption, "Set/replace album");
ap.refer(&mut opt.set_tags.artist).add_option(&["-a", "--artist"], StoreOption, "Set/replace artist");
ap.refer(&mut opt.set_tags.title) .add_option(&["-t", "--title"], StoreOption, "Set/replace title");
@@ -86,8 +91,36 @@ fn modify_tag(tag: &mut ID3v2, new_frame: Frame) -> io::Result<()> {
fn main() -> io::Result<()> {
let options = parse_options();
- let mut tag = ID3v2::from_stream(&mut File::open(&options.file)?)?;
- // println!("{:?}", tag);
+ let open_read = || File::open(&options.file);
+ let open_write = || OpenOptions::new().write(true).open(&options.file);
+
+ let mut tag = {
+ let (tag, tag_v1) = {
+ let mut file = open_read()?;
+ let tag = ID3v2::from_stream(&mut file)?;
+ let tag_v1 = id3v1::recognise(&mut file)?;
+ (tag, tag_v1)
+ };
+
+ if options.remove_v1 {
+ if let Some(tag_type) = tag_v1 {
+ id3v1::remove_tag(&mut open_write()?, tag_type)?;
+ } else {
+ eprintln!("WARNING: Told to remove ID3v1 tag while none is present.");
+ }
+ } else {
+ if let Some(tag_type) = tag_v1 {
+ let descr = match tag_type {
+ id3v1::TagType::TAGv10 => "ID3v1.0",
+ id3v1::TagType::TAGv11 => "ID3v1.1",
+ id3v1::TagType::TAGv12 => "ID3v1.2",
+ };
+ eprintln!("WARNING: {} tag found at end of file; use --remove-v1 to remove it.", descr);
+ }
+ }
+
+ tag
+ };
if options.latin1_as_utf8 {
for frame in &mut tag.frames {
@@ -99,19 +132,18 @@ fn main() -> io::Result<()> {
}
}
- if let Some(s) = options.set_tags.album { modify_tag(&mut tag, Frame::TALB(s))?; }
- if let Some(s) = options.set_tags.artist { modify_tag(&mut tag, Frame::TPE1(s))?; }
- if let Some(s) = options.set_tags.title { modify_tag(&mut tag, Frame::TIT2(s))?; }
- if let Some(s) = options.set_tags.track { modify_tag(&mut tag, Frame::TRCK(s))?; }
- if let Some(s) = options.set_tags.year { modify_tag(&mut tag, Frame::TYER(s))?; }
+ if let Some(s) = &options.set_tags.album { modify_tag(&mut tag, Frame::TALB(s.clone()))?; }
+ if let Some(s) = &options.set_tags.artist { modify_tag(&mut tag, Frame::TPE1(s.clone()))?; }
+ if let Some(s) = &options.set_tags.title { modify_tag(&mut tag, Frame::TIT2(s.clone()))?; }
+ if let Some(s) = &options.set_tags.track { modify_tag(&mut tag, Frame::TRCK(s.clone()))?; }
+ if let Some(s) = &options.set_tags.year { modify_tag(&mut tag, Frame::TYER(s.clone()))?; }
print_tag(&tag)?;
// TODO: if -w, then write tags to file (if it fits)
if options.write {
let encoded = tag.encode()?;
- let mut f = OpenOptions::new().write(true).open(&options.file)?;
- f.write_all(&encoded)?;
+ open_write()?.write_all(&encoded)?;
println!("Tag written!");
}