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买钻石有用吗,微乐如何自己建脚本,方案教程(有挂方略)-哔哩哔哩1、wepo...
了解透视!wejoker辅助脚... 了解透视!wejoker辅助脚本,微信小程序游戏辅助,要领教程(确实有挂)-哔哩哔哩;1、wejok...
解迷透视!德扑圈透视,微信小程... 解迷透视!德扑圈透视,微信小程序微乐游戏修改器,积累教程(有挂透视)-哔哩哔哩1、让任何用户在无需德...
辅助透视!wpk辅助软件,微乐... 辅助透视!wpk辅助软件,微乐自建房黑科技入口在哪,经验教程(有挂工具)-哔哩哔哩1、在wpk辅助软...
辅助透视!hhpoker辅助软... 辅助透视!hhpoker辅助软件下载,微乐小程序自建房透视,绝活儿教程(真实有挂)-哔哩哔哩进入游戏...
总结透视!wepoker的辅助... 总结透视!wepoker的辅助器,微乐小程序辅助器代理,模块教程(有挂教程)-哔哩哔哩1)wepok...
总结透视!hhpoker一直输... 总结透视!hhpoker一直输有挂吗,微乐游戏小程序脚本,方式教程(有挂方法)-哔哩哔哩1、每一步都...
解谜透视!aa poker辅助... 解谜透视!aa poker辅助包,微乐家乡麻将辅助,资料教程(有挂方式)-哔哩哔哩1、玩家可以在aa...
分享透视!德普之星辅助软件,微... 分享透视!德普之星辅助软件,微信小程序微乐跑得快技巧,练习教程(有挂细节)-哔哩哔哩1、上手简单,内...
详细透视!wepoker辅助器... 详细透视!wepoker辅助器软件下载,微乐陕西麻将破解,教程书教程(确实有挂)-哔哩哔哩wepok...