###前提・実現したいこと
Swiftで動画撮影を続けながら、一定時間ごとに自動保存したいです。
音声は必要ありません。
AVCaptureOutputとAVAssetWriterを使って試行錯誤しているのですが、
書き出すと撮影が止まってしまったり、上手くいきません。
ご教授いただけたら幸いです。
###該当のソースコード
import UIKit import AVFoundation class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { fileprivate var writer: AVAssetWriter! fileprivate var videoInput: AVAssetWriterInput! fileprivate var audioInput: AVAssetWriterInput! var counter:Int = 0 override func viewDidLoad() { super.viewDidLoad() setupCameraSession() // データ保存のパス let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] as String let filePath : String? = "(documentsDirectory)/temp.mov" if FileManager.default.fileExists(atPath: filePath!) { try? FileManager.default.removeItem(atPath: filePath!) } writer = try? AVAssetWriter(outputURL: URL(fileURLWithPath: filePath!), fileType: AVFileTypeQuickTimeMovie) // Video入力 let videoOutputSettings: Dictionary<String, AnyObject> = [ AVVideoCodecKey : AVVideoCodecH264 as AnyObject, AVVideoWidthKey : 789 as AnyObject, AVVideoHeightKey : 1280 as AnyObject ]; videoInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoOutputSettings) videoInput.expectsMediaDataInRealTime = true writer.add(videoInput) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) view.layer.addSublayer(previewLayer) writer?.startWriting() writer?.startSession(atSourceTime: kCMTimeZero) cameraSession.startRunning() } lazy var cameraSession: AVCaptureSession = { let s = AVCaptureSession() s.sessionPreset = AVCaptureSessionPresetLow return s }() lazy var previewLayer: AVCaptureVideoPreviewLayer = { let preview = AVCaptureVideoPreviewLayer(session: self.cameraSession) preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height) preview?.position = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY) preview?.videoGravity = AVLayerVideoGravityResize return preview! }() func setupCameraSession() { let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) as AVCaptureDevice do { let deviceInput = try AVCaptureDeviceInput(device: captureDevice) cameraSession.beginConfiguration() if (cameraSession.canAddInput(deviceInput) == true) { cameraSession.addInput(deviceInput) } let dataOutput = AVCaptureVideoDataOutput() dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange as UInt32)] dataOutput.alwaysDiscardsLateVideoFrames = true if (cameraSession.canAddOutput(dataOutput) == true) { cameraSession.addOutput(dataOutput) } cameraSession.commitConfiguration() let queue = DispatchQueue(label: "com.test.keiteratail") dataOutput.setSampleBufferDelegate(self, queue: queue) } catch let error as NSError { NSLog("(error), (error.localizedDescription)") } } func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) { // Here you collect each frame and process it print("write") videoInput?.append(sampleBuffer!) counter = counter + 1 fileCounter = fileCounter + 1 if(counter > 10){ writer.finishWriting(completionHandler: { print(self.writer.outputURL) // データ保存のパスを生成 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] as String let filePath : String? = "(documentsDirectory)/temp_(self.counter).mov" if FileManager.default.fileExists(atPath: filePath!) { try? FileManager.default.removeItem(atPath: filePath!) } writer = try? AVAssetWriter(outputURL: URL(fileURLWithPath: filePath!), fileType: AVFileTypeQuickTimeMovie) writer?.startWriting() writer?.startSession(atSourceTime: kCMTimeZero) }) counter = 0 } } }
###試したこと
・AVAssetWriterを2つ作って交互に書き出し、writerが再生成できない
・didOutputSampleBufferでUIImageを生成、パラパラ漫画の要領→10秒ほどでアプリが落ちる
###補足情報(言語/FW/ツール等のバージョンなど)
Swift4 Xcode9.0
このコードはSwift4.0でしょうか?
あなたの回答
tips
プレビュー