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,

相关内容

热门资讯

辅助透视!hhpoker一直输... 辅助透视!hhpoker一直输有挂吗,微乐自建房辅助工具免费,讲义教程(有挂助手)-哔哩哔哩1、hh...
辅助透视!wepoker底牌透... 辅助透视!wepoker底牌透视脚本,微乐家乡小程序脚本,指南教程(有挂技巧)-哔哩哔哩1、在wep...
详情透视!wepoker好友房... 详情透视!wepoker好友房开挂,微乐透视辅助功能插件下载,方案教程(有挂实锤)-哔哩哔哩1、超多...
解密透视!wejoker辅助软... 解密透视!wejoker辅助软件价格,微乐小程序自建房辅助,积累教程(详细教程)-哔哩哔哩1)wej...
普及透视!wepoker插件辅... 普及透视!wepoker插件辅助,淘宝的微乐辅助是真的吗,机巧教程(有挂助手)-哔哩哔哩1、不需要A...
总结透视!德普之星透视,微乐家... 总结透视!德普之星透视,微乐家乡app下载,模板教程(详细教程)-哔哩哔哩1、德普之星透视有没有辅助...
总结透视!hhpoker有透视... 总结透视!hhpoker有透视功能吗,微乐小程序自建房辅助器下载,练习教程(发现有挂)-哔哩哔哩1....
揭露透视!hhpoker有后台... 揭露透视!hhpoker有后台操作吗,微乐内蒙古自建房脚本免费下载,绝活教程(竟然有挂)-哔哩哔哩该...
开挂透视!智星德州辅助译码插件... 开挂透视!智星德州辅助译码插件靠谱吗,微乐脚本,积累教程(有挂方法)-哔哩哔哩1、这是跨平台的智星德...
解密透视!we-poker辅助... 解密透视!we-poker辅助软件教程,微乐自建房辅助神器怎么安装,指南书教程(真是有挂)-哔哩哔哩...