问题描述
我的目标是获取视频缓冲区并将其最终转换为NSData,但我不了解如何正确访问该缓冲区.我具有captureOutput函数,但是如果转换缓冲区,则无法成功执行,而且我不确定是否实际上在缓冲区中收集了任何东西.这全都是使用快速代码的,我已经找到了一些使用Objective-C的示例,但是我不能很好地理解Obj-c代码以弄清楚它.
My goal is to take the video buffer and ultimately convert it to NSData but I do not understand how to access the buffer properly. I have the captureOutput function but I have not been successful if converting the buffer and I'm not sure I am actually collecting anything in the buffer. This is all using swift code, I have found some examples using Objective-C but I am not able to understand the Obj-c code well enough to figure it out.
var captureDevice : AVCaptureDevice?
var videoCaptureOutput = AVCaptureVideoDataOutput()
var bounds: CGRect = UIScreen.mainScreen().bounds
let captureSession = AVCaptureSession()
var captureConnection: AVCaptureMovieFileOutput?
override func viewDidLoad() {
super.viewDidLoad()
captureSession.sessionPreset = AVCaptureSessionPreset640x480
let devices = AVCaptureDevice.devices()
for device in devices {
if (device.hasMediaType(AVMediaTypeVideo)) {
if device.position == AVCaptureDevicePosition.Back {
captureDevice = device as? AVCaptureDevice
if captureDevice != nil {
beginSession()
}
}
}
}
}
func beginSession() {
var screenWidth:CGFloat = bounds.size.width
var screenHeight:CGFloat = bounds.size.height
var err : NSError? = nil
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err)!)
if err != nil {
println("Error: \(err?.localizedDescription)")
}
videoCaptureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
videoCaptureOutput.alwaysDiscardsLateVideoFrames = true
captureSession.addOutput(videoCaptureOutput)
videoCaptureOutput.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))
if captureSession.canAddOutput(self.videoCaptureOutput) {
captureSession.addOutput(self.videoCaptureOutput)
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
// I think this is where I can get the buffer info.
}
推荐答案
在AVCaptureVideoDataOutputSampleBufferDelegate
方法captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
中,您可以获得缓冲区信息
In the AVCaptureVideoDataOutputSampleBufferDelegate
method, captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
, you can get the buffer info
let formatDescription: CMFormatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)
let imageBuffer: CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer, 0)
var imagePointer: UnsafeMutablePointer<Void> = CVPixelBufferGetBaseAddress(imageBuffer)
let bufferSize: (width: Int, height: Int) = (CVPixelBufferGetHeight(imageBuffer), CVPixelBufferGetWidth(imageBuffer))
println("Buffer Size: \(bufferSize.width):\(bufferSize.height)")
这篇关于Swift的视频缓冲器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!