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、金币登录送、破...
透视讲解!we poker辅助... 透视讲解!we poker辅助器,微乐麻将脚本掌上程序,普及脚本(真的有挂)-哔哩哔哩进入游戏-大厅...
透视详情!aapoker脚本,... 透视详情!aapoker脚本,aapoker辅助怎么用(透视)切实真的有插件(哔哩哔哩)1、操作简单...
透视神器!wepoker有辅助... 透视神器!wepoker有辅助器吗,微乐小程序辅助开发,解密脚本(有挂头条)-哔哩哔哩在进入wepo...
日前!微扑克微乐辅助,微乐自建... 日前!微扑克微乐辅助,微乐自建房脚本免费入口,解密透视(发现有挂)-哔哩哔哩微扑克微乐辅助能透视中分...
透视推荐!wpk透视辅助靠谱吗... 透视推荐!wpk透视辅助靠谱吗,wpk德州局怎么透视(透视)竟然是有软件(哔哩哔哩)1、金币登录送、...
针对!wepoker怎么挂底牌... 针对!wepoker怎么挂底牌,微乐游戏公众号辅助器,普及透视(确实有挂)-哔哩哔哩1、玩家可以在w...
脚本曝光!hhpoker免费透... 脚本曝光!hhpoker免费透视脚本,HH平台挂(透视)其实是有工具(哔哩哔哩)1、实时透视辅助更新...
透视存在!wpk软件是正规的吗... 透视存在!wpk软件是正规的吗,微乐挖坑内购破解,推荐脚本(有挂攻略)-哔哩哔哩一、wpk软件是正规...
脚本了解!德普之星透视辅助软件... 脚本了解!德普之星透视辅助软件是真的吗,德普之星透视辅助软件下载(透视)确实是真的挂(哔哩哔哩)1、...