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

相关内容

热门资讯

攻略讲解!掌中乐游戏中心有辅助... 攻略讲解!掌中乐游戏中心有辅助吗(透视)一直是有挂(2020已更新)(哔哩哔哩)1、掌中乐游戏中心有...
九分钟了解!心悦麻将软挂神器,... 九分钟了解!心悦麻将软挂神器,八闽十三张十三水有外挂吗,AI教程(有挂软件);1、很好的工具软件,可...
必备教程!wepoke打伙牌(... 必备教程!wepoke打伙牌(wepoke真的有挂)wepoke(好像真的有挂)需要回顾用户提供的搜...
教程攻略!八闽状元郎在哪里开挂... 教程攻略!八闽状元郎在哪里开挂(透视)一直是有挂(2020已更新)(哔哩哔哩)1、让任何用户在无需八...
we辅助poker德之星!we... we辅助poker德之星!wepoke软件透明,wepOke原来真的有挂,2025版教程(有挂揭秘)...
五分钟了解!掌中乐游戏中心吗,... 五分钟了解!掌中乐游戏中心吗,兴动辅助器免费版v3.0,技巧教程(有挂ai代打)1、兴动辅助器免费版...
微扑克系统发牌规律!微扑克辅助... 微扑克系统发牌规律!微扑克辅助是真的,微扑克可以加入俱乐部,揭秘攻略(有挂脚本)1、这是跨平台的微扑...
技术分享!狂飙娱乐辅助器(辅助... 技术分享!狂飙娱乐辅助器(辅助)好像真的有挂(2023已更新)(哔哩哔哩)1)狂飙娱乐辅助器辅助挂:...
七分钟了解!闲玩游戏外 挂,蛮... 七分钟了解!闲玩游戏外 挂,蛮籽麻将有没有套路,AI教程(有挂解密);暗藏猫腻,小编详细说明蛮籽麻将...
wepoke有挂!wepoke... wepoke有挂!wepoke模拟器,wepoke有挂,透明教程(有挂介绍);亲真的是有正版授权,小...