I'm trying to crop a video / display a portion of a video in AVFoundation. So far my code looks right to me but the output is not what I wanted. I don't see what is wrong. Any ideas?
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 = cropComposition
self.player.replaceCurrentItem(with: item)
self.player.play()