ios - 如何在 iOS 上将编码的 CMSampleBuffer 样本保存到 mp4 文件

标签 ios swift avcapturesession cmsamplebuffer video-toolbox

我正在使用 VideoToolbox 框架从 AVCaptureSession 检索数据并将其编码为 h264 和 acc。

我现在:

  1. 使用委托(delegate)方法获取数据func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
  2. 将视频 CMSampleBuffer 转换为 CVImageBuffer 并使用 VTCompressionSession 将其编码为 h264,该过程在 VTCompressionOutputCallback 中传递编码帧
  3. 将音频 CMSampleBuffer 编码为 acc 并将其作为 UnsafeMutableAudioBufferListPointer 传递。

我现在被阻止,我想将这些流正确转换为 Data 字节,以通过 FileHandle 写入 URL。我该怎么办?

我需要在实际文件最终确定之前访问编码流,因此我无法使用(我认为)AVAssetWriter 将未编码流保存到 mp4 文件因为据我所知,我需要将未编码的样本传递给 AVAssetWriter 才能正常工作。还是我错了?

最佳答案

事实证明,您可以使用 AVAssetWriter 保存编码的 CMSampleBuffer,但您需要将 nil 作为 AVAssetWriterInput 中的 outputSettings 传递防止重新编码数据:

let input AVAssetWriterInput(mediaType: mediaType, outputSettings: nil, sourceFormatHint: sourceFormatHint)

关于ios - 如何在 iOS 上将编码的 CMSampleBuffer 样本保存到 mp4 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255336/

相关文章:

ios - 滑动 View 就像iOS中的whatsapp(android)

ios - AvFoundation - 如何连接 AVCaptureSession 和 AVAssetWriter?

ios - withSecurityScope 在 NSURL.BookmarkCreationOptions 中不可用

ios - swift 4 错误 : "Cannot convert value of type ' URLRequest' to expected argument type 'URL!' "

ios - 使用 swift 在 iOS 8 应用程序中显示游戏中心挑战而不是排行榜

ios - 向后导航时相机进给卡住 - ios,objective-c

ios - AVCaptureVideoPreviewLayer 未填满屏幕

ios - 在 Storyboard 上工作时 Storyboard UI 不更新

ios - 如何从 WiFi 设置将代理密码传递给应用程序

ios - 如何在 iOS 中使 JSON 文件可本地化?