问题
我希望从视频文件中的音轨中提取 LPCM 音频的样本准确范围。目前,我希望AVAssetReaderTrackOutput
通过AVAssetTrack
阅读AVURLAsset
.
尽管准备并确保使用AVURLAssetPreferPreciseDurationAndTimingKey
set to初始化资产,但在资产中YES
寻找样本准确的位置似乎是不准确的。
NSDictionary *options = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @(YES) };
_asset = [[AVURLAsset alloc] initWithURL:fileURL options:options];
这表现为例如可变比特率编码的 AAC 流。虽然我知道 VBR 音频流在准确搜索方面会带来性能开销,但我愿意支付这笔费用,前提是我收到了准确的样本。
当使用例如扩展音频文件服务和ExtAudioFileRef
API 时,我可以实现样本精确的音频搜索和提取。与 , 类似AVAudioFile
,因为它建立在ExtAudioFileRef
.
然而,问题是我还想从仅音频文件 API 拒绝但在AVFoundation 中通过AVURLAsset
.
方法
CMTime
使用和定义提取的样本准确时间范围CMTimeRange
,并在 上设置AVAssetReaderTrackOutput
。然后迭代地提取样本。
-(NSData *)readFromFrame:(SInt64)startFrame
requestedFrameCount:(UInt32)frameCount
{
NSUInteger expectedByteCount = frameCount * _bytesPerFrame;
NSMutableData *data = [NSMutableData dataWithCapacity:expectedByteCount];
//
// Configure Output
//
NSDictionary *settings = @{ AVFormatIDKey : @( kAudioFormatLinearPCM ),
AVLinearPCMIsNonInterleaved : @( NO ),
AVLinearPCMIsBigEndianKey : @( NO ),
AVLinearPCMIsFloatKey : @( YES ),
AVLinearPCMBitDepthKey : @( 32 ),
AVNumberOfChannelsKey : @( 2 ) };
AVAssetReaderOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:_track outputSettings:settings];
CMTime startTime = CMTimeMake( startFrame, _sampleRate );
CMTime durationTime = CMTimeMake( frameCount, _sampleRate );
CMTimeRange range = CMTimeRangeMake( startTime, durationTime );
//
// Configure Reader
//
NSError *error = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:_asset error:&error];
if( !reader )
{
fprintf( stderr, "avf : failed to initialize reader\n" );
fprintf( stderr, "avf : %s\n%s\n", error.localizedDescription.UTF8String, error.localizedFailureReason.UTF8String );
exit( EXIT_FAILURE );
}
[reader addOutput:output];
[reader setTimeRange:range];
BOOL startOK = [reader startReading];
NSAssert( startOK && reader.status == AVAssetReaderStatusReading, @"Ensure we've started reading." );
NSAssert( _asset.providesPreciseDurationAndTiming, @"We expect the asset to provide accurate timing." );
//
// Start reading samples
//
CMSampleBufferRef sample = NULL;
while(( sample = [output copyNextSampleBuffer] ))
{
CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp( sample );
if( data.length == 0 )
{
// First read - we should be at the expected presentation time requested.
int32_t comparisonResult = CMTimeCompare( presentationTime, startTime );
NSAssert( comparisonResult == 0, @"We expect sample accurate seeking" );
}
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer( sample );
if( !buffer )
{
fprintf( stderr, "avf : failed to obtain buffer" );
exit( EXIT_FAILURE );
}
size_t lengthAtOffset = 0;
size_t totalLength = 0;
char *bufferData = NULL;
if( CMBlockBufferGetDataPointer( buffer, 0, &lengthAtOffset, &totalLength, &bufferData ) != kCMBlockBufferNoErr )
{
fprintf( stderr, "avf : failed to get sample\n" );
exit( EXIT_FAILURE );
}
if( bufferData && lengthAtOffset )
{
[data appendBytes:bufferData length:lengthAtOffset];
}
CFRelease( sample );
}
NSAssert( reader.status == AVAssetReaderStatusCompleted, @"Completed reading" );
[output release];
[reader release];
return [NSData dataWithData:data];
}
笔记
给我的演示时间CMSampleBufferGetPresentationTimeStamp
似乎与我所追求的相匹配——但由于它似乎不准确,所以我没有机会更正和对齐我检索到的样本。
关于如何做到这一点的任何想法?
或者,有没有办法适应orAVAssetTrack
使用?AVAudioFile
ExtAudioFile
是否可以通过 访问音轨AudioFileOpenWithCallbacks
?
是否可以在 macOS 中以不同的方式从视频容器中获取音频流?