我正在创建一个视频,其中添加了效果,但视频效果不佳。过滤后的视频卡住,播放不顺畅。
我选择了一个来自图书馆的视频并获取它的网址和资产,然后将其传递给下面描述的不同功能。
func createVideoComposition(filterEffect:String) -> AVVideoComposition {
let playerItem = AVPlayerItem(asset: self.videoAsset!)
let composition = AVVideoComposition(asset: playerItem.asset) { (request) in
let sourceImage = request.sourceImage.clampedToExtent()
guard let filter = CIFilter(name: filterEffect) else{
return request.finish(with: NSError())
}
filter.setValue(sourceImage, forKey: kCIInputImageKey)
return request.finish(with: filter.outputImage!, context: nil)
}
return composition }
以下是创建合成的功能。
func playVideo(url:URL,filter:Bool,composition:AVVideoComposition?){
let playerItem : AVPlayerItem!
if filter == true {
playerItem = AVPlayerItem(asset: self.videoAsset!)
playerItem.videoComposition = composition!
self.player = AVPlayer(playerItem: playerItem)
}else{
self.player = AVPlayer(url: url)
}
self.player.addObserver(self, forKeyPath: "player.currentItem.status", options: .new, context: nil)
avPlayerController = AVPlayerViewController()
self.avPlayerController.view.backgroundColor = .clear
avPlayerController.player = self.player
avPlayerController.view.frame = CGRect(x: self.viewVideoPlayer.frame.origin.x, y: self.viewVideoPlayer.frame.origin.y, width: self.viewVideoPlayer.frame.size.width, height: self.viewVideoPlayer.frame.size.height)
avPlayerController.showsPlaybackControls = false
self.viewVideoPlayer.addSubview(avPlayerController.view) }
我在这里播放视频
{{1}}
以下是输出
已过滤的视频:https://www.dropbox.com/s/gehhks2z9pjbmpc/Normal%20Video.mov?dl=0