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,

相关内容

热门资讯

最新消息!xpoker透视辅助... 最新消息!xpoker透视辅助(透视)微信微乐辅助器免费安装(其实有辅助技巧)-哔哩哔哩1、进入到x...
2026版攻略!wepoker... 2026版攻略!wepoker免费脚本咨询(透视)微乐家乡app下载(真是真的是有辅助挂)-哔哩哔哩...
备受关注的!agpoker辅助... 备受关注的!agpoker辅助(透视)微乐小程序自建房辅助ios(竟然真的是有辅助方法)-哔哩哔哩1...
复盘辅助挂!wepoker怎么... 复盘辅助挂!wepoker怎么发冤家牌(透视)微信小程序微乐安徽辅助苹果(都是真的有辅助器)-哔哩哔...
透视计算!拱趴大菠萝有什么挂(... 透视计算!拱趴大菠萝有什么挂(透视)微乐家乡小程序修改器(原来是真的辅助插件)-哔哩哔哩1、拱趴大菠...
据监测!wepokerplus... 据监测!wepokerplus到底是挂了吗(透视)微信小程序微乐辅助器贴吧(总是有辅助教程)-哔哩哔...
透视辅助!wepoker透视功... 透视辅助!wepoker透视功能下载(透视)微乐小程游戏破解器下载(好像存在有辅助工具)-哔哩哔哩1...
做出回应!wepoker能不能... 做出回应!wepoker能不能透视(透视)微乐辅助软件购买平台(一贯真的有辅助挂)-哔哩哔哩1、每一...
攻略辅助挂!哈糖大菠萝辅助器(... 攻略辅助挂!哈糖大菠萝辅助器(透视)微乐家乡官方免费下载安装(其实存在有辅助方法)-哔哩哔哩哈糖大菠...
近年来!pokemmo修改器手... 近年来!pokemmo修改器手机版(透视)微乐家乡app辅助器(好像是真的辅助神器)-哔哩哔哩1、p...