Black Video outputted via AVMutableVideoCompositio

2019-08-18 17:07发布

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条回答
干净又极端
2楼-- · 2019-08-18 17:33

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)

            ...
查看更多
登录 后发表回答