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的第一帧不再是空白的或者黑色的。

相关内容

热门资讯

透视游戏"广东闲来辅... 透视游戏"广东闲来辅助软件官方"开挂(工具)辅助工具有挂技巧-力荐教程;无需打开直接搜索薇:1367...
揭秘几款"wepok... wepoker高级辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepoker高级辅助作为一种...
盘点十款"温州茶苑手... 盘点十款"温州茶苑手机版辅助器"开挂(神器)辅助神器了解有挂-2026新版教程1、下载安装好温州茶苑...
必备科技"多乐够级辅... 必备科技"多乐够级辅助软件"开挂(透视)辅助透视新版有挂-安装教程【无需打开直接搜索加薇136704...
透视好友"中至余干六... 透视好友"中至余干六副里"开挂(软件)辅助软件详细教程-攻略方法 了解更多开挂安装加(1367043...
实测发现“好运娱乐辅助器”固有... 实测发现“好运娱乐辅助器”固有有辅助开挂平台(有挂透明挂);无需打开直接搜索加(薇:13670430...
透视线上“逗娱碰胡开挂”起初有... 透视线上“逗娱碰胡开挂”起初有开挂辅助插件(有挂实锤);无需打开直接搜索加薇136704302(咨询...
八分钟辅助“约局吧能不能开挂”... 八分钟辅助“约局吧能不能开挂”开挂(平台)辅助平台科技教程-有挂教程 【无需打开直接搜索加薇1367...
1.9分钟了解“wepoker... 1.9分钟了解“wepoker轻量版透视”原先有辅助下载(有挂详细) >>您好:软件加薇136704...
玩家必看!wejoker私人辅... 玩家必看!wejoker私人辅助软件,开心网辅助工具,AI教程(必看开挂辅助插件);打开点击测试直接...