使用Swift进行视频缓冲输出

时间:2015-05-06 17:05:58

标签: ios swift avfoundation

我的目标是获取视频缓冲区并最终将其转换为NSData,但我不明白如何正确访问缓冲区。我有captureOutput函数但是如果转换缓冲区我没有成功,我不确定我实际上是在缓冲区中收集任何东西。这都是使用swift代码,我已经找到了一些使用Objective-C的例子,但是我无法理解Obj-c代码以便弄明白。

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.

    }

1 个答案:

答案 0 :(得分:1)

AVCaptureVideoDataOutputSampleBufferDelegate方法captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)中,您可以获取缓冲区信息

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)")