Black Video outputted via AVMutableVideoComposition and CAAnimation

∥☆過路亽.° 提交于 2019-12-13 03:45:45

问题


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


回答1:


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)

            ...


来源:https://stackoverflow.com/questions/46926795/black-video-outputted-via-avmutablevideocomposition-and-caanimation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!