2

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).

fzwo
  • 9,842
  • 3
  • 37
  • 57
  • have a look at http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header part 2.3 describes about VBR headers,I tested it against the files from your link and it detected CBR and VBR files correctly. – user3473830 Sep 22 '14 at 10:00

2 Answers2

2

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.

ioctlLR
  • 1,217
  • 6
  • 13
  • Thank you for your answer! Would this work for both mp3 and mpeg4 files? I must admit, I was hoping for more concrete code samples, but apparently, this doesn't seem to be an important question for many people. – fzwo Sep 16 '14 at 07:30
  • I haven't even looked at the MPEG4 container format (looks like Part 14 of the standard). In principle I think it would work, but you'll have to find code to read the framing info. For example code, the decoder I work on is https://nlayer.codeplex.com/. MpegStreamReader.cs has the relevant code (with calls into other types). – ioctlLR Sep 16 '14 at 15:49
  • Since you were the only one to answer, and your answer is somewhat helpful (though not really what I was looking for), I'm awarding you the bounty. Thank you for taking the time to answer! – fzwo Sep 22 '14 at 12:59
0

In VBR mp3 first frame is different. So one doesn't need to read the whole file to find out if it is VBR or not.

jayarjo
  • 16,124
  • 24
  • 94
  • 138