设置AVMutableVideoComposition指令会导致不调用处理程序 [英] Setting AVMutableVideoComposition instruction causes handler not to be called

查看:960
本文介绍了设置AVMutableVideoComposition指令会导致不调用处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过滤AVPlayerItem资产的功能。其中一个问题是设置视频的转换。但是,每当我设置 AVMutableVideoComposition AVMutableVideoCompositionInstruction 时,就不再调用处理程序。

I have a function to filter an AVPlayerItem's asset. One of the problems was setting the transform of the video. However, whenever I set the AVMutableVideoCompositionInstruction of the AVMutableVideoComposition, the handler is no longer called.

这是我的代码:

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
}

为什么处理程序否更长的叫,我该怎么办呢?

Why is the handler no longer called, and how can I fix it?

如果你能回答,我会给你这么多的布朗尼点!

推荐答案

我向Apple提交了一份错误报告,显然这种行为不是一个错误。这是他们的答复:

I filed a bug report with Apple, and apparently this behavior is not a bug. This was their response:


工程部提供了有关此问题的以下信息:

Engineering has provided the following information regarding this issue:

CoreImage过滤和基于图层指令的合成不能同时使用。添加到AVMutableVideoComposition时,不会运行图层指令,使用+ [videoCompositionWithAsset:applyingCIFiltersWithHandler:]初始化它。要在这种情况下使用图层指令,请将功能移到处理程序中,而不是将图层指令添加到AVMutableVideoComposition。

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.

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

This explains why the instructions seem to have not been doing anything, and the handler not called. They say to move the transform functionality to the handler instead of using instructions; unfortunately, I do not quite know how to implement this solution –– that is another question.

这篇关于设置AVMutableVideoComposition指令会导致不调用处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆