如何在iOS中每隔10秒使用AVAssetWriter将CMSampleBufferRef(音频和视频)转换为视频文件(.mp4或.mov)?
我正在使用AVFoundation的如何在iOS中每隔10秒使用AVAssetWriter将CMSampleBufferRef(音频和视频)转换为视频文件(.mp4或.mov)?,ios,objective-c,iphone,avfoundation,avassetwriter,Ios,Objective C,Iphone,Avfoundation,Avassetwriter,我正在使用AVFoundation的AVCaptureSession捕获视频以进行直播(使用RTMP协议将视频发送到服务器) AVCaptureVideoDataOutputSampleBufferDelegate和AVCaptureAudioDataOutputSampleBufferDelegate的回调委托方法使用以下方法返回CMSampleBufferRef: - (void)captureOutput:(AVCaptureOutput *)captureOutputdidOutputS
AVCaptureSession
捕获视频以进行直播(使用RTMP协议将视频发送到服务器)
AVCaptureVideoDataOutputSampleBufferDelegate
和AVCaptureAudioDataOutputSampleBufferDelegate
的回调委托方法使用以下方法返回CMSampleBufferRef
:
- (void)captureOutput:(AVCaptureOutput *)captureOutputdidOutputSampleBuffer:(CMSampleBufferRef)sampleBufferfromConnection:(AVCaptureConnection *)connection
根据此链接的内容,我可以使用将此缓冲区内容转换为文件
像
但是我没有得到这个文件转换所涉及的确切逻辑。didOutputSampleBuffer
是一个逐帧捕获(未压缩)的原始数据,我想以一定的间隔将这些帧转换为单个文件。比如说每10秒左右。我想将此文件发送到服务器
有人能简单介绍一下AVAssetWriter的工作原理吗?如果我做错了什么,请纠正我。你找到了解决方案吗?没有。我做不到。
NSURL *url = …;
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];
AVAssetWriterInput *videoInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:nil];
videoInput.expectsMediaDataInRealTime = YES;
AVAssetWriterInput *audioInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:nil];
audioInput.expectsMediaDataInRealTime = YES;
if ([assetWriter canAddInput:videoInput]) {
[assetWriter addInput:videoInput];
}
if ([assetWriter canAddInput:audioInput]) {
[assetWriter addInput:audioInput];
}