【问题标题】:Record video with AVAssetWriter: first frames are black使用 AVAssetWriter 录制视频:第一帧是黑色的
【发布时间】:2017-05-23 12:44:16
【问题描述】:

我正在使用AVAssetWriter 录制视频(用户也可以只切换到音频)。我在启动应用程序时开始录制。 但第一帧是黑色的(或非常暗)。当我从音频切换到视频时也会发生这种情况。 感觉AVAssetWriter 和/或AVAssetWriterInput 还没有准备好录制。我怎样才能避免这种情况?

我不知道这是否有用,但我也使用GLKView 来显示视频。

func start_new_record(){
    do{
        try self.file_writer=AVAssetWriter(url: self.file_url!, fileType: AVFileTypeMPEG4)
        if video_on{
            if file_writer.canAdd(video_writer){
                file_writer.add(video_writer)
            }
        }
        if file_writer.canAdd(audio_writer){
            file_writer.add(audio_writer)
        }
    }catch let e as NSError{
        print(e)
    }
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!){
    guard is_recording else{
        return
    }

    guard CMSampleBufferDataIsReady(sampleBuffer) else{
        print("data not ready")
        return
    }

    guard let w=file_writer else{
        print("video writer nil")
        return
    }

    if w.status == .unknown && start_recording_time==nil{
        if (video_on && captureOutput==video_output) || (!video_on && captureOutput==audio_output){
            print("START RECORDING")
            file_writer?.startWriting()
            start_recording_time=CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
            file_writer?.startSession(atSourceTime: start_recording_time!)
        }else{
            return
        }
    }

    if w.status == .failed{
        print("failed /", w.error ?? "")
        return
    }

    if captureOutput==audio_output{
        if audio_writer.isReadyForMoreMediaData{
            if !video_on || (video_on && video_written){
                audio_writer.append(sampleBuffer)
                //print("write audio")
            }
        }else{
            print("audio writer not ready")
        }
    }else if video_output != nil && captureOutput==video_output{
        if video_writer.isReadyForMoreMediaData{
            video_writer.append(sampleBuffer)
            if !video_written{
                print("added 1st video frame")
                video_written=true
            }
        }else{
            print("video writer not ready")
        }
    }
}

【问题讨论】:

  • 对我来说,解决方案是调用file_writer?.startWriting(),然后在下一个运行循环调用file_writer?.startSession(atSourceTime: start_recording_time!)。从那以后再也没有问题。

标签: ios swift avassetwriter avassetwriterinput


【解决方案1】:

SWIFT 4

解决方案 #1:

我通过在启动应用程序时尽快调用 file_writer?.startWriting() 解决了这个问题。然后,当您想开始录制时,请执行 file_writer?.startSession(atSourceTime:...)

当您完成录制并调用 finishRecording 后,当您收到表示已完成的回调时,再次设置新的写入会话。

解决方案 #2:

我通过在调用 AVAssetWriter.startSession 时将开始时间增加半秒来解决此问题,如下所示:

start_recording_time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
let startingTimeDelay = CMTimeMakeWithSeconds(0.5, 1000000000)
let startTimeToUse = CMTimeAdd(start_recording_time!, startingTimeDelay)

file_writer?.startSession(atSourceTime: startTimeToUse)

解决方案 #3:

这里更好的解决方案是记录您收到并决定写入的第一帧的时间戳,然后以此开始您的会话。那么你不需要任何延迟:

//Initialization, elsewhere:

var is_session_started = false
var videoStartingTimestamp = CMTime.invalid

// In code where you receive frames that you plan to write:

if (!is_session_started) {
    // Start writing at the timestamp of our earliest sample
    videoStartingTimestamp = currentTimestamp
    print ("First video sample received: Starting avAssetWriter Session: \(videoStartingTimestamp)")
    avAssetWriter?.startSession(atSourceTime: videoStartingTimestamp)

    is_session_started = true
}

// add the current frame
pixelBufferAdapter?.append(myPixelBuffer, withPresentationTime: currentTimestamp)

【讨论】:

  • 我使用了以下延迟 1/10 秒。让startingTimeDelay = CMTimeMakeWithSeconds(1, 10)
  • 我结合了第一种和第三种解决方案,它仍然给我一个延迟,并且在开始时出现黑框。
  • 我尝试了所有解决方案,但开始时仍然出现黑框。有人找到解决方案了吗?
【解决方案2】:

好吧,愚蠢的错误......

在启动应用程序时,我初始化了我的AVCaptureSession,添加了输入、输出等。我只是调用start_new_record 有点太早了,就在我的捕获会话调用commitConfiguration 之前。

至少我的代码可能对某些人有用。

【讨论】:

  • 我试过这个,但我仍然得到黑框。还有什么可以尝试的?
  • @VaddadiKartick 对不起,我不知道。我会说你应该像我一样检查你的配置方式。您是否在同一个队列中初始化所有内容?您是否像我一样尝试在应用启动时不开始录制,而是手动启动?
【解决方案3】:

这是为将来的用户准备的……

以上方法都不适合我,然后我尝试将相机预设更改为中等效果,效果很好

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多