问题描述
我有一个过滤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:
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指令会导致不调用处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!