Created
December 3, 2018 16:21
-
-
Save delasign/22cff82d7cbbc38977344b9e084469a4 to your computer and use it in GitHub Desktop.
ARKit Audio-Video Recorder Asset Writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func prepareWriterAndInput(size:CGSize, videoURL:URL, completionHandler:@escaping(Error?)->()) { | |
do { | |
self.assetWriter = try AVAssetWriter(outputURL: videoURL, fileType: AVFileType.mp4) | |
// Input is the mic audio of the AVAudioEngine | |
let audioOutputSettings = [ | |
AVFormatIDKey : kAudioFormatMPEG4AAC, | |
AVNumberOfChannelsKey : 2, | |
AVSampleRateKey : 44100.0, | |
AVEncoderBitRateKey: 192000 | |
] as [String : Any] | |
self.audioInput = AVAssetWriterInput(mediaType: AVMediaType.audio, outputSettings: audioOutputSettings); | |
self.audioInput!.expectsMediaDataInRealTime = true | |
self.assetWriter?.add(self.audioInput!); | |
// self.audioInput. | |
// Video Input Creator | |
let videoOutputSettings: Dictionary<String, Any> = [ | |
AVVideoCodecKey : AVVideoCodecType.h264, | |
AVVideoWidthKey : size.width, | |
AVVideoHeightKey : size.height | |
]; | |
self.videoInput = AVAssetWriterInput (mediaType: AVMediaType.video, outputSettings: videoOutputSettings) | |
self.videoInput!.expectsMediaDataInRealTime = true | |
self.assetWriter!.add(self.videoInput!) | |
// Create Pixel buffer Adaptor | |
let sourceBufferAttributes:[String : Any] = [ | |
(kCVPixelBufferPixelFormatTypeKey as String): Int(kCVPixelFormatType_32ARGB), | |
(kCVPixelBufferWidthKey as String): Float(size.width), | |
(kCVPixelBufferHeightKey as String): Float(size.height)] as [String : Any] | |
self.pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: self.videoInput!, sourcePixelBufferAttributes: sourceBufferAttributes); | |
self.assetWriter?.startWriting(); | |
self.assetWriter?.startSession(atSourceTime: CMTime.zero); | |
completionHandler(nil); | |
} | |
catch { | |
print("Failed to create assetWritter with error : \(error)"); | |
completionHandler(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment