自从更新到 iOS 13 以来,我用来淡入和淡出视频的视频合成已损坏。这是我的代码在安装 iOS 13 之前可以正常工作。
现在,当我导出视频时,有声音,只是黑屏。
let urlAsset = AVURLAsset(url: inputURL, options: nil)
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetHighestQuality) else { handler(nil)
return
}
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileType.m4v
exportSession.shouldOptimizeForNetworkUse = true
let composition = AVMutableVideoComposition(propertiesOf: urlAsset)
let clipVideoTrack = urlAsset.tracks(withMediaType: AVMediaType.video)[0]
let timeDuration = CMTimeMake(value: 1, timescale: 1)
let layer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
// MARK: Fade in effect
layer.setOpacityRamp(fromStartOpacity: 0.0, toEndOpacity: 1.0, timeRange: CMTimeRange(start: CMTime.zero, duration: timeDuration))
// MARK: Fade out effect
let startTime = CMTimeSubtract(urlAsset.duration, CMTimeMake(value: 1, timescale: 1))
layer.setOpacityRamp(
fromStartOpacity: 1.0,
toEndOpacity: 0.0,
timeRange: CMTimeRangeMake(start: startTime, duration: timeDuration)
)
let instruction = AVMutableVideoCompositionInstruction()
instruction.layerInstructions = [layer]
instruction.timeRange = CMTimeRange(start: CMTime.zero, duration: urlAsset.duration)
composition.instructions = [instruction]
exportSession.videoComposition = composition
exportSession.exportAsynchronously { () -> Void in
handler(exportSession)
print("composition has completed")
}