如何使用AVFoundation框架创建和添加类似Instagram的视频过滤器-Swift编程 [英] How to create and add video filter like Instagram using AVFoundation framework - Swift programming
问题描述
我正在使用Swift编程来开发AVFoundation框架.有人可以帮我找到一些教程或链接或代码片段,以应用类似于Instagram的视频过滤器.
Im working on AVFoundation framework using Swift programming. Can someone help me find some tutorial or links or code snippet to apply video filter similar to what Instagram does.
我正在使用iOS视频创建器类型的应用程序,该应用程序可以记录视频,以后我可以将过滤器应用于视频.
I'm working on iOS video creator kind of app which records video and later i can apply filter to video.
先谢谢了.
推荐答案
自iOS 9.0起,您可以使用AVVideoComposition将核心图像滤镜逐帧应用于视频.
Since iOS 9.0 you can use AVVideoComposition to apply core image filter to video frame by frame.
let filter = CIFilter(name: "CIGaussianBlur")!
let composition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in
// Clamp to avoid blurring transparent pixels at the image edges
let source = request.sourceImage.imageByClampingToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
// Vary filter parameters based on video timing
let seconds = CMTimeGetSeconds(request.compositionTime)
filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)
// Crop the blurred output to the bounds of the original image
let output = filter.outputImage!.imageByCroppingToRect(request.sourceImage.extent)
// Provide the filter output to the composition
request.finishWithImage(output, context: nil)
// Clamp to avoid blurring transparent pixels at the image edges
let source = request.sourceImage.clampedToExtent()
filter.setValue(source, forKey: kCIInputImageKey)
//Vary filter parameters based on video timing
let seconds = CMTimeGetSeconds(request.compositionTime)
filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)
// Crop the blurred output to the bounds of the original image
let output = filter.outputImage!.cropped(to: request.sourceImage.extent)
request.finish(with: output, context: nil)
})
现在在这里,我们可以使用之前创建的资产创建AVPlayerItem并使用AVPlayer播放它
now here we can create AVPlayerItem using the asset created earlier and play it using AVPlayer
let playerItem = AVPlayerItem(asset: asset)
playerItem.videoComposition = composition
let player = AVPlayer(playerItem: playerItem)
player.play()
核心图像过滤器逐帧添加了实时功能.您还可以使用AVAssetExportSession类导出视频.
core image filter added realtime frame by frame. You can also export video using AVAssetExportSession class.
这是WWDC 2015的精彩介绍: https://developer.apple.com/videos/play/wwdc2015 /510/?time = 1222
here is WWDC 2015 great introduction: https://developer.apple.com/videos/play/wwdc2015/510/?time=1222
这篇关于如何使用AVFoundation框架创建和添加类似Instagram的视频过滤器-Swift编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!