ID3TagEditor
A swift library to read and modify ID3 Tag of any mp3 file.
Installation
There are three ways to install ID3TagEditor in your project: manual installation, as a stand-alone framework or using cocoapods.
Manual installation
To manually install ID3TagEditor simply drag and drop all the file contained in the Source folder inside your project (except for the info.plist file).
Framework
ID3TagEditor is also available as a framework. You can follow the standard procedure to install a custom cocoa touch framework (simply drag the ID3TagEditor.xcodeproj inside your project and add it to the Embedded Binaries/Linked Frameworks and Libraries section of your project. See the demo project for a complete example of the setup of the framework.
CocoaPods
ID3TagEditor is also available as a pod on CocoaPods. Add the dependency to your Podfile (choose the release version you prefer):
target 'MyApp' do
pod 'ID3TagEditor', '~> 2.0'
end
and then run pod install (or pod update).
Usage
ID3Tag editor is compatible with the following platforms:
- iOS
- MacOS
- Apple Watch
- Apple TV
To read the ID3 tag of an mp3 file you can choose between two api contained in the ID3TagEditor
class:
public func read(from path: String) throws -> ID3Tag?
public func read(mp3: Data) throws -> ID3Tag?
Below you can find a sample code of the api usage.
do {
let id3TagEditor = ID3TagEditor()
if let id3Tag = try id3TagEditor.read(from: "<valid path to the mp3 file>") {
...use the tag...
}
if let id3Tag = try id3TagEditor.read(mp3: "<valid mp3 file passed as Data>") {
...use the tag...
}
} catch {
print(error)
}
To write a new ID3 tag into an mp3 file you can choose between two api contained in the ID3TagEditor
class:
public func write(tag: ID3Tag, to path: String, andSaveTo newPath: String? = nil) throws
public func write(tag: ID3Tag, mp3: Data) throws -> Data
Below you can find a sample code of the api usage.
do {
let id3Tag = ID3Tag(
version: .version3,
artist: "an example artist",
albumArtist: "an example album artist",
album: "an example album",
title: "an example title",
recordingDateTime: RecordingDateTime(date: RecordingDate(day: 1, month: 10, year: 2019),
time: RecordingTime(hour: 14, minute: 30)),
genre: Genre(genre: .ClassicRock, description: "Rock & Roll"),
attachedPictures: AttachedPicture(picture: <NSData/Data of the image>, type: .FrontCover, format: .Jpeg),
trackPosition: TrackPositionInSet(position: 2, totalTracks: 9)
)
try id3TagEditor.write(tag: id3Tag, to: "<valid path to the mp3 file that will be overwritten>"))
try id3TagEditor.write(tag: id3Tag,
to: "<valid path to the mp3 file>",
andSaveTo: "<new path where you want to save the mp3>"))
let newMp3: Data = try id3TagEditor.write(tag: id3Tag, mp3: <valid mp3 file passed as Data>)
} catch {
print(error)
}
The above methods use the ID3Tag
class to describe a valid ID3 tag. This class contains various properties that could be
used to read/write a tag to the mp3 file.
Three versions of the tag are supported. They are described in the ID3Version
enum:
Version
version 2.2, described by the enum value.version2
Version
version 2.3, described by the enum value.version3
Version
version 2.4, described by the enum value.version4
The ID3 supported properties are:
version
, as previously describedartist
, as a string containing the name of the song’s artistsalbumArtist
, as a string containing additional info about the artistsalbum
, as a string containing the album titletitle
, as a string containing the title of the songrecordingDateTime
, as an object composed by two other properties:date
as aRecordingDate
object with three fields:day
, as a number that represents the recording daymonth
, as a number that represents the recording monthyear
, as a number that represents the recording year
time
as aRecordingTime
object with two fields:hour
, as a number that represents the recording hourminute
, as a number that represents the recording minute
trackPosition
, as aTrackPositionInSet
object containing the position of the track in the recording and the total number of track in the recordingsgenre
, as aGenre
object containing theID3Genre
identifier and/or adescription
of the song’s genreattachedPictures
, as an array ofAttachedPicture
objects containing theData
of an image, theID3PictureType
and theID3PictureFormat
Only the version
field is mandatory. The other fields are optional.
The field artist
, albumArtist
, title
and album
are encoded/saved using Unicode 16 bit string (as requested by specification).
The library is also able to read text frame wrongly encoded with Unicode (for example recordingDateTime must always be a ISO88591 string).
Documentation
You can find the complete api documentation on fabrizioduroni.it.
Examples
In the following screenshots you can find examples of the data extracted/updated. In the demo project you will find an example for each supported target.