AVAudioFoundation(3):音视频编辑 (2)

如果可能,每一种媒体类型***只使用一个 composition track,这样能够优化资源的使用。当你连续播放媒体数据时,应该将相同类型的媒体数据放到同一个 composition track 中,你可以通过类似下面的代码来从 composition 中查找是否有与当前的 asset track 兼容的 composition track,然后拿来使用:

AVMutableCompositionTrack *compatibleCompositionTrack = [mutableComposition mutableTrackCompatibleWithTrack:<#the AVAssetTrack you want to insert#>]; if (compatibleCompositionTrack) { // Implementation continues. }

需要注意的是,在同一个 composition track 中添加多个视频段时,当视频段之间切换时可能会丢帧,尤其在嵌入式设备上。基于这个问题,应该合理选择一个 composition track 里的视频段数量。

设置音量渐变

只使用一个 AVMutableAudioMix 对象就能够为 composition 中的每一个 audio track 单独做音频处理。

下面代码展示了如果使用 AVMutableAudioMix 给一个 audio track 设置音量渐变给声音增加一个淡出效果。使用 audioMix 类方法获取 AVMutableAudioMix 实例;然后使用 AVMutableAudioMixInputParameters 类的 audioMixInputParametersWithTrack: 接口将 AVMutableAudioMix 实例与 composition 中的某一个 audio track 关联起来;之后便可以通过 AVMutableAudioMix 实例来处理音量了。

AVMutableAudioMix *mutableAudioMix = [AVMutableAudioMix audioMix]; // Create the audio mix input parameters object. AVMutableAudioMixInputParameters *mixParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:mutableCompositionAudioTrack]; // Set the volume ramp to slowly fade the audio out over the duration of the composition. [mixParameters setVolumeRampFromStartVolume:1.f toEndVolume:0.f timeRange:CMTimeRangeMake(kCMTimeZero, mutableComposition.duration)]; // Attach the input parameters to the audio mix. mutableAudioMix.inputParameters = @[mixParameters]; 自定义视频处理

处理音频是我们使用 AVMutableAudioMix,那么处理视频时,我们就使用 AVMutableVideoComposition,只需要一个 AVMutableVideoComposition 实例就可以为 composition 中所有的 video track 做处理,比如设置渲染尺寸、缩放、播放帧率等等。

下面我们依次来看一些场景。

设置视频背景色

所有的 video composition 也必然对应一组 AVVideoCompositionInstruction 实例,每个 AVVideoCompositionInstruction 中至少包含一条 video composition instruction。我们可以使用 AVMutableVideoCompositionInstruction 来创建我们自己的 video composition instruction,通过这些指令,我们可以修改 composition 的背景颜色、后处理、layer instruction 等等。

下面的实例代码展示了如何创建 video composition instruction 并将一个 composition 的整个时长都设置为红色背景色:

AVMutableVideoCompositionInstruction *mutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; mutableVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, mutableComposition.duration); mutableVideoCompositionInstruction.backgroundColor = [[UIColor redColor] CGColor]; 设置透明度渐变

我们也可以用 video composition instructions 来应用 video composition layer instructions。AVMutableVideoCompositionLayerInstruction 可以用来设置 video track 的图形变换、图形渐变、透明度、透明度渐变等等。一个 video composition instruction 的 layerInstructions 属性中所存储的 layer instructions 的顺序决定了 tracks 中的视频帧是如何被放置和组合的。

下面的示例代码展示了如何在从一个视频切换到第二个视频时添加一个透明度渐变的效果:

AVAsset *firstVideoAssetTrack = <#AVAssetTrack representing the first video segment played in the composition#>; AVAsset *secondVideoAssetTrack = <#AVAssetTrack representing the second video segment played in the composition#>; // Create the first video composition instruction. AVMutableVideoCompositionInstruction *firstVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; // Set its time range to span the duration of the first video track. firstVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration); // Create the layer instruction and associate it with the composition video track. AVMutableVideoCompositionLayerInstruction *firstVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableCompositionVideoTrack]; // Create the opacity ramp to fade out the first video track over its entire duration. [firstVideoLayerInstruction setOpacityRampFromStartOpacity:1.f toEndOpacity:0.f timeRange:CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration)]; // Create the second video composition instruction so that the second video track isn\'t transparent. AVMutableVideoCompositionInstruction *secondVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; // Set its time range to span the duration of the second video track. secondVideoCompositionInstruction.timeRange = CMTimeRangeMake(firstVideoAssetTrack.timeRange.duration, CMTimeAdd(firstVideoAssetTrack.timeRange.duration, secondVideoAssetTrack.timeRange.duration)); // Create the second layer instruction and associate it with the composition video track. AVMutableVideoCompositionLayerInstruction *secondVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableCompositionVideoTrack]; // Attach the first layer instruction to the first video composition instruction. firstVideoCompositionInstruction.layerInstructions = @[firstVideoLayerInstruction]; // Attach the second layer instruction to the second video composition instruction. secondVideoCompositionInstruction.layerInstructions = @[secondVideoLayerInstruction]; // Attach both of the video composition instructions to the video composition. AVMutableVideoComposition *mutableVideoComposition = [AVMutableVideoComposition videoComposition]; mutableVideoComposition.instructions = @[firstVideoCompositionInstruction, secondVideoCompositionInstruction]; 动画效果

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggxpp.html