9

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.

Alex
  • 5,565
  • 6
  • 36
  • 57
Return Zero
  • 424
  • 4
  • 15

2 Answers2

8

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
manikal
  • 953
  • 7
  • 30
  • tried with your code . in my case i get only "approximate duration in seconds" . – Return Zero Jan 13 '19 at 07:38
  • Are you sure your flac file contains metadata that you want? I've used first google [result](https://helpguide.sony.net/high-res/sample1/v1/data/Sample_BeeMoved_96kHz24bit.flac.zip) for the example code. Here's exiftool [output](https://www.dropbox.com/s/g7lo5c8n0z5vvl1/Screenshot%202019-01-13%20at%2012.55.51.png?dl=0) for that file. – manikal Jan 13 '19 at 11:59
  • yes the file i used contains metadata. you can download from this link https://drive.google.com/file/d/1bLcKaIG5DmOT_RfzgtOKy8tgCjdGUlBx/view?usp=sharing . thanks – Return Zero Jan 13 '19 at 12:54
  • The metadata you are looking for is written in [VORBIS_COMMENT](https://en.wikipedia.org/wiki/Vorbis_comment), I'll update the answer If AudiToolbox API knows how to parse it. – manikal Jan 13 '19 at 15:09
  • It's weird, the example file I used also has metadata written as VORBIS_COMMENT [here](https://www.dropbox.com/s/k8f52erbu89t1qs/bee.png?dl=0) as [metadata](https://www.dropbox.com/s/4sxenmcjhgyc1ui/ennio.png?dl=0) of your output. – manikal Jan 13 '19 at 15:53
  • @mijokaliger do you know how to write FLAC file metadata. As the above code for reading metadata. But I want to *WRITE FLAC* file metadata. – Hardip Kalola Jan 17 '19 at 06:12
  • anyone know how to read the Vorbis comments? – malhal Aug 31 '23 at 21:47
0

In my case I have used https://github.com/CodeEagle/APlay. You can write this code to get metadata .

let mediaPlayer = APlay()
mediaPlayer.play("local_document_path/file.flac")
print(a.metadatas)

// output

[APlay.MetadataParser.Item.year("2009"), APlay.MetadataParser.Item.album("Music From Braid"), APlay.MetadataParser.Item.track("03"), APlay.MetadataParser.Item.title("Lullaby Set"), APlay.MetadataParser.Item.artist("Kammen & Swan"), APlay.MetadataParser.Item.genre("Folk, World, & Country/Stage & Screen/Soundtrack")]
Krishna Kumar Thakur
  • 1,456
  • 12
  • 27
  • thanks for your answer @Krishna. I tried your given solution. In my case it able to find only Song Name , Year , And track Number . but i need artist name , album name , genre .I also need metadata without playing song. – Return Zero Dec 31 '18 at 07:29