ios 如何在AVFoundation中显示视频的剪切/部分

pcww981p  于 2022-12-24  发布在  iOS
关注(0)|答案(1)|浏览(102)

bounty将在3天后过期。回答此问题可获得+200声望奖励。Nico S.希望引起更多人关注此问题。

我正在尝试在AVFoundation中裁剪视频/显示视频的一部分。到目前为止,我的代码看起来正确,但输出不是我想要的。我看不出哪里出了问题。有什么想法吗?

let item = AVPlayerItem(url: nextVideoItem.url)
let cropRect = CGRect(x: 200, y: 200, width: 300, height: 300)
let cropComposition = AVMutableVideoComposition(asset: item.asset, applyingCIFiltersWithHandler: { request in    
    let cropFilter = CIFilter(name: "CICrop")!
    cropFilter.setValue(request.sourceImage, forKey: kCIInputImageKey)
    cropFilter.setValue(CIVector(cgRect: cropRect), forKey: "inputRectangle")
    
    let imageAtOrigin = cropFilter.outputImage!.transformed(by: CGAffineTransform(translationX: -cropRect.origin.x, y: -cropRect.origin.y))
    request.finish(with: imageAtOrigin, context: nil)
})
cropComposition.renderSize = cropRect.size
item.videoComposition = cropScaleComposition
self.player.replaceCurrentItem(with: item)
self.player.play()
pqwbnv8z

pqwbnv8z1#

cropScaleComposition未定义,但在以下行中使用:

item.videoComposition = cropScaleComposition

缩放到零/空/未定义,即使是可编译的?,也肯定会导致空屏幕。

相关问题