如何使用AVFoundation框架创建和添加像Instagram这样的视频过滤器 - Swift编程

时间:2015-11-13 09:03:00

标签: ios swift video filter instagram

我正在使用Swift编程处理AVFoundation框架。有人可以帮我找一些教程或链接或代码片段来应用类似于Instagram的视频过滤器。

我正在使用iOS视频创建器类型的应用程序来录制视频,之后我可以将过滤器应用于视频。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您应该查看Apple的示例项目RosyWriter。它是你想要实现的一个很好的例子。

https://developer.apple.com/library/prerelease/ios/samplecode/RosyWriter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011110

此外,您还可以查看GLImageProcessing示例项目。

https://developer.apple.com/library/ios/samplecode/GLImageProcessing/Introduction/Intro.html

希望有所帮助!

答案 1 :(得分:0)

从iOS 9.0开始,您可以使用AVVideoComposition逐帧应用核心图像过滤器。

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

播放它
let playerItem = AVPlayerItem(asset: asset)
playerItem.videoComposition = composition
let player = AVPlayer(playerItem: playerItem)
player.play()

核心图像过滤器逐帧实时添加。您还可以使用AVAssetExportSession类导出视频。

这里是WWDC 2015的精彩介绍: https://developer.apple.com/videos/play/wwdc2015/510/?time=1222