ios - Quickblox - 如何将 QBRTCCameraCapture 保存到文件

标签 ios swift webrtc quickblox

我有一个使用最新 Quickblox 版本(Quickblox 2.5、Quickblox-WebRTC 2.0)的视频通话应用程序,我需要将通话中流式传输的视频保存为一个文件。有一个使用旧版本 SDK 的旧示例,它看起来与当前版本完全不同。

当前文档中没有关于此的内容,我无法启动 AVCaptureMovieFileOutout,因为 Quickblox 已经在使用 AVCaptureVideoDataOutput。有没有办法将流保存到文件?

更新:

我设法将视频写入文件。所缺少的只是音轨。

import Foundation

class VideoManager : NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {

    static let sharedInstance = VideoManager()

    var pixelBufferAdaptor: AVAssetWriterInputPixelBufferAdaptor?
    var assetWriterInput: AVAssetWriterInput?
    var assetWriter: AVAssetWriter?
    var frameNumber: Int64 = 0
    var qbDelegate: AVCaptureVideoDataOutputSampleBufferDelegate?

    func startSavingCaptureToFileWithURL(url: NSURL, capture: QBRTCCameraCapture) {
        print("[VideoManager]: startSavingCaptureToFileWithURL")
        guard let dataOutput = getVideoCaptureDataOutput(capture) else { return }

        frameNumber = 0

        qbDelegate = dataOutput.sampleBufferDelegate
        dataOutput.setSampleBufferDelegate(self, queue: dataOutput.sampleBufferCallbackQueue)

        let outputSettings: [String : AnyObject] = [
            AVVideoWidthKey : 720,
            AVVideoHeightKey: 1280,
            AVVideoCodecKey : AVVideoCodecH264
        ]

        assetWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: outputSettings)
        pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: assetWriterInput!, sourcePixelBufferAttributes: [kCVPixelBufferPixelFormatTypeKey as String : NSNumber(unsignedInt: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)])

        do {
            assetWriter = try AVAssetWriter(URL: url, fileType: AVFileTypeMPEG4)
            assetWriter!.addInput(assetWriterInput!)
            assetWriterInput!.expectsMediaDataInRealTime = true

            assetWriter!.startWriting()
            assetWriter!.startSessionAtSourceTime(kCMTimeZero)
        }
        catch {
            print("[VideoManager]: Error persisting stream!")
        }

    }

    func stopSavingVideo() {
        assetWriter?.finishWritingWithCompletionHandler { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.frameNumber = 0
        }
    }

    private func getVideoCaptureDataOutput(videoCapture: QBRTCCameraCapture) -> AVCaptureVideoDataOutput? {
        var output: AVCaptureVideoDataOutput?
        videoCapture.captureSession.outputs.forEach{ captureOutput in
            if captureOutput is AVCaptureVideoDataOutput {
                output = captureOutput as? AVCaptureVideoDataOutput
            }
        }
        return output
    }

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        qbDelegate?.captureOutput?(captureOutput, didOutputSampleBuffer: sampleBuffer, fromConnection: connection)

        guard let assetWriterInput = assetWriterInput else { return }
        guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }

        if assetWriterInput.readyForMoreMediaData {
            pixelBufferAdaptor?.appendPixelBuffer(imageBuffer, withPresentationTime: CMTimeMake(frameNumber, 25))
        }

        frameNumber++
    }

    func getUniqueFileURL() -> NSURL {
        let guid = NSProcessInfo.processInfo().globallyUniqueString
        let fileName = "\(guid).mp4"
        return NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(fileName)
    }

}

关于如何获取 QBRTCLocalAudioTrack 的底层 AVCaptureAudioDataOutput 有什么想法吗?

最佳答案

我来自开发 QuicbkloxWebRTC 团队。该功能计划用于下一个大版本。

关于ios - Quickblox - 如何将 QBRTCCameraCapture 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857572/

相关文章:

ios - UICollectionViewCell 中的 UIScrollView 不工作

带有披露指示器的 iOS 居中 UITableViewCell 标签

ios - 如何在 ios swift 中正确实现后台获取

javascript - RTC 对等连接 - 接收流两次

ios - 地幔自动将 0 和 1 转换为 BOOL?

ios - 自定义表格 View 单元格中的 UITextField。点击单元格/文本字段时显示选取器 View

ios - 如何在 iOS 应用程序中强制访问 'always' 位置

node.js - 使用 WebRTC 创建远程桌面

ubuntu - 编译 WebRTC 失败

ios - 如何使用 swift 从解析类中查询/检索随机对象?