这是汤米答案的快速版本。
// Set up the Capture Session
// Add the Inputs
// Add the Outputs
var outputSettings = [
AVVideoWidthKey : Int(640),
AVVideoHeightKey : Int(480),
AVVideoCodecKey : .h264
]
var assetWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo,outputSettings: outputSettings)
var pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput, sourcePixelBufferAttributes:
[ kCVPixelBufferPixelFormatTypeKey : Int(kCVPixelFormatType_32BGRA)])
var assetWriter = AVAssetWriter(url: URLFromSomwhere, fileType: AVFileTypeMPEG4 , error : Error )
assetWriter.addInput(assetWriterInput)
assetWriterInput.expectsMediaDataInRealTime = true
assetWriter.startWriting()
assetWriter.startSession(atSourceTime: kCMTimeZero)
captureSession.startRunning()
func captureOutput(_ captureOutput: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
var imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
// a very dense way to keep track of the time at which this frame
// occurs relative to the output stream, but it's just an example!
var frameNumber: Int64 = 0
if assetWriterInput.readyForMoreMediaData {
pixelBufferAdaptor.appendPixelBuffer(imageBuffer, withPresentationTime: CMTimeMake(frameNumber, 25))
}
frameNumber += 1 }
captureSession.stopRunning()
assetWriter.finishWriting()
我不保证100%的准确性,因为我是新手。
0
我想用我的代码同时录制视频并抓取帧。
我将
AVCaptureVideoDataOutput
用于抓帧,将AVCaptureMovieFileOutput
用于视频录制。但是不能同时工作,但会同时出现错误代码-12780。我搜索了这个问题,但没有得到答案。有没有人有相同的经历或解释?一段时间我真的很困扰。
谢谢。