Reputation: 107
Swift How do you edit the Metadata (ID3) of a .mp3 file in macOS
I am writing a macOS app and have been trying for some while to change the wording in section of ‘More Info’ in an .mp3 and have done much searching of SO and found 2 snippets of code that will read the metadata but only one actually outputs the existing keys but I don’t understand how to write new data to any of them. I actually want to write data to a .mp3 file I have created and add an image if possible, as a newbie with some knowledge in Swift 3 can anybody help please. The output below is from a test song (Imagine_Test_Song) I have copied to my desktop.
I found this on SO Writing ID3 tags via AVMetaDataItem but I get a compiling error in these lines :-
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeyArtist, "MyArtist")!) // compiler error here
soundFileMetadata.append(createMetadata(AVMetadataiTunesMetadataKeySongName, "MySong")!)
….
which says :- Missing argument label 'tagKey' in call. The func is this :-
func createMetadata(tagKey: String, _ tagValue: AnyObject?,
keySpace:String = AVMetadataKeySpaceiTunes) -> AVMutableMetadataItem? {
if let tagValue = tagValue {
let tag = AVMutableMetadataItem()
tag.keySpace = keySpace
tag.key = tagKey as NSCopying & NSObjectProtocol
tag.value = (tagValue as? String as! NSCopying & NSObjectProtocol) ?? (tagValue as? Int as! NSCopying & NSObjectProtocol)
return tag
}
return nil
}
Second snippet is my code below which does compile and outputs the various data but how do you edit the text and save the changes. Ideally I would also like to add “artwork” as well, is this possible?
let homeUrl = NSHomeDirectory()
let sourceFilePath = String(format: "%@/Desktop/%@.mp3", homeUrl, " Imagine_Test_Song")
let fileUrl = NSURL(fileURLWithPath: sourceFilePath)
var asset = AVAsset(url: fileUrl as URL) as AVAsset
//using the asset property to get the metadata of file
for metaDataItems in asset.commonMetadata {
//getting the title of the song
if metaDataItems.commonKey == "title" {
let titleData = metaDataItems.value as! NSString
print("title = \(titleData)")
}
//getting the "Artist of the mp3 file"
if metaDataItems.commonKey == "artist" {
let artistData = metaDataItems.value as! NSString
print("artist = \(artistData)")
}
//getting the "creator of the mp3 file"
if metaDataItems.commonKey == "creator" {
let creatorData = metaDataItems.value as! NSString
print("creator = \(creatorData)")
}
//getting the "Album of the mp3 file"
if metaDataItems.commonKey == "albumName" {
let albumNameData = metaDataItems.value as! NSString
print("albumName = \(albumNameData)")
}
Output :-
title = Imagine
creator = John Lennon
type = Singer-Songwriter
albumName = Imagine
Any help would be appreciated, thanks in advance.
Upvotes: 2
Views: 1997
Reputation: 1269
After a lot of research on this issue, it seems the ability to modify and save AVMetaDataItems to an MP3 file is impossible using the AVFoundation API calls (even though the mp3 file type is listed as an option) due to licensing issues with MP3 files. So, the only way to do this, as far as I know, is to either roll your own id3 tag editor, or use a library/framework from another author.
I do not personally like using frameworks or libraries from other authors that do not provide the full source code because I have run into problems when the author stops maintaining the project and Apple changes something and the change breaks my apps.
However, I have found ID3TagEditor library by Fabrizio Duroni. Full source is available on github here. It's a straight forward and easy to use library. Fabrizio is actively maintaining the code as of this writing.
Upvotes: 2