AVAssetWriter无法调用startWriting方法

时间:2017-12-19 06:20:51

标签: ios avfoundation

谢谢大家。

这是我设置的AVAssetWriter代码。

错误是 错误域= AVFoundationErrorDomain代码= -11800“”“”UserInfo = {NSLocalizedFailureReason =“”( - 12780),NSLocalizedDescription =“”,NSUnderlyingError = 0x1c465a310 {错误域= NSOSStatusErrorDomain代码= -12780“(null)”}}

 private func createWriter(assetURL: URL) {
    guard let assetWriter = try? AVAssetWriter.init(url: assetURL, fileType: AVFileType.mov) else { return }
    ...
    videoWriterInput = AVAssetWriterInput.init(mediaType: AVMediaType.video, outputSettings: outputSettings)
    videoWriterInput?.expectsMediaDataInRealTime = true
    videoWriterInput?.transform = videoWriterInput!.transform.rotated(by: CGFloat.pi / 2)
    audioWriterInput = AVAssetWriterInput.init(mediaType: AVMediaType.audio, outputSettings: nil)
    audioWriterInput?.expectsMediaDataInRealTime = true

    let SPBADictionary: [String: Any] = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
                                         kCVPixelBufferWidthKey as String: kScreenWidth,
                                         kCVPixelBufferHeightKey as String:kScreenHeight,
                                         kCVPixelFormatOpenGLESCompatibility as String: kCFBooleanTrue]
    assetWriterPixelBufferInput = AVAssetWriterInputPixelBufferAdaptor.init(assetWriterInput: videoWriterInput!, sourcePixelBufferAttributes: SPBADictionary)

    assetWriter.shouldOptimizeForNetworkUse = true
    if assetWriter.canAdd(videoWriterInput!) {
        assetWriter.add(videoWriterInput!)
    }
    if assetWriter.canAdd(audioWriterInput!) {
        assetWriter.add(audioWriterInput!)
    }

}

这是回调代码,我不知道为什么startWriting没有使用,AVAssetWriterStatus无法写入,所以它会崩溃并给我错误-11800,未知错误。

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    if !isRecoding { return }
    guard let assetWriter = assetWriter else { return }
    autoreleasepool {
        let currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)
        if assetWriter.status != .writing {
            assetWriter.startWriting()
            assetWriter.startSession(atSourceTime: currentSampleTime)
        }

        if assetWriter.status != .writing{
            print("Warning: writer status is \(assetWriter.status.rawValue)")
            if assetWriter.status == AVAssetWriterStatus.failed {
                print(assetWriter.error ?? "")
                return
            }
        }

        if output == videoOutput {
            if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer),videoWriterInput?.isReadyForMoreMediaData ?? false {
                print("videoWriterInput")
                assetWriterPixelBufferInput?.append(pixelBuffer, withPresentationTime: currentSampleTime)
            }
        }
        if output == audioOutput {
            if audioWriterInput?.isReadyForMoreMediaData ?? false {
                print("audioWriterInput")
                audioWriterInput?.append(sampleBuffer)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

导致" startWriting()"的原因之一失败,是给定的URL无效。我建议你看看:

  1. URL中指定的文件不存在。 (确认一下 FileManager.fileExists(atPath:)。)
  2. 该网址的所有目录都有 已创建。 (使用FileManager.createDirectory(at:withIntermediateDirectories:attributes:)显式创建它们。)