我在 iOS 文档中注意到AVAssetWriterInput
你可以通过nil
为了outputSettings
字典来指定输入数据不应重新编码。
用于对附加到输出的媒体进行编码的设置。传递 nil 来指定不应重新编码附加的样本。
我想利用此功能来传递原始 H.264 NAL 流,但我在将原始字节流调整为CMSampleBuffer
我可以传递给 AVAssetWriterInputappendSampleBuffer
方法。我的 NAL 流仅包含 SPS/PPS/IDR/P NAL (1, 5, 7, 8)。我尚未找到有关如何将预编码的 H264 数据与 AVAssetWriter 一起使用的文档或结论性答案。生成的视频文件无法播放。
我怎样才能正确地将NAL单元打包成CMSampleBuffers
?我需要使用起始代码前缀吗?长度前缀?我是否需要确保每次只放置一个 NALCMSampleBuffer
?我的最终目标是使用 H264/AAC 创建 MP4 或 MOV 容器。
这是我一直在玩的代码:
-(void)addH264NAL:(NSData *)nal
{
dispatch_async(recordingQueue, ^{
//Adapting the raw NAL into a CMSampleBuffer
CMSampleBufferRef sampleBuffer = NULL;
CMBlockBufferRef blockBuffer = NULL;
CMFormatDescriptionRef formatDescription = NULL;
CMItemCount numberOfSampleTimeEntries = 1;
CMItemCount numberOfSamples = 1;
CMVideoFormatDescriptionCreate(kCFAllocatorDefault, kCMVideoCodecType_H264, 480, 360, nil, &formatDescription);
OSStatus result = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, NULL, [nal length], kCFAllocatorDefault, NULL, 0, [nal length], kCMBlockBufferAssureMemoryNowFlag, &blockBuffer);
if(result != noErr)
{
NSLog(@"Error creating CMBlockBuffer");
return;
}
result = CMBlockBufferReplaceDataBytes([nal bytes], blockBuffer, 0, [nal length]);
if(result != noErr)
{
NSLog(@"Error filling CMBlockBuffer");
return;
}
const size_t sampleSizes = [nal length];
CMSampleTimingInfo timing = { 0 };
result = CMSampleBufferCreate(kCFAllocatorDefault, blockBuffer, YES, NULL, NULL, formatDescription, numberOfSamples, numberOfSampleTimeEntries, &timing, 1, &sampleSizes, &sampleBuffer);
if(result != noErr)
{
NSLog(@"Error creating CMSampleBuffer");
}
[self writeSampleBuffer:sampleBuffer ofType:AVMediaTypeVideo];
});
}
请注意,我正在打电话CMSampleBufferSetOutputPresentationTimeStamp
在样本缓冲区内writeSampleBuffer
方法与我认为在我实际尝试附加它之前的有效时间。
任何帮助表示赞赏。