通过AVMutableVideoComposition和CAAnimation输出的黑色视频 [英] Black Video outputted via AVMutableVideoComposition and CAAnimation
问题描述
我正在构建iOS应用程序,该应用程序可以录制视频并使用AVFoundation
和CAAnimation
在录制的视频上添加动画叠加.
所有子功能都可以正常工作,但是在播放动画时,视频背景的末端为黑色.它不是渲染我选择的背景视频.
我已经使用AVAssetWriter
录制视频,并且可以很好地在相机胶卷上播放.但是,如果我使用此录制的视频添加叠加层,则视频背景的末端为黑色.有趣的是,如果我通过本机iOS相机应用程序录制视频并使用它来添加叠加层,则效果很好.
我已经检查了这个问题,但对我没有用. Black Video CAAnimation和AVFoundation AVAssetExportSession
I am building iOS application that is able to record video and add animation overlay over recorded video with AVFoundation
and CAAnimation
.
All sub features are working fine but end of video's background is black while animation is playing. It's not rendering background video that I selected.
I've used AVAssetWriter
to record video and it's well played on camera roll. But if I use this recorded video to add overlay, end of video's background is black. Interesting thing is if I record video via native iOS camera app and use it to add overlay, it's working perfectly.
I've checked this question but did not work for me. Black Video CAAnimation and AVFoundation AVAssetExportSession
任何帮助将不胜感激. 谢谢
Any help would be greatly appreciated. Thanks
推荐答案
您需要更正资产的preferredTransform,因为某些视频文件的tx和ty参数为空(默认值为0.0):
You need to correct preferredTransform of asset, cause for some video files it has empty tx and ty parameters (by default they're 0.0):
...
if let clipVideoTrack = asset.tracks(withMediaType: .video).first {
var preferredTransform = clipVideoTrack.preferredTransform
var videoSize = clipVideoTrack.naturalSize.applying(preferredTransform)
preferredTransform.tx = (videoSize.width < 0) ? fabs(videoSize.width) : 0.0
preferredTransform.ty = (videoSize.height < 0) ? fabs(videoSize.height) : 0.0
videoSize = CGSize(width: fabs(videoSize.width), height: fabs(videoSize.height))
...
let transformer = AVMutableVideoCompositionLayerInstruction.init(assetTrack: clipVideoTrack)
transformer.setTransform(videoTransform, at: kCMTimeZero)
...
这篇关于通过AVMutableVideoComposition和CAAnimation输出的黑色视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!