使用AVAssetWriter录制视频

使用AVAssetWriter录制视频

本文介绍了使用AVAssetWriter录制视频:第一帧为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用AVAssetWriter录制视频(用户也只能切换到音频).我在启动应用程序时开始录制.但是第一帧是黑色(或非常暗).当我从音频切换到视频时,也会发生这种情况.感觉AVAssetWriter和/或AVAssetWriterInput尚未准备好记录.如何避免这种情况?

I am recording video (the user also can switch to audio only) with AVAssetWriter. I start the recording when the app is launched.But the first frames are black (or very dark). This also happens when I switch from audio to video.It feels like the AVAssetWriter and/or AVAssetWriterInput are not yet ready to record. How can I avoid this?

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

I don't know if this is a useful info but I also use a GLKView to display the video.

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")
        }
    }
}

推荐答案

好吧,愚蠢的错误...

Ok, stupid mistake...

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

When launching the app, I init my AVCaptureSession, add inputs, outputs, etc. And I was just calling start_new_record a bit too soon, just before commitConfiguration was called on my capture session.

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

At least my code might be useful to some people.

这篇关于使用AVAssetWriter录制视频:第一帧为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:57