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,

相关内容

热门资讯

总结透视!werplan辅助软... 总结透视!werplan辅助软件!确实存在有辅助神器(有挂教程)-哔哩哔哩1、玩家可以在werpla...
开挂透视!德普之星私人局透视,... 开挂透视!德普之星私人局透视,来玩德州破解器,资料教程(有挂细节)-哔哩哔哩一、德普之星私人局透视游...
专业透视!哈糖大菠萝助手!一直... 专业透视!哈糖大菠萝助手!一直真的有辅助软件(有挂头条)-哔哩哔哩1)哈糖大菠萝助手辅助插件:进一步...
解谜透视!约局吧德州有挂吗!一... 解谜透视!约局吧德州有挂吗!一贯存在有辅助软件(详细教程)-哔哩哔哩1)约局吧德州有挂吗辅助插件:进...
解迷透视!hhpoker辅助挂... 解迷透视!hhpoker辅助挂是真的吗,智星德州可以透视吗,方案教程(有挂方法)-哔哩哔哩1、完成h...
详情透视!hh poker辅助... 详情透视!hh poker辅助器先试用,红龙poker辅助器免费观看,绝活教程(有挂攻略)-哔哩哔哩...
详情透视!大菠萝免费辅助!好像... 详情透视!大菠萝免费辅助!好像是真的有辅助app(真的有挂)-哔哩哔哩1、大菠萝免费辅助公共底牌简单...
解谜透视!哈糖大菠萝有没有挂!... 解谜透视!哈糖大菠萝有没有挂!原来存在有辅助技巧(有挂助手)-哔哩哔哩哈糖大菠萝有没有挂是不是有人用...
辅助透视!pokemmo免费脚... 辅助透视!pokemmo免费脚本,安装不了wepoker,方案教程(有挂总结)-哔哩哔哩安装不了we...
有挂透视!聚星ai辅助工具下载... 有挂透视!聚星ai辅助工具下载!竟然真的是有辅助软件(竟然有挂)-哔哩哔哩1、让任何用户在无需聚星a...