GPUImageMovieWriter - 由 audioEncodingTarget 引起的黑帧

GPUImageMovieWriter - black frame caused by audioEncodingTarget

我正在尝试使用 GPUImage 库录制视频。录制的剪辑以黑框结尾。我知道这是由 audioEncodingTarget 引起的,这是一个非常昂贵的操作。这已经被讨论了很多,但我仍然没有找到解决方案。 这是我的代码:

GPUCameraRecorder class 初始化

    videoCamera = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPresetiFrame960x540, cameraPosition: .Back)
    videoCamera.outputImageOrientation = .LandscapeRight;
    videoCamera.horizontallyMirrorFrontFacingCamera = true

    filter = GPUImageFilter()
    videoCamera.addTarget(filter)

    view = GPUImageView(frame: frame)
    view.fillMode = kGPUImageFillModePreserveAspectRatioAndFill

    movieWriter = GPUImageMovieWriter(movieURL: output, size: view.frame.size)
    movieWriter?.encodingLiveVideo = true

    filter?.addTarget(movieWriter!)
    filter?.addTarget(view as GPUImageView)

    videoCamera.audioEncodingTarget = self.movieWriter!
    videoCamera.startCameraCapture()

开启录音功能

func startRecording(){
    println("Start recording.")
    recording = true

    dispatch_async(dispatch_get_main_queue()) {
        var res = self.videoCamera.addAudioInputsAndOutputs()
        println(res)

        self.movieWriter!.startRecording()            
    }
}

结束录音功能

func stopRecording(){
    println("Recording end.")

    dispatch_async(dispatch_get_main_queue()) {            
        self.filter?.removeTarget(self.movieWriter!)
        self.videoCamera.audioEncodingTarget = nil
        self.movieWriter!.finishRecording()

        self.putTorchOff()
        self.delegate?.recordingEnd()

        println("completed")
    }
 }

有什么建议吗?有人找到没有黑框的方法吗?

你试过吗?

里面GPUImageMovieWriter.m 添加代码:

static BOOL allowWriteAudio = NO;

- (void)startRecording;
{
  ...
  allowWriteAudio = NO;
}

- (void)processAudioBuffer:(CMSampleBufferRef)audioBuffer;
{
  if (!allowWriteAudio) {
    return;
  }
  ...
}

- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
{
  ...
  if (![assetWriterPixelBufferInput appendPixelBuffer:pixel_buffer withPresentationTime:frameTime])
    NSLog(@"Problem appending pixel buffer at time: %@", CFBridgingRelease(CMTimeCopyDescription(kCFAllocatorDefault, frameTime)));

  allowWriteAudio = YES;   //< add this
  ...
}

https://github.com/BradLarson/GPUImage/issues/1255