我正在尝试处理从文件(不是相机流)加载的视频帧。
为此,我已经加载了视频(目前是捆绑包中的测试 mp4,尽管我计划将其设为外部 url)。
我已经阅读了很多关于 AVFoundation 的文档和教程,我发现了许多不同的建议,但没有一个奏效。我希望在帧更改时找到委托方法或通知,但我认为这些实际上并不存在(可能是因为它们处理帧流的速度太慢)。
这是我到目前为止所拥有的。每次从视频流渲染图像时,我都需要运行 [self doImageProcess:] 选择器。如何才能做到这一点?
(一旦正常工作,我将发布完全工作的代码)
#import <AVFoundation/AVFoundation.h>
@implementation VTViewController
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// load mp4
NSURL *theURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]];
AVAsset *avAsset = [AVAsset assetWithURL:theURL];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc] initWithAsset:avAsset];
avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];
CMTime interval = CMTimeMake(33, 1000); // 30fps
id playbackObserver = [avPlayer addPeriodicTimeObserverForInterval:interval queue:nil usingBlock: ^(CMTime time) {
// get image
[self doImageProcess:nil];
}];
img1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
[self.view addSubview:img1];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:CGRectMake(0, 0, 480, 320)];//self.view.frame];
[self.view.layer addSublayer:avPlayerLayer];
[avPlayer seekToTime:kCMTimeZero];
[avPlayer play];
}
- (void)doImageProcess:(UIImage *)theImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, 1.0, 1.0);
[avPlayerLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[arrImages addObject:img];
[img1 removeFromSuperview];
img1 = nil;
img1 = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:img1];
if ([arrImages count] > 4)
[arrImages removeObjectAtIndex:0];
}