ios - 使用 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)
解决方案
通过指令添加背景颜色时,仅支持纯BGRA 颜色。
您的背景颜色设置为什么(并且它是否具有 alpha 值)?
苹果文档
AVVideoCompositionInstruction.backgroundColor
:仅支持纯 BGRA 颜色;图案和其他支持的颜色将被忽略。如果渲染的像素缓冲区没有 alpha,则忽略背景颜色的 alpha 值。
如果背景颜色为 NULL,则视频合成器使用不透明黑色的默认背景颜色。
推荐阅读
- javascript - 如何将 setSelectionRange 与 document.getElementsByClassName 一起使用?
- php - 如何解决 Laravel Excel 工作表问题?
- php - 带有 Ajax 调用的 Laravel 授权中间件返回 403
- tailwind-css - 动态高度 Tailwind CSS [滚动]
- android - 我正在尝试使用 Kotlin 中的 recyclerview 从 Firebase 实时数据库中检索数据,但是当我加载数据时应用程序不断崩溃
- sparql - 无法找到请求目标的有效证书路径(从 URL 加载 RDF 时)
- python - 如何在两个数据框中使用不同的主键和外键列名在 pandas 中进行 vlookup?
- java - 了解和探索 JAAS-GSSAPI-JNDI 如何在后台协同工作
- cassandra - Axon:使用 Cassandra 作为事件存储
- c++ - 如何使用本机寡妇 api 移动 Gtk::Window?