Подтвердить что ты не робот

Теги ID3 с Swift

Я ищу способ изменить теги ID3 с помощью Swift. В частности, я хочу записать изображение альбома в файл mp3/m4a.

Библиотека Swift была бы лучшей, но я возьму все, что можно сделать изначально в Swift. Я не хочу полагаться на другую языковую библиотеку.

Я быстро просмотрел AVFoundation, но он выглядит только для воспроизведения аудио и видео и преобразования. Это примерно то, что я нашел из тегов ID3: https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/

Любое предложение?

4b9b3361

Ответ 1

Я столкнулся с этой проблемой снова и снова, поэтому решил сделать для нее быстрые рамки. Вы можете найти его здесь: https://github.com/philiphardy/ID3Edit

Добавьте его в свой проект Xcode, а затем включите его, перейдя в настройки вашего проектa > Общие > Встраиваемые двоичные файлы

Вот как реализовать его в вашем коде:

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 {}