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,

相关内容

热门资讯

关于透视"wepok... 关于透视"wepoker真能买到挂吗"开挂(透视)辅助神器(记者揭秘透明挂教程)关于透视"wepok...
推荐辅助!广东雀神祈福有用吗,... 推荐辅助!广东雀神祈福有用吗,奇迹陕西挂(分享给玩家开挂辅助下载) >>您好:软件加薇1367043...
盘点透视"wepok... 盘点透视"wepoker能不能透视辅助"开挂(透视)辅助神器(详细说明wpk教程)盘点透视"wepo...
推荐辅助!三哥玩app辅助,来... 推荐辅助!三哥玩app辅助,来来拼十免费辅助(推荐一款开挂辅助安装)1、下载安装好来来拼十免费辅助,...
盘点透视"wepok... 盘点透视"wepoker到底能不能开挂"开挂(透视)辅助工具(技术分享实用技巧)盘点透视"wepok...
必看开挂!新二号辅助下载,潮友... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
科技透视"wepok... 科技透视"wepokerplus挂先试用后付款"开挂(透视)辅助脚本(终于清楚安装教程)科技透视"w...
通报辅助!卡农大厅辅助,智星德... 通报辅助!卡农大厅辅助,智星德州插件最新版本更新内容详解(每日必备开挂辅助脚本) 【无需打开直接搜索...
实测透视"Wepok... 实测透视"Wepoker最厉害三个组合"开挂(透视)辅助插件(一分钟了解必赢教程)实测透视"Wepo...
详细开挂!新道游怎么开挂,创思... 详细开挂!新道游怎么开挂,创思维激k辅助控制器(一分钟带你了解开挂辅助下载);无需打开直接搜索打开薇...