2014-10-04 15 views
15

Tôi đã quản lý để viết một số mã mở máy ảnh và xem trước video. bây giờ tôi muốn nắm bắt được khung hình từ đầu ra để gửi đến một máy chủ lý tưởng mã hóa như H.264Nhận đầu ra từ AVCaptureSession trong Swift để gửi đến máy chủ

Đây là những gì tôi đã có:

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    let captureSession = AVCaptureSession() 
    var previewLayer : AVCaptureVideoPreviewLayer? 

    // If we find a device we'll store it here for later use 
    var captureDevice : AVCaptureDevice? 

    override func viewDidLoad() { 
     super.viewDidLoad()    
     // Do any additional setup after loading the view, typically from a nib. 

     captureSession.sessionPreset = AVCaptureSessionPresetHigh 

     let devices = AVCaptureDevice.devices() 

     // Loop through all the capture devices on this phone 
     for device in devices { 
      // Make sure this particular device supports video 
      if (device.hasMediaType(AVMediaTypeVideo)) { 
       // Finally check the position and confirm we've got the back camera 
       if(device.position == AVCaptureDevicePosition.Back) { 
        captureDevice = device as? AVCaptureDevice 
        if captureDevice != nil { 
         println("Capture device found") 
         beginSession() 
        } 
       } 
      } 
     } 

    } 

    func beginSession() { 

     var err : NSError? = nil 
     captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err)) 

     if err != nil { 
      println("error: \(err?.localizedDescription)") 
     } 

     previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
     self.view.layer.addSublayer(previewLayer) 
     previewLayer?.frame = self.view.layer.frame 

     captureSession.startRunning() 

    } 

} 

mở này camera thành công và tôi có thể xem trước các cảnh quay .

Tôi đã tìm thấy mã C Mục tiêu này có vẻ như nó nhận được kết quả đầu ra nhưng tôi không biết cách chuyển đổi nó thành nhanh. Nó đang sử dụng AVCaptureVideoDataOutput, AVAssetWriter, AVAssetWriterInput và AVAssetWriterInputPixelBufferAdaptor để ghi khung hình ra tệp phim được mã hóa H.264.

Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?

Ai đó có thể giúp chuyển đổi nó hoặc cho tôi gợi ý như thế nào để có được những khung hình ra khỏi mã hiện tại của tôi?

+0

Tài liệu về những thứ này thiếu nhanh chóng. Nó khá bực bội. –

+0

Bạn đã tìm thấy giải pháp chưa? –

+0

là bạn đang cố chuyển đổi mã từ [Có thể sử dụng AVCaptureVideoDataOutput và AVCaptureMovieFileOutput cùng một lúc không?] (Http://stackoverflow.com/questions/4944083/can-use-avcapturevideodataoutput-and-avcapturemoviefileoutput-at-the-same-time) để nhanh chóng? –

Trả lời

3

Apple có dự án mẫu AVCam trong ObjectiveC hoạt động với những thứ này.

Here's một câu hỏi khác về SO về việc sử dụng AVCamera trong Swift.

Cá nhân tôi đã sử dụng số này https://github.com/alex-chan/AVCamSwift và điều đó là tốt. Tôi chỉ phải chuyển đổi nó sang cú pháp Swift mới nhất trong Xcode và nó hoạt động tốt.

Một đề xuất khác là sử dụng mã ObjectiveC mà bạn đã tìm và nhập mã trong mã Swift của bạn thông qua tiêu đề bắc cầu.

Các vấn đề liên quan