首页 > 解决方案 > 使用 AVAssetExportSession 后视频颜色错误

问题描述

我正在生成一个视频并添加backgroundView它的背景。

BackgroundView包含颜色或图像。

使用时AVAssetExportSession,我们无法为视频添加背景。这就是为什么我从backgroundView.

guard let export = AVAssetExportSession(asset: mixComposition,presetName: AVAssetExportPresetHighestQuality) else { return }
export.videoComposition = mainComposition
export.outputFileType = .mp4
export.outputURL = exportURL
export.shouldOptimizeForNetworkUse = true

之后,AVURLAsset通过视频背景上的说明添加。并生成最后一个。

guard let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) else { return nil }
do {
    try track.insertTimeRange(CMTimeRange(start: .zero, end: asset.duration),
                                      of: asset.tracks(withMediaType: .video).first!, at: .zero)
} catch {
    print(error)
}
return AVMutableVideoCompositionLayerInstruction(assetTrack: track)

还有我的颜色不正确的问题。从图像生成后它是正确的,但在添加指令后它变得更轻了。

更新

发现AVAssetExportSession改变视频的颜色后。它不依赖于说明,如果将视频添加到videoLayer.

我有一个添加了纯色的视频在此处输入图像描述

并在导出后更改为在此处输入图像描述

测试视频:下载链接

更新:

添加了 mainComposition 和 mixComposition

var mixComposition = AVMutableComposition()
let mainInstruction = AVMutableVideoCompositionInstruction()

使用 func 添加像 inout 参数一样的 backgroundAsset。

    func addBackgroundAsset(_ asset: AVURLAsset?,
                            composition: inout AVMutableComposition) -> AVMutableVideoCompositionLayerInstruction? {
        guard let asset = asset else { return nil}
        // Block with adding Background Image
        guard let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
            let assetTrack = asset.tracks(withMediaType: .video).first else { return nil }
        do {
            try track.insertTimeRange(CMTimeRange(start: .zero, end: asset.duration),
                                      of: assetTrack, at: .zero)
        } catch {
            print(error)
        }
        let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: track)
        let transform = assetTrack.preferredTransform
        instruction.setTransform(transform, at: .zero)
        return instruction
    }

        let mainInstruction = mainInstruction
        mainInstruction.timeRange = CMTimeRange(start: .zero, end: duration)
        mainInstruction.layerInstructions = instructions
        let mainComposition = AVMutableVideoComposition()
        mainComposition.instructions = [mainInstruction]
        mainComposition.frameDuration = CMTime(value: 1, timescale: 30)
        mainComposition.renderSize = CGSize(width: 1080, height: 1920)
        mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer,
                                                                            in: outputLayer)

标签: iosswiftavfoundationavassetexportsession

解决方案


通过指令添加背景颜色时,仅支持BGRA 颜色。

您的背景颜色设置为什么(并且它是否具有 alpha 值)?

苹果文档AVVideoCompositionInstruction.backgroundColor

仅支持纯 BGRA 颜色;图案和其他支持的颜色将被忽略。如果渲染的像素缓冲区没有 alpha,则忽略背景颜色的 alpha 值。

如果背景颜色为 NULL,则视频合成器使用不透明黑色的默认背景颜色。


推荐阅读