我需要制作一个简单的录像机 当dataOutput时,我必须在上面添加水印 当我使用UIGraphicsBeginImageContextWithOptions从相机修改示例缓冲区时。 音频将丢失,无法录制到文件中。
如果我删除了UIGraphicsBeginImageContextWithOptions,只需转换为pixelBuffer,每件事都很精细,视频和音频都是同步的。
什么是UIGraphicsBeginImageContextWithOptions,我不应该在captureOutput中绘制图像?
open func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
if self.isRecording {
let timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
if !self.startTime.isValid {
assetWriter!.startWriting()
assetWriter!.startSession(atSourceTime: timestamp)
startTime = timestamp
}
duration = timestamp - startTime
let trueTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer) ;
if output is AVCaptureVideoDataOutput {
if self.videoInput != nil {
while self.videoInput!.isReadyForMoreMediaData == false {
}
if let pixelbuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
let ret = self.videoWriterInputPixelBufferAdaptor?.append(pixelbuffer, withPresentationTime: trueTime )
if ( ret == false ) {
print("@@@@ video queue write error", ret) ;
}
}
}
self.frameCount += 1 ;
}
else if output is AVCaptureAudioDataOutput {
if self.audioInput != nil && self.audioInput!.isReadyForMoreMediaData {
if ( self.audioInput!.append(sampleBuffer) == false ) {
}
}
}
}
}
func processSampleBuffer(_ buf:CMSampleBuffer) -> CVPixelBuffer? {
let imageBuffer = CMSampleBufferGetImageBuffer(buf)
if let pixelBuffer = imageBuffer {
if let img = UIImage(pixelBuffer: pixelBuffer) {
let rect:CGSize = img.size
UIGraphicsBeginImageContextWithOptions(rect, true, 1.0);
let context = UIGraphicsGetCurrentContext()!
img.draw(in: CGRect(x: 0, y: 0, width: rect.width , height: rect.height))
img.draw(in: CGRect(x: rect.width / 4, y: rect.height / 4, width: rect.width / 2 , height: rect.height / 2))
let capture = UIGraphicsGetImageFromCurrentImageContext() ;
UIGraphicsEndImageContext() ;
return capture?.pixelBuffer(width: width, height: height)
}
}
return nil;
}