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,

相关内容

热门资讯

教程辅助!微乐自建房辅助工具神... 教程辅助!微乐自建房辅助工具神器,微乐家乡麻将自建房辅助app(开挂自建房)插件暗藏猫腻,小编详细说...
一分钟揭秘!微乐微信自建房辅助... 一分钟揭秘!微乐微信自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用后不...
玩家必备教程!微乐自建房辅助工... 玩家必备教程!微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)攻略微乐自建房辅助工具脚本下载...
实测揭晓!微乐家乡麻将自建房辅... 实测揭晓!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)软件1、点击下载安...
推荐几款新版!微乐自建房辅助工... 推荐几款新版!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)攻略1、下载好微乐自建房辅助工...
玩家必知教程!微乐家乡麻将自建... 玩家必知教程!微乐家乡麻将自建房辅助app,微乐小程序免费黑科技(开挂自建房)软件1、点击下载安装,...
今日重大通报!微信雀神麻将挂件... 今日重大通报!微信雀神麻将挂件开挂辅助,微乐小程序免费黑科技(开挂自建房)器1、起透看视 微信雀神麻...
总算了解!微乐自建房辅助工具脚... 总算了解!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)攻略微乐家乡麻将自建房辅...
今日头条!微乐自建房辅助工具免... 今日头条!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)app1、微乐自建房辅助工...
教学盘点!微乐自建房辅助工具免... 教学盘点!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)工具1、该软件可以轻松地帮...