I am developing video based application, where I need to add CIFilter to the captured video selected from device gallery. For this I am using below VideoEffects library:
https://github.com/FlexMonkey/VideoEffects
Using this, I can able to add filters to my video, but the problem is Audio is missing in the final video output. I tried below code to add Audio assets but not working:
videoOutputURL = documentDirectory.appendingPathComponent("Output_\(timeDateFormatter.string(from: Date())).mp4")
do {
videoWriter = try AVAssetWriter(outputURL: videoOutputURL!, fileType: AVFileTypeMPEG4)
}
catch {
fatalError("** unable to create asset writer **")
}
let outputSettings: [String : AnyObject] = [
AVVideoCodecKey: AVVideoCodecH264 as AnyObject,
AVVideoWidthKey: currentItem.presentationSize.width as AnyObject,
AVVideoHeightKey: currentItem.presentationSize.height as AnyObject]
guard videoWriter!.canApply(outputSettings: outputSettings, forMediaType: AVMediaTypeVideo) else {
fatalError("** unable to apply video settings ** ")
}
videoWriterInput = AVAssetWriterInput(
mediaType: AVMediaTypeVideo,
outputSettings: outputSettings)
//setup audio writer
let audioOutputSettings: Dictionary<String, AnyObject> = [
AVFormatIDKey : Int(kAudioFormatMPEG4AAC) as AnyObject,
AVSampleRateKey:48000.0 as AnyObject,
AVNumberOfChannelsKey:NSNumber(value: 1),
AVEncoderBitRateKey : 128000 as AnyObject
]
guard videoWriter!.canApply(outputSettings: audioOutputSettings, forMediaType: AVMediaTypeAudio) else {
fatalError("** unable to apply Audio settings ** ")
}
audioWriterInput = AVAssetWriterInput(
mediaType: AVMediaTypeAudio,
outputSettings: audioOutputSettings)
if videoWriter!.canAdd(videoWriterInput!) {
videoWriter!.add(videoWriterInput!)
videoWriter!.add(audioWriterInput!)
}
else {
fatalError ("** unable to add input **")
}
Is there any other way to add filter to video? Please suggest me.
Also I tried to use GPUImage to add CIFilter, but this is working only for Live video, not captured video.