如何导出具有透明背景alpha通道的视频

问题描述

我有一个视频要覆盖在另一个视频的顶部,其中覆盖的视频具有透明的背景。我正在使用AVMutableComposition做到这一点,我只需添加轨道即可,效果很好; AVPlayer播放得很好。问题是当我尝试导出视频时。

导出视频时,我仍然可以看到叠加层,但是其背景不再透明。它将奇怪的滤镜添加到其背后的视频中。叠加层的编解码器是AAC,带有Alpha的HEVC,这是我可以使其用于合成的唯一方法。

这是要导出的代码,其中outputURL以.mp4结尾:

func getVideoUrl(composition: AVMutableComposition,vidComp: AVMutableVideoComposition,audioMix: AVMutableAudioMix,overlays: [Overlay],completion: @escaping (_ success: Bool,_ videoID: String?,_ videoName: String?) -> ()) {

    var preset = "AVAssetExportPreset1920x1080"
    
    exportSession = AVAssetExportSession(asset: composition,presetName: preset)
    
    guard let exportSession = exportSession else {
        completion(false,nil,nil)
        return
    }
    
    exportSession.videoComposition = vidComp
    exportSession.audioMix = audioMix
    exportSession.outputURL = uniqueURLForSave()
    exportSession.outputFileType = AVFileType.mp4
    exportSession.shouldOptimizeForNetworkUse = true
    
    exportSession.exportAsynchronously {
        switch exportSession.status {
        case .completed:
            UISaveVideoAtPathToSavedPhotosAlbum(exportSession.outputURL!.relativePath,self,nil)
            
            completion(true,self.videoUrlID,self.videoName)
        case .cancelled:
            print("Cancelled")
            completion(false,nil)
        case .failed:
            print("Failed: \(exportSession.error.debugDescription)")
            completion(false,nil)
        default:
            print("Other Error")
            completion(false,nil)
        }
    }
}

我也尝试使用.mov和AVAssetExportPresetHEVC1920x1080和AVAssetExportPresetHEVC1920x1080WithAlpha之类的预设进行输出。有人知道如何导出视频,以便尊重透明度吗?如果可能的话,我宁愿将其保存为mp4。

下面是视频的屏幕快照,其中是叠加层视频,然后是叠加层视频播放完毕后的画面。

enter image description here

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...