AVMutableVideoComposition请求sourceImage有时是空的

时间:2017-07-07 03:48:53

标签: ios swift image avfoundation cifilter

这是this question of mine的扩展,但我认为它有点不同,值得提出自己的问题:

我将各种尺寸,比例等的视频过滤到AVMutableVideoComposition

这是我目前的代码:

private func filterVideo(with filter: Filter?) {
    if let player = playerLayer?.player, let playerItem = player.currentItem {
        let composition = AVMutableComposition()
        let videoAssetTrack = playerItem.asset.tracks(withMediaType: .video).first
        let videoCompositionTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
        try? videoCompositionTrack?.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration: playerItem.asset.duration), of: videoAssetTrack!, at: kCMTimeZero)

        let videoComposition = AVMutableVideoComposition(asset: composition, applyingCIFiltersWithHandler: { (request) in
           print(request.sourceImage.pixelBuffer) // Sometimes => nil
           if let filter = filter {
                if let filteredImage = filter.filterImage(request.sourceImage) {
                    request.finish(with: filteredImage, context: nil)
                } else {
                    request.finish(with: RenderError.couldNotFilter)
                }
            } else {
                request.finish(with: request.sourceImage, context: nil)
            }
        })
        playerItem.videoComposition = videoComposition
    }
}

filter是我的自定义Filter类的一个实例,它具有过滤UIImage或CIImage的功能。

问题是有些视频搞砸了。只有filteredImage => nil的问题视频才是这种情况。这表示某些图片为空:他们的pixelBuffernil。顺便说一下,pixelBuffer之前是nil 我甚至将它提供给filter

为什么会发生这种情况,我该如何解决?

0 个答案:

没有答案