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

相关内容

热门资讯

程序员教你!超凡辅助app链接... 程序员教你!超凡辅助app链接(白金岛手游放炮罚有挂)切实有作弊器开挂辅助工具(有挂猫腻);白金岛手...
玩家必看教程!518互游辅助(... 【福星临门,好运相随】;玩家必看教程!518互游辅助(如何做方片十三张脚本)从前有作弊器开挂辅助黑科...
玩家必看教程!新海豚辅助工具(... 玩家必看教程!新海豚辅助工具(微友助手破解版)好像有作弊挂开挂辅助神器(有挂秘笈);1、让任何用户在...
分享一款!新众亿软件可以安装(... 分享一款!新众亿软件可以安装(福州十八扑外卦)一向有作弊挂开挂辅助下载(今日头条);分享一款!新众亿...
实测教程!新财神正版辅助挂(游... 实测教程!新财神正版辅助挂(游戏辅助器免费版)其实有作弊挂开挂辅助插件(有挂辅助);游戏辅助器免费版...
玩家必看!新九哥智能辅助软件(... 玩家必看!新九哥智能辅助软件(小程序能不能开挂)确实有作弊器开挂辅助神器(有挂技巧);新九哥智能辅助...
信息共享!火神工作室辅助大全(... 信息共享!火神工作室辅助大全(朋友圈辅助软件)果然有作弊挂开挂辅助神器(有挂工具);一、火神工作室辅...
玩家必看科普!樱花之盛免费挂(... 玩家必看科普!樱花之盛免费挂(边锋老友辅助)固有有作弊挂开挂辅助脚本(有挂秘籍);亲真的是有正版授权...
总算清楚!天天微友辅助神器(上... 总算清楚!天天微友辅助神器(上饶打炸辅助)最初有作弊器开挂辅助工具(确实有挂);天天微友辅助神器 a...
科技新动态!微友辅助神器下载(... 科技新动态!微友辅助神器下载(人海大厅脚本)最初有作弊挂开挂辅助神器(有挂头条);科技新动态!微友辅...