我可以同时录制屏幕和摄像机,并使用AVCaptureSession和avassetwriter将其保存在单个文件中吗?

时间:2019-01-31 12:42:02

标签: avcapturesession avassetwriter screen-recording avcaptureoutput

我正在使用两个AVCaptureInput(屏幕和摄像机),并将它们添加到两个AVCaptureVideoDataOutput。然后,我使用“ addOutputWithNoConnections”将这些输出添加到会话中,以便为屏幕和摄像机获得单独的帧。当我使用单个AVAssetWriter编写两个示例缓冲区时,它给我带来了未知的错误。

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
            if(captureOutput == screenOutput) {
                [self appendBufferToWriter:sampleBuffer];
            }
            else if(captureOutput == cameraOutput) {
                [self appendBufferToWriter:sampleBuffer];
            }
}

- (void)appendBufferToWriter:(CMSampleBufferRef)sampleBuffer {
    presentationTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
    if(firstFrame){
        [videoWriter startSessionAtSourceTime:presentationTime];
        firstFrame = false;
    }
    [self addBufferToWriter:sampleBuffer];
}
- (void)addBufferToWriter:(CMSampleBufferRef)inSample {
   if(assetWriterVideoInput.readyForMoreMediaData == YES){
        [assetWriterVideoInput appendSampleBuffer:inSample];
}
 
- (void)stopRecording:(id)sender {
    [assetWriterVideoInput markAsFinished];
    [videoWriter endSessionAtSourceTime: presentationTime];
    [videoWriter finishWritingWithCompletionHandler:^{
        [self->mSession stopRunning];
        self->mSession = nil;
    }];
}
    

0 个答案:

没有答案