设置 AVMutableVideoComposition 指令导致处理程序不被调用
Setting AVMutableVideoComposition instruction causes handler not to be called
我有一个过滤 AVPlayerItem 资产的功能。问题之一是设置视频的转换。但是,每当我设置 AVMutableVideoComposition
的 AVMutableVideoCompositionInstruction
时,处理程序就不再被调用。
这是我的代码:
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
}
为什么 handler
不再被调用,我该如何解决?
如果你能回答我就给你这么多加分!
我向 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.
这解释了为什么指令似乎没有做任何事情,并且没有调用处理程序。他们说将转换功能移至处理程序而不是使用指令;不幸的是,我不太清楚如何实施这个解决方案——那是另一个问题。
我有一个过滤 AVPlayerItem 资产的功能。问题之一是设置视频的转换。但是,每当我设置 AVMutableVideoComposition
的 AVMutableVideoCompositionInstruction
时,处理程序就不再被调用。
这是我的代码:
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
}
为什么 handler
不再被调用,我该如何解决?
如果你能回答我就给你这么多加分!
我向 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.
这解释了为什么指令似乎没有做任何事情,并且没有调用处理程序。他们说将转换功能移至处理程序而不是使用指令;不幸的是,我不太清楚如何实施这个解决方案——那是另一个问题。