草庐IT

ios - AVAssetWriter::append() 返回 false,没有额外信息(How to do segmenting of captured input to video-chunks with AVFoundation?)

coder 2023-09-29 原文

我正在构建一个使用 AVFoundation 录制视频和音频的功能。我会录制几个小时,但我想将 block 上传到我们的后端,以便我们可以构建一个实时 HLS 播放列表(在对片段进行 som 处理之后)。

首先,某处是否有样本在执行此操作?我还没有找到任何引用实现可以这么说......

这是我的看法:

  • 按照网络上的文档和示例设置 AVCaptureSession 非常简单。
  • 我实现了 AVCaptureVideoDataOutputSampleBufferDelegateAVCaptureAudioDataOutputSampleBufferDelegate 以访问样本缓冲区
  • 我有两个 AVAssetWriter 并在它们之间切换,一个是 mediaType == AVMediaTypeVideo 另一个是 mediaType == AVMediaTypeAudio
  • 使用 CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer) 我通常在 5 秒后在编写器之间切换。

问题:

  1. 有时,当调用 AVAssetWriter::append() 时,它会失败 并返回 false
  2. 根据文档,应该检查AVAssetWriter.status
    在这种情况下,它被设置为 AVAssetWriterStatusFailed 并且更多信息应该在 AVAssetWriter.error
  3. 中可用
  4. AVAssetWriter.error 设置为 可选(错误域=AVFoundationErrorDomain 代码=-11800\"操作无法完成\"UserInfo={NSUnderlyingError=0x14e73dc10 {错误域=NSOSStatusErrorDomain 代码=-16364\"(null)\"},NSLocalizedFailureReason=An发生未知错误 (-16364),NSLocalizedDescription=操作无法完成})
  5. AVFoundationErrorDomain.code 11800 表示 AVErrorUnknown

有人遇到过同样的问题或知道如何找到更多信息吗?

最后,当我在 writer 之间切换时,从我调用 AVAssetWriter.startWriting() 开始有一段时间(我在创建空闲 writer 时这样做,然后是时候切换到新段)到我调用 AVAssetWriter.startSession(atSourceTime: startTime) 时。在此期间,我需要坚持使用 sampleBuffers(通常是音频)。我只是复制一份

 var copiedBuffer: CMSampleBuffer?
 CMSampleBufferCreateCopy(nil, sampleBuffer, &copiedBuffer)
 guard copiedBuffer != nil else {
    throw VideoWriterError.failedToCopyBuffer
 }
 pendingSampleBuffers.append((isVideo, copiedBuffer))

AVAssetWriter.StartSession() 之后,我将它们写入新编写器:

    while !pendingSampleBuffers.isEmpty {
        let (isVideo, sampleBufferOpt) = pendingSampleBuffers.removeFirst()
        guard let sampleBuffer = sampleBufferOpt else {
            throw VideoWriterError.failedToCopyBuffer
        }
        try capturedFrame(sampleBuffer: sampleBuffer, isVideo: isVideo)
    }

好像可以,但是有很多帖子说copying is shallow。我想知道这是否与我的问题有关?样本缓冲区存在于某种池中?

最佳答案

If false is returned, clients can check the value of the AVAssetWriter status property to determine whether the writing operation completed, failed, or was cancelled. If the status is failed, The AVAssetWriter error property will contain an instance of NSErrorß that describes the failure.

来自 append(_:) .

希望对您有所帮助。请对改进发表评论。

关于ios - AVAssetWriter::append() 返回 false,没有额外信息(How to do segmenting of captured input to video-chunks with AVFoundation?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42125302/

有关ios - AVAssetWriter::append() 返回 false,没有额外信息(How to do segmenting of captured input to video-chunks with AVFoundation?)的更多相关文章

随机推荐