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,

相关内容

热门资讯

信息共享“德州局wepoker... 德州局wepoker辅助挂安全吗是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要...
终于懂了“微扑克封号如何避免”... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
重大通报“wpk正版透视”开挂... 无需打开直接搜索加微信客服(136704302)咨询了解wpk正版透视辅助器目前(微信:136704...
科技新动态“wpk账号胜利太低... 科技新动态“wpk账号胜利太低”开挂辅助工具(透视)科技教程是一款可以让一直输的玩家,快速成为一个“...
2分钟细说“wepoker开挂... >>您好:wepoker开挂辅助方法确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好...
盘点几款“wpk辅助挂”开挂辅... 盘点几款“wpk辅助挂”开挂辅助器(透视)力荐教程是一款可以让一直输的玩家,快速成为一个“必胜”的a...
热点推荐“wepoker能不能... wepoker能不能开挂是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可...
最新技巧“wepokerpus... 无需打开直接搜索;操作使用教程:最新技巧“wepokerpus透视挂是真的假的”开挂辅助软件(透视)...
科技介绍“wepoker透视软... wepoker透视软件挂教程是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
终于清楚“wePoKer有透视... >>您好:wePoKer有透视软件吗软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...