【问题标题】:Get output from AVCaptureSession in Swift to send to server从 Swift 中的 AVCaptureSession 获取输出以发送到服务器
【发布时间】:2014-11-29 15:16:07
【问题描述】:

我设法编写了一些代码来打开相机并预览视频。我现在想从输出中捕获帧以发送到理想编码为 H.264 的服务器

这是我得到的:

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

    }

}

这成功打开了相机,我可以预览镜头了。

我发现这个 Objective C 代码看起来像是得到了输出,但我不知道如何将它转换为 swift。它使用 AVCaptureVideoDataOutput、AVAssetWriter、AVAssetWriterInput 和 AVAssetWriterInputPixelBufferAdaptor 将帧写入 H.264 编码的电影文件。

Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?

有人可以帮助转换它或给我指点如何从我当前的代码中获取框架吗?

【问题讨论】:

  • swift 缺少这类事情的文档。这很令人沮丧。
  • 您找到解决方案了吗?
  • 您是否正在尝试将代码从 Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time? 转换为 swift?
  • 也许您真正的问题是如何从您的 iPhone 流式传输视频,对吗?您尝试过 G-Streamer、FFmpeg 还是 live555?我很确定它会成功,只是要小心许可证......

标签: ios video swift stream avcapturesession


【解决方案1】:

Apple 在 ObjectiveC 中有一个示例项目 AVCam 可以处理这些东西。

Here's 另一个关于在 Swift 中使用 AVCamera 的问题。

我个人用过这个https://github.com/alex-chan/AVCamSwift,没问题。我只需要在 Xcode 中将其转换为最新的 Swift 语法,它就可以正常工作了。

另一个建议是使用您找到的 ObjectiveC 代码并通过桥接头将其导入到您的 Swift 代码中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多