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,

相关内容

热门资讯

指导大家!扣点点脚本辅助器,乐... 指导大家!扣点点脚本辅助器,乐乐围棋入门插件,详细工具(确实有挂)1、完成乐乐围棋入门插件有辅助插件...
实测交流!小程序牵手跑得辅助,... 实测交流!小程序牵手跑得辅助,创思维激k辅助控制器,详细软件(有挂详细)1、上手简单,内置详细流程视...
热点讨论!新八戒辅助,广东老友... 热点讨论!新八戒辅助,广东老友麻将有挂吗,详细app(确实有挂)1)广东老友麻将有挂吗免费钻石:进一...
分享一款!手游辅助平台,同乡游... 分享一款!手游辅助平台,同乡游辅助工具制作,详细方法(确实有挂)1、上手简单,内置详细流程视频教学,...
玩家必看科普!皮皮跑子辅助,欢... 玩家必看科普!皮皮跑子辅助,欢乐达人破解器,详细器(有挂猫腻)1、欢乐达人破解器辅助软件下载优化,欢...
重大通报!决战十水三辅助,新道... 重大通报!决战十水三辅助,新道游开挂视频,详细方法(证实有挂)1、这是跨平台的新道游开挂视频轻量版有...
必备辅助推荐!腾微填大坑辅助,... 必备辅助推荐!腾微填大坑辅助,天天贵阳游戏辅助,详细方法(今日头条)1、上手简单,内置详细流程视频教...
分享一款!福建天庭啊开心辅助软... 分享一款!福建天庭啊开心辅助软件,开心门一番有挂吗,详细脚本(揭秘有挂)福建天庭啊开心辅助软件是不是...
必备辅助推荐!wepoker智... 必备辅助推荐!wepoker智能辅助插件,长城互娱辅助,详细脚本(有挂教程)1、打开软件启动之后找到...
让我来分享经验!潮友会内部脚本... 让我来分享经验!潮友会内部脚本辅助软件,微乐河南家乡麻将辅助脚本,详细app(有挂细节)1)微乐河南...