繁体   English   中英

如何在辅助线程上以与主线程相同的速率运行AVAssetReader?

[英]How do I run AVAssetReader at the same rate on a secondary thread as it does on the main thread?

我有一个OpenGL iOS应用程序,正在使用AVAssetReader从视频中读取帧并使用视频内容构造一个简单的模型。

一切都按预期工作,除了我在辅助线程上使用AVAssetReader并在主线程上绑定了纹理。 问题在于视频正在逐帧运行,速度非常慢。

有什么方法可以将视频位置与当前时间同步,以使其看起来好像不是在慢动作?

额外信息

这是我(在辅助线程上)阅读视频的方式:

NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSString* fullPath = [[documentsDirectory stringByAppendingPathComponent:@"sample_video.mp4"] retain];
NSString *urlAddress = fullPath;
NSURL *url = [NSURL fileURLWithPath:urlAddress];
AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *tracks = [urlAsset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *track = [tracks objectAtIndex:0];
NSDictionary* trackDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] 
                                                            forKey:(id)kCVPixelBufferPixelFormatTypeKey];

AVAssetReaderTrackOutput *asset_reader_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:track outputSettings:trackDictionary];

AVAssetReader *asset_reader = [[AVAssetReader alloc] initWithAsset:urlAsset error:nil];
[asset_reader addOutput:asset_reader_output];
[asset_reader startReading];

while (asset_reader.status == AVAssetReaderStatusReading){

    CMSampleBufferRef sampleBufferRef = [asset_reader_output copyNextSampleBuffer];

    if (sampleBufferRef){
            [self performSelectorOnMainThread:@selector(bindNewFrame) withObject:nil waitUntilDone:YES];
    }
    CMSampleBufferInvalidate(sampleBufferRef);
    CFRelease(sampleBufferRef);
}
[pool release];

我通过在另一个函数中移动while循环的内容来解决此问题,然后使用NSTimer以1.0 / track.nominalFrameRate的速率调用该函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM