Search code examples
objective-cmacoscocoaaudiomp3

Determine if an audio file is VBR


I'm trying to determine whether an audio file uses VBR or not in a Cocoa app (OS X 10.8+).

AVFoundation doesn't seem to be able to answer the question at all, and AudioToolbox is lying to me.

The code below adamantly claims that any mp3 file I throw at it is VBR; even for files where I know that isn't the case (cross-checked with MediaInfo)

OSStatus result = noErr;
UInt32 size;

AudioFileID audioFile;
AudioStreamBasicDescription audioFormat;
AudioFormatPropertyID vbrInfo;

// Open audio file.
result = AudioFileOpenURL( (__bridge CFURLRef)originalURL, kAudioFileReadPermission, 0, &audioFile );
if( result != noErr )
{
    NSLog( @"Error in AudioFileOpenURL: %d", (int)result );
    return;
}

// Get data format
size = sizeof( audioFormat );
result = AudioFileGetProperty( audioFile, kAudioFilePropertyDataFormat, &size, &audioFormat );
if( result != noErr )
{
    NSLog( @"Error in AudioFileGetProperty: %d", (int)result );
    return;
}

// Get vbr info
size = sizeof( vbrInfo );
result = AudioFormatGetProperty( kAudioFormatProperty_FormatIsVBR, sizeof(audioFormat), &audioFormat, &size, &vbrInfo);

if( result != noErr )
{
    NSLog( @"Error getting vbr info: %d", (int)result );
    return;
}

NSLog(@"%@ is VBR: %d", originalURL.lastPathComponent, vbrInfo);

Why is it lying? What am I doing wrong?

How can I realiably determine the VBR-iness of an audio file in a Cocoa Application?

VBR and CBR sample files can be obtained here (not my page).


Solution

  • Your best bet is to read the MPEG frame headers from the file directly. If they all specify the same encoding parameters (version, layer, bitrate, samplerate, & channels), the file is CBR. Otherwise, the file is VBR / ABR.

    There are several MP3 libraries out there that can either be used to do this directly or have the correct code for you to borrow to do it. If the file is on fast storage, performance is also well within your requirements.

    If you need further assistance, I'm a major contributor to a C# MP3 decoder project and can point you to it for some examples.