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

相关内容

热门资讯

现就发布提示!wpk俱乐部怎么... 现就发布提示!wpk俱乐部怎么作必弊(透视)果然真的是有辅助插件(有挂实锤)-哔哩哔哩1、首先打开w...
据玩家消息!智星德州插件怎么下... 据玩家消息!智星德州插件怎么下载(透视)确实真的有辅助插件(真是有挂)-哔哩哔哩1、智星德州插件怎么...
透视有挂!拱趴大菠萝怎么开挂(... 透视有挂!拱趴大菠萝怎么开挂(透视)都是有辅助工具(有挂规律)-哔哩哔哩拱趴大菠萝怎么开挂是不是有人...
据了解!大菠萝免费辅助器(透视... 据了解!大菠萝免费辅助器(透视)一直存在有辅助下载(今日头条)-哔哩哔哩1)大菠萝免费辅助器有没有挂...
透视免费!哈糖大菠萝开挂(透视... 透视免费!哈糖大菠萝开挂(透视)都是真的有辅助插件(有挂秘诀)-哔哩哔哩1、超多福利:超高返利,海量...
透视科技!来玩德州破解器(透视... 透视科技!来玩德州破解器(透视)总是真的是有辅助插件(有挂教程)-哔哩哔哩;1、进入到来玩德州破解器...
围绕透视问题!来玩app 德州... 围绕透视问题!来玩app 德州 辅助(透视)一直是有辅助平台(有挂实锤)-哔哩哔哩1、完成来玩app...
透视有挂!wejoker辅助软... 透视有挂!wejoker辅助软件价格(透视)竟然真的是有辅助插件(有挂秘笈)-哔哩哔哩1)wejok...
迎来新发展!hhpoker辅助... 迎来新发展!hhpoker辅助挂(透视)其实真的是有辅助安装(有挂秘笈)-哔哩哔哩1、让任何用户在无...
针对!pokemmo辅助脚本(... 针对!pokemmo辅助脚本(透视)确实是真的辅助软件(有挂实锤)-哔哩哔哩小薇(辅助器软件下载)致...