ID3 tags with Swift
Asked Answered
N

1

13

I'm looking for a way to modify ID3 tags with Swift. More specifically, I want to write the Album Art image to an mp3/m4a file.

A Swift library would be the best, but I'll take anything that can be done natively in Swift. I don't want to rely on another language's library.

I had a quick look at AVFoundation, but it looks like it's only for audio/video playback and conversion. This is about the closest I found from ID3 tags: https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/

Any suggestion?

Nickolai answered 15/5, 2015 at 16:4 Comment(4)
This might be helpful for m4a: #33583345Newsreel
@EricD. Thanks! I hadn't heard about AvMetaData. I'll take a look.Nickolai
Possible duplicate https://mcmap.net/q/907487/-writing-id3-tags-via-avmetadataitemAccommodating
@Accommodating Yes, it is a quite similar question, but it was posted a few months after this one.Nickolai
G
11

I was faced with this same problem over and over so I decided to make a swift framework for it. You can find it here: https://github.com/philiphardy/ID3Edit

Add it into your Xcode project and then make sure to embed it by going to your project settings > General > Embedded Binaries

Here is how to implement it in your code:

import ID3Edit
...
do
{
   // Open the file
   let mp3File = try MP3File(path: "/Users/Example/Music/example.mp3")
   // Use MP3File(data: data) data being an NSData object
   // to load an MP3 file from memory
   // NOTE: If you use the MP3File(data: NSData?) initializer make
   //       sure to set the path before calling writeTag() or an
   //       exception will be thrown

   // Get song information
   print("Title:\t\(mp3File.getTitle())")
   print("Artist:\t\(mp3File.getArtist())")
   print("Album:\t\(mp3File.getAlbum())")
   print("Lyrics:\n\(mp3File.getLyrics())")

   let artwork = mp3File.getArtwork()

   // Write song information
   mp3File.setTitle("The new song title")
   mp3File.setArtist("The new artist")
   mp3File.setAlbum("The new album")
   mp3File.setLyrics("Yeah Yeah new lyrics")

   if let newArt = NSImage(contentsOfFile: "/Users/Example/Pictures/example.png")
   {
          mp3File.setArtwork(newArt, isPNG: true)
   }
   else
   {
          print("The artwork referenced does not exist.")
   }

   // Save the information to the mp3 file
   mp3File.writeTag() // or mp3.getMP3Data() returns the NSData
                      // of the mp3 file
}
catch ID3EditErrors.FileDoesNotExist
{
   print("The file does not exist.")
}
catch ID3EditErrors.NotAnMP3
{
   print("The file you attempted to open was not an mp3 file.")
}
catch {}
Gabon answered 9/1, 2016 at 5:29 Comment(3)
Any hint on what you used to achieve this?Nickolai
Just used the foundation framework. I manually parse and write the ID3 tag according to the specifications here: id3.org/id3v2-00 I've uploaded my code to github. You should now be able to see it if you follow the linkGabon
Is this supposed to work with m4a files as well ? In my tests its not working with music I purchased and downloaded from the iTunes Store.Galle

© 2022 - 2024 — McMap. All rights reserved.