语言用的是OC 所以平台是IOS
在手机上想要播放一段透明背景视频其实无非就只有两种模式(其实电脑上也一样):
第一种方法.把透明通道压制到原视频内部,变成另外一段“真正”的透明视频,但带来的问题是视频体积辐射状态扩大。尝试压制了一段2M4秒的透明背景AVI,体积变成了1G。
当然这部分工作也可以通过代码实现,比如AVAnimator:
NSString*rgbResourceName;
NSString*alphaResourceName;
NSString*rgbTmpMvidFilename;
NSString*rgbTmpMvidPath;
rgbResourceName =@"MarioRendered_960_640_rgb_CRF_20_24BPP.m4v";
alphaResourceName =@"MarioRendered_960_640_alpha_CRF_20_24BPP.m4v";
rgbTmpMvidFilename =@"MarioRendered.mvid";
rgbTmpMvidPath = [AVFileUtilgetTmpDirPath:rgbTmpMvidFilename];
NSLog(@"loading %@", rgbTmpMvidPath);
AVAnimatorMedia*media = [AVAnimatorMediaaVAnimatorMedia];
self.marioMedia= media;
AVAssetJoinAlphaResourceLoader*resLoader = [AVAssetJoinAlphaResourceLoaderaVAssetJoinAlphaResourceLoader];
resLoader.movieRGBFilename= rgbResourceName;
resLoader.movieAlphaFilename= alphaResourceName;
resLoader.outPath= rgbTmpMvidPath;
resLoader.alwaysGenerateAdler=TRUE;
media.resourceLoader= resLoader;
AVMvidFrameDecoder*frameDecoder = [AVMvidFrameDecoderaVMvidFrameDecoder];
media.frameDecoder= frameDecoder;
media变成了一段内含透明通道的真正透明视频。
第二种方法·通过实时渲染把背景绿幕或其他幕透明化,这种事情一般都是GPU处理,比如GPUImage
GPUImageChromaKeyBlendFilter*filter = [[GPUImageChromaKeyBlendFilteralloc]init];
[filtersetColorToReplaceRed:0.0green:1.0blue:0.0];
[filtersetThresholdSensitivity:0.4];
[movieFileaddTarget:filter];
#if defined(GPUIMAGEWORKAROUND)
//NSString *resFilename = @"checkerboard_background.png";
NSString*resFilename =@"empty.png";
UIImage*inputImage = [UIImageimageNamed:resFilename];
GPUImagePicture*sourcePicture = [[GPUImagePicturealloc]initWithImage:inputImagesmoothlyScaleOutput:YES];
[sourcePictureaddTarget:filter];
self.sourcePicture= sourcePicture;
#else
#endif// GPUIMAGEWORKAROUND
[filteraddTarget:self.marioView];
self.marioView.fillMode=kGPUImageFillModeStretch;
self.movieFile= movieFile;
self.filter= filter;
前一种情况是app容量剧增,后一种情况是GPU使用不能冲突