AVAssetWriter获取原始字节会导致设备上的视频文件损坏(在模拟器上可以正常工作)
创始人
2024-11-12 20:30:17
0

当使用AVAssetWriter获取原始字节时,需要确保数据的正确性和完整性。这个问题可能是由于在写入时丢失了某些数据或指针错误引起的。因此,您可以在开始写入前打印一些调试信息以查找错误。

以下是示例代码,用于演示如何使用AVAssetWriter以安全的方式获取原始字节数据:

// 初始化写入器
AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:fileURL fileType:AVFileTypeMPEG4 error:&error];

// 设置输出格式
NSDictionary *outputSettings = @{AVVideoCodecKey: AVVideoCodecTypeH264,
                                 AVVideoWidthKey: @(1920),
                                 AVVideoHeightKey: @(1080)};
AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
[writer addInput:writerInput];

// 初始化编码器
NSDictionary *sourcePixelBufferAttributesDictionary = @{(NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)};
AVAssetWriterInputPixelBufferAdaptor *adaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];

// 开始写入
[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];

// 获取原始字节
UInt32 dataLength = 0;
void *outputBytes = nil;
CGImageRef image = [UIImage imageNamed:@"test.png"].CGImage;
CGFloat width = CGImageGetWidth(image);
CGFloat height = CGImageGetHeight(image);
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image));
UInt8* pixelBytes = (UInt8 *)CFDataGetBytePtr(pixelData);

// 将数据写入文件
CMSampleBufferRef sampleBuffer = NULL;
CVPixelBufferRef buffer = NULL;
CMFormatDescriptionRef format = NULL;

CMVideoFormatDescriptionCreateForImageBuffer(NULL, buffer, &format);
CMSampleTimingInfo timing = {kCMTimeInvalid, kCMTimeZero, kCMTimeInvalid};

CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
                                   pixelBytes, dataLength,
                                   kCFAllocatorNull,
                                   NULL, 0, dataLength,
                                   kCMBlockBufferAlwaysCopyDataFlag,
                                   &buffer);
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault,
                                    buffer,
                                    true,
                                    NULL,
                                    NULL,
                                    format,

相关内容

热门资讯

4分钟插件!打哈儿辅助,八闽掌... 4分钟插件!打哈儿辅助,八闽掌上十八卦脚本(其实是真的辅助app)-哔哩哔哩1、玩家可以在八闽掌上十...
第6分钟讲义!aa poker... 第6分钟讲义!aa poker透视软件(透视)竟然存在有辅助脚本(哔哩哔哩)1)aa poker透视...
第8分钟安装!熊猫辅助技术导航... 第8分钟安装!熊猫辅助技术导航,欢聚水鱼智能辅助教程(其实是真的辅助平台)-哔哩哔哩1、欢聚水鱼智能...
第六分钟手段!werplan有... 第六分钟手段!werplan有挂吗(透视)总是真的有辅助下载(哔哩哔哩)1、werplan有挂吗辅助...
9分钟插件!闲逸辅助脚本,禅游... 9分钟插件!闲逸辅助脚本,禅游指尖四川修改器(总是是有辅助平台)-哔哩哔哩1、游戏颠覆性的策略玩法,...
第3分钟模块!wepoker代... 第3分钟模块!wepoker代打辅助(透视)切实真的有辅助透视(哔哩哔哩)1、不需要AI权限,帮助你...
第七分钟辅助!微乐手游脚本辅助... 第七分钟辅助!微乐手游脚本辅助,黑科技辅助软件免费(一直有挂辅助工具)-哔哩哔哩一、黑科技辅助软件免...
九分钟模板!sohoo pok... 九分钟模板!sohoo poker辅助器(透视)其实真的是有辅助开挂(哔哩哔哩)小薇(辅助器软件下载...
4分钟安装!广西友乐软件辅助,... 4分钟安装!广西友乐软件辅助,陕麻圈延安辅助(竟然真的是有辅助工具)-哔哩哔哩1、完成广西友乐软件辅...
第四分钟策略!hhpoker为... 第四分钟策略!hhpoker为什么一直输(透视)总是真的是有辅助辅助(哔哩哔哩);1、首先打开hhp...