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分析(透视)胡乐辅助脚本(一贯是有辅助工具)-哔哩哔哩透视工具!wepoke...
来临!we-poker辅助(透... 来临!we-poker辅助(透视)玄龙大厅怎么开辅助(真是是真的辅助软件)-哔哩哔哩所有人都在同一条...
透视最新!wpk真吗(透视)欢... 透视最新!wpk真吗(透视)欢乐对决脚本辅助器ios(总是真的有辅助插件)-哔哩哔哩1)欢乐对决脚本...
透视好友!wepoker破解是... 透视好友!wepoker破解是真的还是假的(透视)桂林字牌黑科技(确实是有辅助平台)-哔哩哔哩桂林字...
透视讲解!德普之星私人局辅助免... 透视讲解!德普之星私人局辅助免费(透视)海贝之城辅助器(都是有辅助神器)-哔哩哔哩德普之星私人局辅助...
透视模拟器!wepoker看底... 透视模拟器!wepoker看底牌(透视)天天卡五星辅助官网入口(果然存在有辅助下载)-哔哩哔哩暗藏猫...
此事备受玩家关注!hhpkoe... 此事备受玩家关注!hhpkoer辅助器视频(透视)丽水茶苑辅助有吗(竟然真的有辅助安装)-哔哩哔哩丽...
近年来!hhpoker透视工具... 近年来!hhpoker透视工具(透视)广东麻将辅助工具(竟然有辅助下载)-哔哩哔哩1、操作简单,无需...
迎来新发展!佛手大菠萝辅助(透... 您好,微信小程序雀神麻将好运来这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
刚刚!pokemmo手机版脚本... 刚刚!pokemmo手机版脚本免费(透视)开心十三张哈局辅助(总是是真的辅助插件)-哔哩哔哩一、开心...