Skip to content

Instantly share code, notes, and snippets.

@WFT
Last active November 6, 2024 20:17
Show Gist options
  • Save WFT/eb72124bf69e63fd5e304cfb4af89ee2 to your computer and use it in GitHub Desktop.
Save WFT/eb72124bf69e63fd5e304cfb4af89ee2 to your computer and use it in GitHub Desktop.
Modify CAF audio file metadata with Core Audio / AudioToolbox
import AudioToolbox
import Foundation
func check(_ status: OSStatus, line: Int = #line) {
if status != noErr {
fatalError("Error on line \(line): OSStatus \(status) - https://osstatus.com/search/results?search=\(status)")
}
}
func openAudioFile(at fileURL: URL) -> AudioFileID {
var audioFileMaybe: AudioFileID?
check(
AudioFileOpenURL(
fileURL as CFURL,
.readWritePermission,
kAudioFileCAFType, // Could also pass 0 to let it guess.
&audioFileMaybe
)
)
return audioFileMaybe!
}
func closeAudioFile(_ audioFile: AudioFileID) {
check(
AudioFileClose(audioFile)
)
}
func getInfoDictionary(from audioFile: AudioFileID) -> NSMutableDictionary {
var infoDictionaryOut: CFDictionary?
var infoDictionarySize = UInt32(MemoryLayout<CFDictionary>.size)
withUnsafeMutablePointer(to: &infoDictionaryOut) {
check(
AudioFileGetProperty(
audioFile,
kAudioFilePropertyInfoDictionary,
&infoDictionarySize,
.init($0)
)
)
}
precondition(infoDictionarySize == UInt32(MemoryLayout<CFDictionary>.size), "This size shouldn't change.")
return (infoDictionaryOut! as NSDictionary).mutableCopy() as! NSMutableDictionary
}
func setInfoDictionary(_ infoDictionary: NSDictionary, on audioFile: AudioFileID) {
let infoDictionarySize = UInt32(MemoryLayout<CFDictionary>.size)
var infoDictionaryIn = infoDictionary as CFDictionary
withUnsafeMutablePointer(to: &infoDictionaryIn) {
check(
AudioFileSetProperty(
audioFile,
kAudioFilePropertyInfoDictionary,
infoDictionarySize,
$0
)
)
}
}
// The program:
guard CommandLine.arguments.count == 2 else {
fatalError("\(CommandLine.arguments[0]) file-name.caf")
}
// Open the file
let fileURL = URL(fileURLWithPath: CommandLine.arguments[1])
let audioFile = openAudioFile(at: fileURL)
// Don't forget to schedule the close
defer { closeAudioFile(audioFile) }
// Get the original info
let infoDictionary = getInfoDictionary(from: audioFile)
print("Initial info dictionary:")
print(infoDictionary)
// Mutate the info
// Search "Info String Keys" in AudioToolbox/AudioFile.h for more options
infoDictionary[kAFInfoDictionary_Artist] = "My Excellent User"
print("New info dictionary:")
print(infoDictionary)
// Set the info
setInfoDictionary(infoDictionary, on: audioFile)
/*
MIT License
Copyright (c) 2024 William Field-Thompson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
@WFT
Copy link
Author

WFT commented Nov 5, 2024

Other good keys include:

  • kAFInfoDictionary_Title
  • kAFInfoDictionary_Composer

@WFT
Copy link
Author

WFT commented Nov 5, 2024

You can run it like so:

swift metadata.swift myfile.caf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment