我使用Gpuimage过滤器创建了视频过滤器演示,用于将过滤器应用于视频。下面是我的亮度滤镜代码。视频在某个时候保存,但大部分时间movieWritter无法完成finishRecording或completionBlock,同时moviefile完成处理。最后,由于CPU和内存使用率过高,app终止。
let finalpath = "(FileManager.default.finalCompositions)/composition(getTimeStamp).mp4"
let finalUrl = URL(fileURLWithPath: finalpath)
let asset: AVURLAsset = AVURLAsset(url: self.videoUrl!)
let asetTrack: AVAssetTrack = asset.tracks(withMediaType: AVMediaTypeVideo)[0]
self.exportedMovieFile = GPUImageMovie(url: self.videoUrl)
self.exportedMovieFile?.runBenchmark = true
self.exportedMovieFile?.playAtActualSpeed = false
var exportfilter = GPUImageBrightnessFilter()
exportfilter.brightness = 0.5
self.exportedMovieFile?.addTarget(exportfilter)
let videosize: CGSize = CGSize(width: asetTrack.naturalSize.width, height: asetTrack.naturalSize.height)
self.exportedMovieWritter = GPUImageMovieWriter(movieURL: finalUrl, size: videosize)
exportfilter.addTarget(self.exportedMovieWritter)
//Configure this for video from the movie file, where we want to preserve all video frames and audio samples
self.exportedMovieWritter?.shouldPassthroughAudio = true
if asset.tracks(withMediaType: AVMediaTypeAudio).count > 0 {
self.exportedMovieFile?.audioEncodingTarget = self.exportedMovieWritter
}
else
{
self.exportedMovieFile?.audioEncodingTarget = nil
}
self.exportedMovieFile?.enableSynchronizedEncoding(using: self.exportedMovieWritter)
self.exportedMovieWritter!.startRecording()
self.exportedMovieFile?.startProcessing()
DispatchQueue.main.async {
self.timerProgress = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector:#selector(self.filterRetrievingProgress), userInfo: nil, repeats: true)
}
self.exportedMovieWritter?.failureBlock = {(err) in
loggingPrint("Error :: (err?.localizedDescription)")
}
self.exportedMovieWritter?.completionBlock = {() -> Void in
exportfilter.removeTarget(self.exportedMovieWritter)
self.exportedMovieWritter?.finishRecording(completionHandler: {
self.timerProgress?.invalidate()
self.timerProgress = nil
self.exportedMovieFile?.removeAllTargets()
self.exportedMovieFile?.cancelProcessing()
self.exportedMovieFile = nil
DispatchQueue.main.async {
let uploadViewController = UploadViewController.loadController()
uploadViewController.isPhoto = false
uploadViewController.videoUrl = finalUrl
self.navigationController?.pushViewController(uploadViewController, animated: true)
}
})
}
@objc fileprivate func filterRetrievingProgress() {
loggingPrint("Progess :: (self.exportedMovieFile?.progress)")
}