gpt4 book ai didi

ios - 像素缓冲区不会附加到设备上的 AVAssetWriterInput

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:56 28 4
gpt4 key购买 nike

我正在尝试拍摄图像并将其转换为 Swift 中的视频文件。我能够让代码在 iOS 模拟器中运行,但它无法在实际设备上运行。当我尝试将像素缓冲区附加到 AVAssetWriterInput 时出错。我想知道这里是否有人以前遇到过同样的问题。这是代码:

func createVideoFromImage(img: CGImageRef) -> Void {

var error: NSError?
let frameSize = CGSizeMake(CGFloat(CGImageGetWidth(img)), CGFloat(CGImageGetHeight(img)))

let fileName = "\(uniqueString()).m4v"
let assetWriter = AVAssetWriter(URL: fileUrl(fileName), fileType: AVFileTypeAppleM4V, error: &error)

let outputSettings = [
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: frameSize.width,
AVVideoHeightKey: frameSize.height
]

let assetWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)

let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: assetWriterInput, sourcePixelBufferAttributes: nil)

if assetWriter.canAddInput(assetWriterInput) {
assetWriter.addInput(assetWriterInput)
}

assetWriter.startWriting()
assetWriter.startSessionAtSourceTime(kCMTimeZero)

let buffer:CVPixelBufferRef = pixelBufferFromCGImage(img, frameSize: frameSize)

if pixelBufferAdaptor.assetWriterInput.readyForMoreMediaData {
let frameTime = CMTimeMakeWithSeconds(0, 30)
let pixelBufferAppend = pixelBufferAdaptor.appendPixelBuffer(buffer, withPresentationTime: frameTime)

if pixelBufferAppend {
assetWriterInput.markAsFinished()

assetWriter.finishWritingWithCompletionHandler { () -> Void in
switch assetWriter.status {
case AVAssetWriterStatus.Failed:
println("Error: \(assetWriter.error)")
default:
let path = self.fileUrl(fileName).path!
let content = NSFileManager.defaultManager().contentsAtPath(path)
println("Video: \(path) \(content?.length)")
}
}
} else {
println("failed to append pixel buffer")
}
}


}

func pixelBufferFromCGImage (img: CGImageRef, frameSize: CGSize) -> CVPixelBufferRef {

let options = [
"kCVPixelBufferCGImageCompatibilityKey": true,
"kCVPixelBufferCGBitmapContextCompatibilityKey": true
]

var pixelBufferPointer = UnsafeMutablePointer<Unmanaged<CVPixelBuffer>?>.alloc(1)

let buffered:CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, UInt(frameSize.width), UInt(frameSize.height), OSType(kCVPixelFormatType_32ARGB), options, pixelBufferPointer)

let lockBaseAddress = CVPixelBufferLockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0)
var pixelData:UnsafeMutablePointer<(Void)> = CVPixelBufferGetBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue())

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)
let space:CGColorSpace = CGColorSpaceCreateDeviceRGB()

var context:CGContextRef = CGBitmapContextCreate(pixelData, UInt(frameSize.width), UInt(frameSize.height), 8, CVPixelBufferGetBytesPerRow(pixelBufferPointer.memory?.takeUnretainedValue()), space, bitmapInfo)

CGContextDrawImage(context, CGRectMake(0, 0, frameSize.width, frameSize.height), img)

CVPixelBufferUnlockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0)

return pixelBufferPointer.memory!.takeUnretainedValue()
}

pixelBufferAppend 返回 false!任何帮助将不胜感激。我整晚都在为这个问题绞尽脑汁!

最佳答案

我终于明白是怎么回事了。该设备只能支持最大 1920x1080 像素的编码帧 - 减小了图像大小并且它起作用了。将 frameSize 更改为变量并使用以下代码:

if frameSize.width > maxImageSize.width || frameSize.height > maxImageSize.height {
let scale = min((maxImageSize.width/frameSize.width), (maxImageSize.height/frameSize.height))
frameSize = CGSizeMake((scale * frameSize.width), (scale * frameSize.height))
}

关于ios - 像素缓冲区不会附加到设备上的 AVAssetWriterInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28261223/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com