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,

相关内容

热门资讯

受玩家影响!aapoker公共... 受玩家影响!aapoker公共底牌,德扑之星私人局辅助器,本来真的是有挂(今日头条)1、德扑之星私人...
不少玩家反映!wepoker私... 不少玩家反映!wepoker私人局透视方法,德普之星透视,本来是有挂(有挂透明挂)1、上手简单,内置...
有了最新消息!wepoker有... 有了最新消息!wepoker有辅助功能吗,智星菠萝有挂吗,本来真的是有挂(有挂助手)1、智星菠萝有挂...
受玩家影响!wepoker辅助... 受玩家影响!wepoker辅助分析器,德普之星透视辅助软件是真的吗,其实确实有挂(了解有挂)1、每一...
有了最新消息!wepokerp... 有了最新消息!wepokerplus透视脚本免费,德普之星透视软件免费入口官网,确实确实有挂(证实有...
黑科技技巧!wepoker脚本... 黑科技技巧!wepoker脚本下载,德扑之星透视辅助插件,果然真的是有挂(有挂详细)1、完成德扑之星...
总结辅助挂!wepoker透视... 总结辅助挂!wepoker透视苹果系统,红龙poker辅助,一贯存在有挂(有挂教程)1.红龙poke...
近年来!智星菠萝有挂吗,如何下... 近年来!智星菠萝有挂吗,如何下载德普之星辅助软件,本来存在有挂(有挂助手)1、如何下载德普之星辅助软...
今日!wepoker透视版下载... 今日!wepoker透视版下载,德扑之星怎么开辅助,切实真的是有挂(有挂存在)1、进入到德扑之星怎么...
据统计!wepoker透视最简... 据统计!wepoker透视最简单三个步骤,德普之星透视,真是真的有挂(有挂教学)1、金币登录送、破产...