问题描述
如何通过AVAssetReader阅读示例?我发现使用AVAssetReader复制或混合的示例,但这些循环总是由AVAssetWriter循环控制。是否可以创建一个AVAssetReader并读取它,获取每个样本?
How do you read samples via AVAssetReader? I've found examples of duplicating or mixing using AVAssetReader, but those loops are always controlled by the AVAssetWriter loop. Is it possible just to create an AVAssetReader and read through it, getting each sample?
谢谢。
推荐答案
从你的问题不清楚你是在谈论视频样本或音频样本。要阅读视频示例,您需要执行以下操作:
It's unclear from your question whether you are talking about video samples or audio samples. To read video samples, you need to do the following:
-
构建AVAssetReader:
Construct an AVAssetReader:
asset_reader = [[AVAssetReader alloc] initWithAsset:asset error:& error];
(错误检查在此处)
从资产获取视频轨道:
NSArray * video_tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack * video_track = [video_tracks objectAtIndex:0];
设置所需的视频帧格式:
Set the desired video frame format:
NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber numberWithInt:<format code from CVPixelBuffer.h>] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
某些视频格式不能正常工作,如果你正在做某些实时性,某些视频格式的效果比其他格式(BGRA比ARGB快)。
Note that certain video formats just will not work, and if you're doing something real-time, certain video formats perform better than others (BGRA is faster than ARGB, for instance).
构建实际轨道输出并将其添加到资产读取器:
Construct the actual track output and add it to the asset reader:
AVAssetReaderTrackOutput* asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:video_track outputSettings:dictionary];
[asset_reader addOutput:asset_reader_output];
启动资产读取器:
Kick off the asset reader:
[asset_reader startReading];
读取样本:
CMSampleBufferRef buffer;
while ( [asset_reader status]==AVAssetReaderStatusReading )
buffer = [asset_reader_output copyNextSampleBuffer];
这篇关于通过AVAssetReader读取样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!