我正在寻找一种使用Swift修改ID3标签的方法。更具体地说,我想将Album Art图像写入mp3 / m4a文件。
Swift库将是最好的,但我将采取任何可以在Swift本地完成的东西。我不想依赖另一种语言的库。
我快速浏览了AVFoundation,但看起来它只适用于音频/视频播放和转换。这是我从ID3标签找到的最近的:https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/
有什么建议吗?
答案 0 :(得分:10)
我一遍又一遍地遇到同样的问题所以我决定为它制定一个快速的框架。您可以在此处找到它:https://github.com/philiphardy/ID3Edit
将其添加到您的Xcode项目中,然后确保通过转到您的项目设置来嵌入它>一般>嵌入式二进制文件
以下是如何在代码中实现它:
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 {}