0

How can I get an equal-power crossfade working in an AVVideoComposition? I'm using something like the following to fade between video tracks, but when looping the same video over and over there is a very noticeable brightness dip during the transition due to whatever curve is being used internally in setOpacityRamp.

let videoInstruction = AVMutableVideoCompositionInstruction()

let fromLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: fromCompositionTrack)
fromLayerInstruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0, timeRange: timeRange)

let toLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: toCompositionTrack)
toLayerInstruction.setOpacityRamp(fromStartOpacity: 0, toEndOpacity: 1, timeRange: timeRange)

videoInstruction.timeRange = timeRange
videoInstruction.layerInstructions = [fromLayerInstruction, toLayerInstruction]
4

1 回答 1

0

这是不可能的。的setOpacityRamp方法AVMutableVideoCompositionLayerInstruction列出了以下描述:

在不透明度渐变期间,使用线性插值计算不透明度。

不过,有一个更简单的解决方案,我在《Learning AV Foundation: A Hands-on Guide to Mastering the AV Foundation》一书中找到了它——淡出当前的视频轨道,但不要淡入新的轨道。这本书将其称为普通溶解而不是交叉溶解

let videoInstruction = AVMutableVideoCompositionInstruction()

let fromLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: fromCompositionTrack)
fromLayerInstruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0, timeRange: timeRange)

let toLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: toCompositionTrack)
// toLayerInstruction.setOpacityRamp(fromStartOpacity: 0, toEndOpacity: 1, timeRange: timeRange)

videoInstruction.timeRange = timeRange
videoInstruction.layerInstructions = [fromLayerInstruction, toLayerInstruction]
于 2020-03-23T15:10:14.440 回答