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,

相关内容

热门资讯

从来有辅助"龙江博乐... 从来有辅助"龙江博乐填大坑辅助"详细开挂辅助平台有挂透视(力荐教程)【无需打开直接搜索加薇13670...
最初有开挂"陕麻圈智... 最初有开挂"陕麻圈智能辅助软件"开挂(透视)辅助工具(黑科技教程有挂方法)您好:陕麻圈智能辅助软件这...
手筋开挂"钱塘十水三... 钱塘十水三辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,钱塘十水三辅助作为一种经典的娱乐方式,...
透视了解!麻友圈安庆版插件,皇... 透视了解!麻友圈安庆版插件,皇豪互娱控制系统(透视)开挂辅助安装教你教程 >>您好:软件加薇1367...
原先有开挂"衢州都莱... 原先有开挂"衢州都莱辅助器是真是假"详细开挂辅助插件了解有挂(透明挂教程!)>>您好:软件加1367...
手册辅助"皮皮游戏挂... >>您好:皮皮游戏挂机辅助确实是有挂的,很多玩家在这款皮皮游戏挂机辅助游戏中打牌都会发现很多用户的牌...
原先有辅助"aapo... 【亲,aapoker透视脚本 这款游戏可以开挂的,确实是有挂的,很多玩家在这款aapoker透视脚本...
透视规律!八闽掌上十八卦脚本,... 透视规律!八闽掌上十八卦脚本,werplan透视挂(透视)开挂辅助挂线上教程;亲,八闽掌上十八卦脚本...
本然有辅助"九江讨赏... 九江讨赏插件 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、软...
秘籍开挂"皮皮游戏挂... 秘籍开挂"皮皮游戏挂机辅助"开挂(脚本)辅助脚本(有挂总结);无需打开直接搜索薇:136704302...