谢谢大家

这是我设置的AVAssetWriter代码。

错误是
错误域= AVFoundationErrorDomain代码= -11800“”“” UserInfo = {NSLocalizedFailureReason =“”(-12780),NSLocalizedDescription =“”,NSUnderlyingError = 0x1c465a310 {Error Domain = 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)
            }
        }
    }
}

最佳答案

导致“ startWriting()”失败的原因之一是给定的URL无效。我建议您看一下:


URL中指定的文件不存在。 (确认
FileManager.fileExists(atPath:)。)
该URL的所有目录都有
被创建。 (使用FileManager.createDirectory(at:withIntermediateDirectories:attributes:)明确创建它们。)

关于ios - AVAssetWriter无法调用方法startWriting,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47881086/

10-08 21:00