AVAssetWriter的第一帧要么是空白的,要么是黑色的。
创始人
2024-11-12 20:30:14
0

要解决AVAssetWriter的第一帧要么是空白的,要么是黑色的问题,可以通过以下代码示例来处理:

  1. 首先,创建一个AVAssetWriter对象和一个AVAssetWriterInput对象,并配置它们以写入视频数据:
guard let videoOutputURL = // 设置输出URL
    let assetWriter = try? AVAssetWriter(url: videoOutputURL, fileType: AVFileType.mov),
    let assetWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoSettings) else {
        return
}

// 配置AVAssetWriterInput
assetWriterInput.expectsMediaDataInRealTime = true

// 添加AVAssetWriterInput到AVAssetWriter
assetWriter.add(assetWriterInput)
  1. 在开始写入数据之前,使用AVAssetWriterInputPixelBufferAdaptor来创建一个CVPixelBufferPool和一个CVPixelBuffer:
let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: assetWriterInput, sourcePixelBufferAttributes: nil)

// 创建一个CVPixelBufferPool
let pixelBufferAttributes: [String: Any] = [
    kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB,
    kCVPixelBufferWidthKey as String: videoSize.width,
    kCVPixelBufferHeightKey as String: videoSize.height
]
var pixelBufferPool: CVPixelBufferPool?
CVPixelBufferPoolCreate(nil, nil, pixelBufferAttributes as CFDictionary, &pixelBufferPool)

// 创建一个CVPixelBuffer
var pixelBuffer: CVPixelBuffer?
CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool!, &pixelBuffer)
  1. 在开始写入数据之前,先将第一帧的CVPixelBuffer填充为纯黑色:
if let pixelBuffer = pixelBuffer {
    CVPixelBufferLockBaseAddress(pixelBuffer, [])
    let data = CVPixelBufferGetBaseAddress(pixelBuffer)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
    let width = CVPixelBufferGetWidth(pixelBuffer)
    let height = CVPixelBufferGetHeight(pixelBuffer)
    let context = CGContext(data: data, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)
    context?.setFillColor(UIColor.black.cgColor)
    context?.fill(CGRect(x: 0, y: 0, width: width, height: height))
    CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
}

// 在开始写入数据之前,将第一帧的CVPixelBuffer添加到AVAssetWriterInput
if let pixelBuffer = pixelBuffer {
    pixelBufferAdaptor.append(pixelBuffer, withPresentationTime: kCMTimeZero)
}
  1. 现在可以开始将实际的视频帧写入AVAssetWriterInput了。在写入视频帧之前,需要将每个视频帧添加到CVPixelBuffer中,并将其添加到AVAssetWriterInput中:
func writeVideoFrame(sampleBuffer: CMSampleBuffer) {
    if let pixelBuffer = pixelBufferAdaptor.pixelBuffer(forAppendTo: assetWriterInput, withPresentationTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) {
        // 将sampleBuffer的图像数据添加到pixelBuffer中
        // 这一步根据你的具体需求来实现,可以使用CVPixelBuffer的各种方法来处理图像数据
        // 可以使用Core Graphics、Core Image、Metal等框架来处理图像数据
        // ...
        
        // 将pixelBuffer添加到AVAssetWriterInput
        pixelBufferAdaptor.append(pixelBuffer, withPresentationTime: CMSampleBufferGetPresentationTimeStamp(sampleBuffer))
    }
}

通过以上步骤,可以确保AVAssetWriter的第一帧不再是空白的或者黑色的。

相关内容

热门资讯

一分钟了解!德扑圈透视,wep... 一分钟了解!德扑圈透视,wepoker插件下载(分享透视开挂辅助挂)是一款可以让一直输的玩家,快速成...
今日公布!pokerworld... 今日公布!pokerworld修改器,wepoker数据分析(必备透视开挂辅助神器)是一款可以让一直...
玩家必看科普!hhpoker德... 玩家必看科普!hhpoker德州挂真的有,werplan可以透视(解密透视开挂辅助工具);1、选手技...
科普攻略!有人wepoker,... 科普攻略!有人wepoker,约局吧德州真的存在透视(系统透视开挂辅助插件)是一款可以让一直输的玩家...
揭秘关于!wepoker透视方... 揭秘关于!wepoker透视方法,pokemmo手机版修改器(线上透视开挂辅助插件)是一款可以让一直...
2分钟细说!拱趴大菠萝开挂方法... 2分钟细说!拱趴大菠萝开挂方法,hhpoker是真的假的(科技透视开挂辅助工具);运拱趴大菠萝开挂方...
指导大家!hhpoker有透视... 指导大家!hhpoker有透视挂的,wepoker的辅助器(安装透视开挂辅助挂);指导大家!hhpo...
如何分辨真伪!wepoker可... 您好,wepoker可以买辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
详细说明!pokerworld... 详细说明!pokerworld辅助器,有哪些免费的wpk透视挂码(方法透视开挂辅助脚本)是一款可以让...
必备辅助推荐!拱趴大菠萝有挂,... 必备辅助推荐!拱趴大菠萝有挂,wepoker透视版下载(插件透视开挂辅助器)是一款可以让一直输的玩家...