最近接到一个需求,需要将一个绿幕视频的绿幕背景抠成透明色。并将其加到摄像机镜头上。
NSString *path = [DownLoadTool getFileWithName:[DataManage getObjectForKey:[self.uuid stringByAppendingString:@"-ar"]]];
NSURL *mediaURL = [NSURL fileURLWithPath:path];
self.item = [AVPlayerItem playerItemWithURL:mediaURL];
self.myPlayer = [AVPlayer playerWithPlayerItem:self.item];
[self.myPlayer seekToTime:kCMTimeZero];
self.movieGreen = [[GPUImageMovie alloc] initWithURL:mediaURL];
self.movieGreen.playAtActualSpeed = YES;
self.movieGreen.shouldRepeat = YES;
NSString*resFilename =@"1.png";
UIImage*inputImage = [UIImage imageNamed:resFilename];
self.movieNormal = [[GPUImagePicture alloc]initWithImage:inputImage smoothlyScaleOutput:YES];
NSURL *videoURl = [NSURL fileURLWithPath:path];
AVAsset *asset = [AVAsset assetWithURL:videoURl];
NSArray *array = asset.tracks;
CGSize videoSize = CGSizeZero;
for (AVAssetTrack *track in array) {
if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
videoSize = track.naturalSize;
}
}
self.gpuImageView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, kWholeScreenWidth, videoSize.height*kWholeScreenWidth/videoSize.width)];
self.gpuImageView.center = self.center;
self.gpuImageView.fillMode = kGPUImageFillModePreserveAspectRatio;
self.gpuImageView.backgroundColor = [UIColor clearColor];
[filteredVideoView addSubview:self.gpuImageView];
[self.movieGreen addTarget:chromaKeyBlendFilter];
[self.movieNormal addTarget:chromaKeyBlendFilter];
[chromaKeyBlendFilter addTarget:self.gpuImageView];
解释一下AVPlayerItem
和AVPlayer
是用来播放视频声音的
GPUImageMovie
是播放绿幕视频的
GPUImagePicture
是背景图层放透明的图片1.png
GPUImageView
是父组件
chromaKeyBlendFilter
是GPUImageChromaKeyBlendFilter
滤镜用来过滤绿色。
运行发现GPUImageMovie
播放音画不同步。
通过搜索有大神已经贴出了相关解决办法。
就是修改GPUImageMovie.m
中的readNextVideoFrameFromOutput
方法。
具体链接
网友评论