但是第一帧是黑色(或非常暗).当我从音频切换到视频时也会发生这种情况.
感觉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 = falsevar 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 framepixelBufferAdapter?.append(myPixelBuffer,withPresentationTime: currentTimestamp)总结
以上是内存溢出为你收集整理的ios – 使用AVAssetWriter录制视频:第一帧是黑色的全部内容,希望文章能够帮你解决ios – 使用AVAssetWriter录制视频:第一帧是黑色的所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)