最近接到一個(gè)需求荐类,需要將一個(gè)綠幕視頻的綠幕背景摳成透明色境肾。并將其加到攝像機(jī)鏡頭上蚂夕。
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
濾鏡用來過濾綠色翰绊。
運(yùn)行發(fā)現(xiàn)GPUImageMovie
播放音畫不同步佩谷。
通過搜索有大神已經(jīng)貼出了相關(guān)解決辦法。
就是修改GPUImageMovie.m
中的readNextVideoFrameFromOutput
方法监嗜。
具體鏈接