swift - 如何创建 CMVideoFormatDescription 对象

标签 swift avfoundation core-video

我从 ARSessionDelegate 得到一个 CVPixelBuffer。

我的应用程序还有另一个不可更改的部分,我需要一个 CMSampleBuffer 对象。所以我试图从 CVPixelBuffer 创建一个 CMSampleBuffer。

我正在使用这种方法来创建 CMSampleBuffer:

func CMSampleBufferCreateReadyWithImageBuffer(_ allocator: CFAllocator?, 
                                        _ imageBuffer: CVImageBuffer, 
                                        _ formatDescription: CMVideoFormatDescription, 
                                        _ sampleTiming: UnsafePointer<CMSampleTimingInfo>, 
                                        _ sBufOut: UnsafeMutablePointer<CMSampleBuffer?>) -> OSStatus

这个函数有 5 个参数:

  1. CFAllocator - 我认为这不是必需的。
  2. imageBuffer - 这是 CVPixelBuffer。
  3. CMVideoFormatDescription - 如何正确创建它?
  4. sampleTiming:我可以弄清楚如何使用 this answer 创建它.
  5. sBufOut - 只是指向我要创建的 CMSampleBuffer 对象的指针。

这是我创建 CMVideoFormatDescription 的尝试:

    let w = CVPixelBufferGetWidth(pixelBuffer)
    let h = CVPixelBufferGetHeight(pixelBuffer)
    var format: CMVideoFormatDescription?
    CMVideoFormatDescriptionCreate(nil, kCMVideoCodecType_HEVC, Int32(w), Int32(h), nil, &format)

我很确定我不应该对 kCMVideoCodecType_HEVC 进行硬编码,但我不确定如何确定编解码器类型。

最佳答案

使用 CMVideoFormatDescriptionCreateForImageBuffer 直接从 CVPixelBuffer(它是一个 CVImageBuffer)创建格式描述。

关于swift - 如何创建 CMVideoFormatDescription 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52901220/

相关文章:

swift - 如何将工具栏放置在滚动 TableView 上方(或下方)

iOS ffmpeg 如何运行命令来修剪远程 url 视频?

ios - MPMediaItem 获取精确的文件大小

ios - AVPlayerItemVideoOutput copyPixelBufferForItemTime针对特定视频在iOS上给出了不正确的CVPixelBufferRef

ios - 放置在覆盖层上时 MapKit 引脚标题不可见?

ios - UITableView 滚动到单元格的顶部

ios - 从 dequeueReusableCellWithIdentifier 获取 nil

objective-c - AVCaptureVideoPreviewLayer 不显示

ios - 将原始数据转换为适用于 iOS 的可显示视频

macos - 核心视频像素缓冲区为 GL_TEXTURE_2D