iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用AVAssetWriter录制视频:第一帧是黑色的大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在用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")
        }
    }
}

解决方法

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)

大佬总结

以上是大佬教程为你收集整理的ios – 使用AVAssetWriter录制视频:第一帧是黑色的全部内容,希望文章能够帮你解决ios – 使用AVAssetWriter录制视频:第一帧是黑色的所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。