【问题标题】:ID3 tags with SwiftSwift 的 ID3 标签
【发布时间】:2015-07-27 15:25:06
【问题描述】:

我正在寻找一种使用 Swift 修改 ID3 标签的方法。更具体地说,我想将专辑封面图像写入 mp3/m4a 文件。

一个 Swift 库是最好的,但我会接受任何可以在 Swift 中本地完成的事情。我不想依赖另一种语言的库。

我快速浏览了 AVFoundation,但它看起来只是用于音频/视频播放和转换。这是我从 ID3 标签中找到的最接近的:https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/

有什么建议吗?

【问题讨论】:

标签: swift metadata mp3 id3 m4a


【解决方案1】:

我一遍又一遍地面临同样的问题,所以我决定为它制作一个快速的框架。你可以在这里找到它: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 {}

【讨论】:

  • 您有什么提示可以做到这一点吗?
  • 刚刚使用了基础框架。我根据这里的规范手动解析和编写ID3标签:id3.org/id3v2-00我已经将我的代码上传到了github。如果您点击链接,您现在应该可以看到它
  • 这太棒了!谢谢,菲利普!
  • 这是否也适用于 m4a 文件?在我的测试中,它不适用于我从 iTunes Store 购买和下载的音乐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2012-05-03
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多