Swift 的视频缓冲输出

Posted

技术标签:

【中文标题】Swift 的视频缓冲输出【英文标题】:Video Buffer Output with Swift 【发布时间】:2015-05-06 17:05:57 【问题描述】:

我的目标是获取视频缓冲区并最终将其转换为 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】:

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

【讨论】:

以上是关于Swift 的视频缓冲输出的主要内容,如果未能解决你的问题,请参考以下文章

为啥视频没有播放,只在 swift 中显示加载程序

Windows,如何直接渲染到视频扫描输出内存?

大数据:Mapper输出缓冲区MapOutputBuffer

FFMPEG:为输出流 0:1 缓冲的数据包过多

Flash 视频播放器缓冲区问题和大型视频文件

V4L2捕捉画面+H264压缩视频+帧缓冲显示视频————帧缓冲显示视频