Skip to content

Instantly share code, notes, and snippets.

@natesymer
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natesymer/c5b16672981d5fb8072e to your computer and use it in GitHub Desktop.
Save natesymer/c5b16672981d5fb8072e to your computer and use it in GitHub Desktop.
retrieve metadata from mp3s
#import <Foundation/Foundation.h>
@interface metadataRetriever : NSObject
+ (NSDictionary *)metadataForFile:(NSString *)filePath;
+ (NSData *)id3TagForFile:(NSString *)filePath;
@end
#import "metadataRetriever.h"
#import <CoreFoundation/CoreFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
static NSString * const kRaiseMessage = @"Failed to read ID3 tag.";
@implementation metadataRetriever
+ (AudioFileID)openFile:(NSString *)path {
NSURL *fileURL = [NSURL fileURLWithPath:path];
AudioFileID fileID = nil;
OSStatus err = noErr;
err = AudioFileOpenURL((__bridge CFURLRef)fileURL, kAudioFileReadPermission, 0, &fileID);
if (err != noErr) [NSException raise:kRaiseMessage format:@"Failed to open audio file."];
return fileID;
}
+ (NSDictionary *)metadataForFile:(NSString *)filePath {
AudioFileID fileID = [self openFile:filePath];
CFDictionaryRef piDict = nil;
UInt32 piDataSize = 0;
OSStatus err = AudioFileGetProperty(fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict);
if (err != noErr) [NSException raise:kRaiseMessage format:@"Failed to read info dictionary."];
return (__bridge NSDictionary *)piDict;
}
+ (NSData *)id3TagForFile:(NSString *)filePath {
AudioFileID fileID = [self openFile:filePath];
UInt32 id3DataSize = 0;
OSStatus err;
err = AudioFileGetPropertyInfo(fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL);
if (err != noErr) [NSException raise:kRaiseMessage format:@"Failed to read tag size."];
char *rawID3Tag = (char *)malloc(id3DataSize*sizeof(char));
err = AudioFileGetProperty(fileID, kAudioFilePropertyID3Tag, &id3DataSize, rawID3Tag);
if (err != noErr) [NSException raise:kRaiseMessage format:@"Failed to read raw ID3 tag."];
UInt32 id3TagSize = 0;
UInt32 id3TagSizeLength = 0;
err = AudioFormatGetProperty(kAudioFormatProperty_ID3TagSize, id3DataSize, rawID3Tag, &id3TagSizeLength, &id3TagSize);
if (err != noErr) {
switch (err) {
case kAudioFormatUnspecifiedError:
[NSException raise:kRaiseMessage format:@"Unspecified error."];
break;
case kAudioFormatUnsupportedPropertyError:
[NSException raise:kRaiseMessage format:@"Unsupported property."];
break;
case kAudioFormatBadPropertySizeError:
[NSException raise:kRaiseMessage format:@"Bad property size."];
break;
case kAudioFormatBadSpecifierSizeError:
[NSException raise:kRaiseMessage format:@"Bad specifier size"];
break;
case kAudioFormatUnsupportedDataFormatError:
[NSException raise:kRaiseMessage format:@"Unsupported data."];
break;
case kAudioFormatUnknownFormatError:
[NSException raise:kRaiseMessage format:@"Unkown audio format."];
break;
default:
[NSException raise:kRaiseMessage format:@"Unknown error."];
break;
}
}
return [NSData dataWithBytesNoCopy:rawID3Tag length:id3DataSize freeWhenDone:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment