In my experience, the only things that is clear are that ID3v1
must be at the very end of the file. The rest is a whole mess of concurrent standards attempting to improve upon ID3v1, and since they all consider themselves the ultimate tagging standard there's usually no official documentation on what to do if there are, for example, 3 concurrent tags. The order in which multiple tags should appear is roughly related to the order in which they were invented:
- ID3v1
- Lyrics3
- ID3v2
- APE
So ID3v1 must come at the end of the file, always.
Lyrics3 must come immediate before ID3v1, and if no ID3v1 was present then a new one must be created - Lyrics3 can never appear alone at the end of the file. Any tags between Lyrics3 and ID3v1 will break most (if not all) Lyrics3-capable software.
APE appears near the end of the file somewhere, and because APE is the most recent tagging standard, most implementations are pretty flexible, so the APEtag can usually appear at EOF, before ID3v1, or before Lyrics3+ID3v1.
ID3v2 usually stays out of the mess by being at the beginning of the file, but if it's appended then as
madah said it'd probably be best to stick it immediately after audio data and before APE/Lyrics3/ID3v1. Few (if any?) tag reading/writing programs (other than Frontah) support appended ID3v2.4 anyhow, so it's unlikely to be much of an issue.
QUOTE(Sebastian Mares)
Should I keep reading the file until I detected all ID3v2/APE/Lyrics3 tags, or should I quit the parsing after finding one ID3v2/APE/Lyrics3 tag?
The best thing would be to extract all possible tags, and then decide (either your software or the user) what data should be kept, discarded, etc.
QUOTE(Sebastian Mares)
Also, what should I do when there are more ID3v1 tags? I know that iTunes has this bug (writes an additional ID3v1 tag if modifying an ID3v2 tag).
Take the data from the one at the end of the file. Ignore any/all that come before. Flag the file as broken. Better yet, remove the last ID3v1 tag, and overwrite the second-last ID3v1 with the correct data. Repeat until there's only one ID3v1 tag present, at which point you'll be able to detect any additional (Lyrics3/APE/ID3v2.4) footer tags, if present.