Kakapos 是一個視頻添加過濾器工具环肘,支持網絡和本地網址以及相冊視頻欲虚。
同時可以簡單的支持多種濾鏡庫,例如Harbeth悔雹、GPUImage苍在、MetalPetal、CoreImage等等荠商。
反正核心其實就是對CVPixelBuffer
進行注入濾鏡處理寂恬,然后再導出視頻即可。
支持全平臺系統(tǒng)莱没,macOS初肉、iOS、tvOS饰躲、watchOS
演示Demo
這邊已經提供關于macOS和iOS兩個端的演示Demo牙咏,需要的朋友請移步GitHub下載即可。
框架介紹
該架構主要涵蓋4個文件:
- Exporter:主要的轉換文件嘹裂,注入濾鏡后導出視頻妄壶。
/// 設置導出文件質量
public var presetName: String = AVAssetExportPresetHighestQuality {
didSet {
if !AVAssetExportSession.allExportPresets().contains(presetName) {
presetName = AVAssetExportPresetMediumQuality
}
}
}
/// 根據(jù)視頻鏈接初始化
public init(videoURL: URL, delegate: ExporterDelegate) {
self.init(asset: AVAsset(url: videoURL), delegate: delegate)
}
/// 根據(jù)AVAsset初始化
public init(asset: AVAsset, delegate: ExporterDelegate) {
self.asset = asset
self.delegate = delegate
}
/// 添加濾鏡然后導出視頻,主要在`filtering`當中對`buffer`添加濾鏡寄狼,最后協(xié)議反饋結果丁寄。
public func export(outputURL: URL, optimizeForNetworkUse: Bool = true, filtering: @escaping PixelBufferCallback) {
...
}
- ExporterDelegate:導出協(xié)議,包括成功和失敗兩個協(xié)議方法泊愧。
public protocol ExporterDelegate: NSObjectProtocol {
/// 導出成功的視頻網址伊磺,相當于outputURL。
func export(_ exporter: Exporter, success videoURL: URL)
/// 視頻導出失敗删咱。
func export(_ exporter: Exporter, failed error: Exporter.Error)
}
- Compositor:對視頻幀進行處理屑埋,對緩沖進入注入濾鏡處理,最后再還原回去
finish(withComposedVideoFrame:)
即可痰滋。
func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
self.renderQueue.sync {
guard let instruction = request.videoCompositionInstruction as? CompositionInstruction,
let pixels = request.sourceFrame(byTrackID: instruction.trackID) else {
return
}
let buffer = instruction.bufferCallback(pixels) ?? pixels
request.finish(withComposedVideoFrame: buffer)
}
}
- CompositionInstruction:表示一個指令摘能,決定每個
timeRange
內每個軌道的狀態(tài)续崖,配置視頻組合的渲染尺寸、縮放团搞、幀時長等袜刷,定義時間范圍信息,以及每一幀的層級莺丑。
/// 接收回調信息著蟹,方便后續(xù)轉換處理。
init(trackID: CMPersistentTrackID, bufferCallback: @escaping Exporter.PixelBufferCallback) {
self.trackID = trackID
self.bufferCallback = bufferCallback
super.init()
self.enablePostProcessing = true
}
關于AVMutableVideoCompositionInstruction
更多介紹
參考:https://www.codersrc.com/archives/11663.html
該類還能拿來 完成對媒體裁剪 / 縮放 / 轉場 / 過渡等操作梢莽。
如何使用萧豆?
- 設置轉換視頻存儲沙盒鏈接。
/// 創(chuàng)建臨時路徑以保存轉換的視頻
let outputURL: URL = {
let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let outputURL = documents.appendingPathComponent("condy_exporter_video.mp4")
// Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: outputURL.path) {
do {
try FileManager.default.removeItem(at: outputURL)
} catch {
//completionHandler(nil, error)
}
}
return outputURL
}()
- 創(chuàng)建視頻導出器
exporter
實例昏名。
let exporter = Exporter.init(videoURL: ``URL Link``, delegate: self)
或者
let exporter = Exporter.init(asset: ``AVAsset``, delegate: self)
- 實現(xiàn)視頻導出結果協(xié)議
ExporterDelegate
涮雷。
/// Video export successed.
/// - Parameters:
/// - exporter: VideoExporter
/// - videoURL: Export the successful video url, Be equivalent to outputURL.
func export(_ exporter: Kakapos.Exporter, success videoURL: URL) {
self.view.hideAllToasts()
let player = AVPlayer(url: videoURL)
let vc = AVPlayerViewController()
vc.player = player
self.present(vc, animated: true) {
vc.player?.play()
}
}
/// Video export failure.
/// - Parameters:
/// - exporter: VideoExporter
/// - error: Failure error message.
func export(_ exporter: Kakapos.Exporter, failed error: Kakapos.Exporter.Error) {
// do someing..
}
- 轉換視頻緩沖區(qū)
CVPixelBuffer
并添加過濾器。
let filters: [C7FilterProtocol] = [
C7Flip(horizontal: true, vertical: false),
C7ColorConvert(with: .gray),
C7SoulOut(soul: 0.3),
MPSGaussianBlur(radius: 5),
]
let exporter = Exporter.init(videoURL: ``URL Link``, delegate: self)
exporter.export(outputURL: outputURL) {
let dest = BoxxIO(element: $0, filters: filters)
return try? dest.output()
}
或者
let martix = C7ColorMatrix4x4(matrix: Matrix4x4.Color.gray)
let screen = C7SplitScreen(type: .two)
let exporter = Exporter.init(videoURL: ``URL Link``, delegate: self)
exporter.export(outputURL: outputURL) { $0 ->> martix ->> screen }
最后
- 再附上一個Metal濾鏡庫HarbethDemo地址轻局,目前包含
100+
種濾鏡洪鸭,同時也支持CoreImage混合使用。 - 再附上一個開發(fā)加速庫KJCategoriesDemo地址
- 再附上一個網絡基礎庫RxNetworksDemo地址
- 喜歡的老板們可以點個星??仑扑,謝謝各位老板@谰簟!镇饮!
??.