【问题标题】:Making video with overlay view in AVCaptureVideoPreviewLayer在 AVCaptureVideoPreviewLayer 中使用叠加视图制作视频
【发布时间】:2017-08-21 10:25:37
【问题描述】:

我正在尝试使用 AVCaptureVideoPreviewLayer 捕获带有覆盖视图的视频,但视频在没有覆盖视图的情况下保存。基本上我的应用程序正在设置一个对象而不是一个人脸,它工作得很好,我也可以拍摄图像(见下面的链接)。捕获视频是唯一的问题。

https://i.stack.imgur.com/J5T15.jpg

CMFormatDescriptionRef formatDescriptionRef = CMSampleBufferGetFormatDescription(sampleBuffer);

        const AudioStreamBasicDescription *audioStreamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(fdesc);

        _sampleRate = audioStreamBasicDescription -> mSampleRate;
        _audioChannel = audioStreamBasicDescription -> mChannelsPerFrame;

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"HH:mm:ss";
        NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]];
        NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
        NSString *videoName = [NSString stringWithFormat:@"video_%@.mp4", currentDateString];
        _videoPath = [self.cacheDirectoryPath stringByAppendingPathComponent:videoName];
        _resolutionWidth  = 360;
        _resolutionHeight = 640;
        _recordingWriter = [VideoRecordingWriter recordingWriterWithVideoPath:_videoPath
                                                              resolutionWidth:_resolutionWidth resolutionHeight:_resolutionHeight
                                                                 audioChannel:_audioChannel sampleRate:_sampleRate];

        CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
        if (_startRecordingCMTime.value == 0) {
            _startRecordingCMTime = presentationTimeStamp;
        }

        CMTime subtract = CMTimeSubtract(presentationTimeStamp, _startRecordingCMTime);
        _currentRecordingTime = CMTimeGetSeconds(subtract);
        if (_currentRecordingTime > _maxRecordingTime) {
            if (_currentRecordingTime - _maxRecordingTime >= 0.1) {
                return;
            }
        }
        [_recordingWriter writeWithSampleBuffer:sampleBuffer isVideo:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateRecordingProgress:_currentRecordingTime / _maxRecordingTime];
        });

【问题讨论】:

  • 欢迎来到 StackOverflow,请提供您尝试过的代码,这是任何人都可以帮助您的唯一方法。
  • 更新了我的视频保存代码

标签: ios objective-c avcapturesession google-vision


【解决方案1】:
func startCapturing(view: SKView, background: CALayer, done: @escaping((_:CALayer)->())) {
    let device = getCamera()!

    do {
        captureSession.beginConfiguration()

        // Set up INPUTS
        try captureSession.addInput(AVCaptureDeviceInput(device: device))

        captureSession.sessionPreset = AVCaptureSessionPresetHigh
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

        if let previewLayer = previewLayer {
            // add background
            previewLayer.addSublayer(background)

            // init preview
            previewLayer.name = "camera"
            previewLayer.frame.size = view.bounds.size
            if IS_DEBUG {
                print("VideoService: size of previewLayer")
                print(previewLayer.frame.size)
            }
            previewLayer.position = CGPoint(
                x: view.frame.width/2, y: view.frame.height/2)
            view.layer.addSublayer(previewLayer)
            done(previewLayer)
        }
        captureSession.commitConfiguration()
        captureSession.startRunning()

        print("\n\nVideoService: camera configured")
    } catch {
        print("Error: start capturing failed")
    }
}

func getCaptureSessionBack() -> AVCaptureDeviceDiscoverySession {
    session = session ?? AVCaptureDeviceDiscoverySession(
        deviceTypes: [deviceTypeBackCamera],
        mediaType: AVMediaTypeVideo,
        position: position)
    return session!
}

func getCamera() -> AVCaptureDevice? {
    device = device ?? getCaptureSessionBack().devices.first
    return device
}

【讨论】:

  • 我需要为此使用精神套件吗?对不起,我对此一无所知
  • 它就像一个 3D 对象,所以当用户四处走动时,它会像 snapchat 一样在视频中从各个角度看到。
猜你喜欢
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 2022-08-08
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2021-04-01
相关资源
最近更新 更多