什么是 AVVideoCompositon?
你可以参考
AVFoundation Programming Guide
强行概括:
告诉系统需要我这个视频该怎么放。(什么时间放什么画面)
如何创建?
- 手动创建
- 用系统的“神奇方法”帮忙创建
手动创建
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration);
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
[videolayerInstruction setOpacity:0.0 atTime:self.videoAsset.duration];
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
用系统的“神奇方法”帮忙创建
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoCompositionWithPropertiesOfAsset:self.videoAsset];
没错……一句话就结束了,不然怎么叫神奇方法
能做什么?
- 对不同轨的视频进行透明度修改(可以实现渐隐渐弱的过场动画)
- 不同轨的视频进行裁剪
- 对不同轨的视频进行 transform (缩放、平移……)
Example:
AVAssetTrack *videoAssetTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[videolayerInstruction setTransform:videoAssetTrack.preferredTransform atTime:kCMTimeZero];
[videolayerInstruction setOpacity:0.0 atTime:self.videoAsset.duration];
mainInstruction.layerInstructions = [NSArray arrayWithObjects:videolayerInstruction,nil];
自定义 AVVideoCompositionInstruction
什么情况要自定义?
同一帧需要通过对两个画面做不同滤镜处理然后进行合成的时候
简称:双轨交叉无多滤镜
--
不需要自定义的情况
单轨多滤镜
![](https://img.haomeiwen.com/i795652/4dc0bddf544b2544.jpg)
如果视频是只有单轨,那直接使用
+ (AVVideoComposition *)videoCompositionWithAsset:(AVAsset *)asset
applyingCIFiltersWithHandler:(void (^)(AVAsynchronousCIImageFilteringRequest *request))applier NS_AVAILABLE(10_11, 9_0);
想怎么加滤镜就怎么加
ps:通过 request 可以获取时间
多轨交叉无多滤镜合成
![](https://img.haomeiwen.com/i795652/e99b566ae4b59c8b.jpg)
如果视频是只有双轨,交叉的地方需要做一个渐隐渐弱的动画,那 applyingCIFiltersWithHandler 的方法就不行了。
因为他只对第一个轨道的视频起作用
The returned AVVideoComposition will cause the specified handler block to be called to filter each frame of the asset's first enabled video track.
播放时:
可以使用 AVPlayerItemVideoOutput 加滤镜
导出时:
可以使用 AVAssetReaderOutputMetadataAdaptor 添加滤镜
--
需要自定义的情况
多轨交叉多滤镜合成
![](https://img.haomeiwen.com/i795652/f6a44d852194c40d.jpg)
我们需要先对视频1做一系列滤镜,对视频2做一系列滤镜,把加好滤镜的两张图片做一个渐变的合成动画
这种情况和“多轨交叉无多滤镜合成”有什么区别?
假设从轨道1中取出来的画面是 A,轨道2中取出来的画面是 B
因为 API 的限制,多轨交叉无多滤镜合成
的几种 API
- applyingCIFiltersWithHandler
- AVPlayerItemVideoOutput
- AVAssetReaderOutputMetadataAdaptor
你只能获取一张图:A和B合成完成后的图片
有些情况你可能需要先对 A、B 进行处理,然后再进行合成
这种情况你就需要自定义
自定义 AVVideoCompositionInstruction 需要注意
- 如果需要使用 Animation Tool 进行导出的时候需要把 enablePostProcessing 设为 YES
- passthroughTrackID 设置了以后 startVideoCompositionRequest 不会得到回调
- requiredSourceTrackIDs 为 nil , request 可以拿到所有的 sourceTrack
网友评论