ios - 设置 AVMutableVideoComposition 指令导致处理程序不被调用

标签 ios swift image-processing avfoundation swift4

我有一个过滤 AVPlayerItem Assets 的功能。问题之一是设置视频的转换。但是,每当我设置 AVMutableVideoCompositionAVMutableVideoCompositionInstruction 时,处理程序就不再被调用。

这是我的代码:

private func filter(playerItem: AVPlayerItem) {

    let videoComposition = AVMutableVideoComposition(asset: playerItem.asset, applyingCIFiltersWithHandler: { (request) in
        print("Composing") // does not print whenever the instructions are added
        if let filteredImage = filterImage(request.sourceImage) {
            request.finish(with: filteredImage, context: nil)
        } else {
            request.finish(with: RenderError.couldNotFilter) // An error
        }
    })

    guard let videoTrack = playerItem.asset.tracks(withMediaType: .video).first else { return }

    let size = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width)
    videoComposition.renderSize = size

    let videoInstruction = AVMutableVideoCompositionInstruction()
    videoInstruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: playerItem.asset.duration)

    let transformInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
    let translate = CGAffineTransform(translationX: size.width, y: size.height)
    let rotate = CGAffineTransform(rotationAngle: CGFloat.pi)
    transformInstruction.setTransform(translate.concatenating(rotate), at: kCMTimeZero)
    videoInstruction.layerInstructions.append(transformInstruction)
    videoComposition.instructions.append(videoInstruction)

    playerItem.videoComposition = videoComposition
}

为什么不再调用处理程序,我该如何解决?

如果你能回答我就给你这么多加分!

最佳答案

我向 Apple 提交了错误报告,显然这种行为不是错误。这是他们的回应:

Engineering has provided the following information regarding this issue:

CoreImage filtering and layer instruction based composition can't be used simultaneously. Layer instructions won't be run when added to an AVMutableVideoComposition that it is initialized with +[videoCompositionWithAsset:applyingCIFiltersWithHandler:]. To use layer instructions in this case, move the functionality into the handler instead of adding the layer instructions to the AVMutableVideoComposition.

这解释了为什么指令似乎没有做任何事情,并且没有调用处理程序。他们说将转换功能移至处理程序而不是使用指令;不幸的是,我不太清楚如何实现这个解决方案——那是另一个问题。

关于ios - 设置 AVMutableVideoComposition 指令导致处理程序不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45090477/

相关文章:

image - 使用 ImageMagick 使白色背景透明

ios - AutoHotKey iOS 双空格脚本

ios - google maps route between 2 points with multi waypoints swift ios

ios - Tableview获取索引超出空数组范围

ios - 容器应用程序的控制台输出为空

ios - 快速切换与数组匹配的模式

ios - 如何在不重复使用的情况下选择 TableView 中的第一个单元格?

image-processing - 在 2D 点云中查找简单形状

python - Lightroom 导出后,PIL 和 pyexiv2 中缺少图像标题标签

cocoa-touch - iAds 未在开发者设备上显示