Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get FLAC file metadata in Swift on iOS 11?

Tags:

ios

swift

flac

I need to get metadata of an FLAC file. I tried following code:

let item = AVPlayerItem(url: URL(fileURLWithPath: path))
    let commonMetadata = item.asset.commonMetadata

    songInfo[ARTIST_NAME] = "Unknown"
    songInfo[GENRE_NAME] = "Unknown"
    songInfo[ALBUM_NAME] = "Unknown"
    songInfo[PLAY_COUNT] = "0"

    for metadataItem in commonMetadata {
        switch metadataItem.commonKey?.rawValue ?? "" {
        case "type":
            songInfo[GENRE_NAME] = metadataItem.stringValue
        case "albumName":
            songInfo[ALBUM_NAME]  = metadataItem.stringValue
        case "artist":
            songInfo[ARTIST_NAME] = metadataItem.stringValue
        default: break
        }
    } 

But this is not working for a FLAC file. Any help will be appreciated.

like image 751
Return Zero Avatar asked Sep 11 '18 13:09

Return Zero


People also ask

Do FLAC files have metadata?

Metadata in FLAC precedes the audio. Properties like the sample rate and the number of channels are always contained in the metadata. It may also contain other information, the album cover for example. FLAC uses Vorbis comments for some types of metadata, like the title and artist name.

Do FLAC files have tags?

What kinds of tags does FLAC support? FLAC has it's own native tagging system which is identical to that of Vorbis. They are called alternately "FLAC tags" and "Vorbis comments". It is the only tagging system required and guaranteed to be supported by FLAC implementations.

Can iPhone play FLAC?

Let's start with what the iPhone can do out of the box. According to Apple's own specs, it can play MP3, AAC, ALAC, WAV and AIFF audio files. The iPhone also supports FLAC files, but only through Apple's Files app. This was introduced as part of iOS11, which launched in 2017.


1 Answers

Just use AudioToolbox API:

func audioFileInfo(url: URL) -> NSDictionary? {
    var fileID: AudioFileID? = nil
    var status:OSStatus = AudioFileOpenURL(url as CFURL, .readPermission, kAudioFileFLACType, &fileID)

    guard status == noErr else { return nil }

    var dict: CFDictionary? = nil
    var dataSize = UInt32(MemoryLayout<CFDictionary?>.size(ofValue: dict))

    guard let audioFile = fileID else { return nil }

    status = AudioFileGetProperty(audioFile, kAudioFilePropertyInfoDictionary, &dataSize, &dict)

    guard status == noErr else { return nil }

    AudioFileClose(audioFile)

    guard let cfDict = dict else { return nil }

    let tagsDict = NSDictionary.init(dictionary: cfDict)

    return tagsDict
}

Example output:

- 0 : 2 elements
    * key : artist
    * value : Blue Monday FM
- 1 : 2 elements
    * key : title
    * value : Bee Moved
- 2 : 2 elements
    * key : album
    * value : Bee Moved
- 3 : 2 elements
    * key : approximate duration in seconds
    * value : 39.876
- 4 : 2 elements
    * key : source encoder
    * value : reference libFLAC 1.2.1 win64 200807090
like image 152
mijokaliger Avatar answered Oct 23 '22 14:10

mijokaliger