美文网首页
iOS开发中GUPImage抠除绿幕视频

iOS开发中GUPImage抠除绿幕视频

作者: FateOfKing | 来源:发表于2019-03-18 16:57 被阅读0次

    最近接到一个需求,需要将一个绿幕视频的绿幕背景抠成透明色。并将其加到摄像机镜头上。

    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];
    

    解释一下AVPlayerItemAVPlayer是用来播放视频声音的
    GPUImageMovie是播放绿幕视频的
    GPUImagePicture是背景图层放透明的图片1.png
    GPUImageView是父组件
    chromaKeyBlendFilterGPUImageChromaKeyBlendFilter滤镜用来过滤绿色。

    运行发现GPUImageMovie播放音画不同步。
    通过搜索有大神已经贴出了相关解决办法。
    就是修改GPUImageMovie.m中的readNextVideoFrameFromOutput方法。
    具体链接

    相关文章

      网友评论

          本文标题:iOS开发中GUPImage抠除绿幕视频

          本文链接:https://www.haomeiwen.com/subject/zwrbmqtx.html