saecki / mp4ameta

A library for reading and writing iTunes style MPEG-4 audio metadata
Apache License 2.0
35 stars 5 forks source link

Updating tags corrupts audio files #11

Closed ravicious closed 3 years ago

ravicious commented 3 years ago

I've been trying to use mp4ameta in my side project. I noticed that a simple action of setting the artist and album corrupts the file. It doesn't happen with every m4a file though.

Here's ~a zip file with examples~ (I'll delete the link later). The first two I bought from iTunes and Bandcamp, the third one is from a site with public domain sounds. Run the following code on the three of them and notice that the first two stop being playable, but the third one still works:

let examples = ["example1.m4a", "example2.m4a", "example3.m4a"];

for example in &examples {
    let mut tag = mp4ameta::Tag::read_from_path(example).unwrap();
    tag.set_artist("Foo");
    tag.set_album("Bar");
    tag.write_to_path(example).unwrap()
}

Is this a bug in the library or am I doing something wrong?

saecki commented 3 years ago

Thanks for reporting, I could reproduce your issues and will get to it asap.

saecki commented 3 years ago

The problem is that in the files that get corrupted the mdat atom comes after the movie atom, which contains the metadata and references to the media data in mdat. Changing the size of the metadata should update these references if that's the case but doesn't.

saecki commented 3 years ago

Sorry if this took a while. The latest commit should fix files being corrupted. I will do some cleanup and then publish a new version.

ravicious commented 3 years ago

No problem, thanks for the fix!