Skip to content

Instantly share code, notes, and snippets.

@rfistman
Last active March 29, 2018 07:15
Show Gist options
  • Save rfistman/aa19b6d9b86da582fa85 to your computer and use it in GitHub Desktop.
Save rfistman/aa19b6d9b86da582fa85 to your computer and use it in GitHub Desktop.
Trying to get bitrate of mp3/aac packetized audio with AVFoundation/CoreMedia
// Was hoping to find at least bitrate in packet data, maybe in attachments
// via AVFoundation/CoreMedia. This data is in every mp3 and (I think) aac packet
// yet it seems to be discarded. Anyone know where I can find it without parsing
// by hand or inferring from file duration and size?
// sample rate and number of channels is available.
NSString *path = @"path to mp3/aac/whatevs";
AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
[asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
CMTimeShow(asset.duration);
}];
AVAssetTrack *track = [asset tracksWithMediaType:AVMediaTypeAudio][0];
NSLog(@"track formatDescriptions: %@", track.formatDescriptions);
NSLog(@"estimatedDataRate: %f", track.estimatedDataRate);
NSLog(@"len: %lli", track.totalSampleDataLength);
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:nil];
AVAssetReaderTrackOutput *trackOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:nil];
[reader addOutput:trackOutput];
[reader startReading];
CMSampleBufferRef sampleBuffer = [trackOutput copyNextSampleBuffer];
CMAudioFormatDescriptionRef audioFormat = CMSampleBufferGetFormatDescription(sampleBuffer);
NSLog(@"CM: %@, %@", sampleBuffer, audioFormat);
CFArrayRef attachments = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, false);
NSLog(@"foo %@", attachments);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment