2015-05-15 11 views
11

Sto cercando un modo per modificare i tag ID3 con Swift. Più in particolare, voglio scrivere l'immagine di Album Art in un file mp3/m4a.Tag ID3 con Swift

Una libreria Swift sarebbe la migliore, ma prendo tutto ciò che può essere fatto in modo nativo in Swift. Non voglio fare affidamento sulla libreria di un'altra lingua.

Ho avuto una rapida occhiata a AVFoundation, ma sembra che sia solo per la riproduzione e conversione audio/video. Questo è il più vicino che ho trovato dai tag ID3: https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsset_Class/

Qualche suggerimento?

+1

Questo potrebbe essere utile per m4a: http://stackoverflow.com/questions/33582845/writing-id3-tags-via-avmetadataitem – Moritz

+0

@EricD. Grazie! Non avevo sentito parlare di AvMetaData. Darò un'occhiata. – LaX

+0

Possibile duplicato http://stackoverflow.com/q/33582845/ – Richard

risposta

9

Mi trovavo di fronte a questo stesso problema ancora e ancora, quindi ho deciso di creare un quadro veloce per questo. Lo si può trovare qui: https://github.com/philiphardy/ID3Edit

Aggiungi nel progetto Xcode e quindi assicurarsi di incorporare andando in impostazioni del progetto> Generali> binari embedded

Ecco come implementarlo nel codice:

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

Qualche suggerimento su ciò che hai usato per raggiungere questo obiettivo? – LaX

+0

Ho appena usato la struttura di base. Analizzo e scrivo manualmente il tag ID3 in base alle specifiche qui: http://id3.org/id3v2-00 Ho caricato il mio codice su github. Ora dovresti essere in grado di vederlo se segui il link –

+0

Questo è fantastico! Grazie, Philip! –

Problemi correlati