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,

相关内容

热门资讯

透视科技!德普之星有透视辅助吗... 透视科技!德普之星有透视辅助吗,佛手大菠萝13道挂哪里,诀窍教程(真是真的有挂)-哔哩哔哩1、超多福...
透视俱乐部!wepoker有辅... 透视俱乐部!wepoker有辅助插件吗(透视)开挂辅助技巧(竟然是有挂)-哔哩哔哩进入游戏-大厅左侧...
透视挂!wepoker钻石怎么... 透视挂!wepoker钻石怎么看底牌,wepoker手机版辅助,总结教程(真是真的是有挂)-哔哩哔哩...
透视安卓版!pokemmo辅助... 透视安卓版!pokemmo辅助工具(透视)开挂辅助技巧(都是真的有挂)-哔哩哔哩pokemmo辅助工...
透视神器!aapoker插件,... 透视神器!aapoker插件,pokerworld辅助器,攻略教程(竟然有挂)-哔哩哔哩;该软件可以...
透视教学!hhpoker德州透... 透视教学!hhpoker德州透视挂,约局吧德州真的存在透视吗,指引教程(其实是有挂)-哔哩哔哩1、约...
透视神器!wepoker有没有... 透视神器!wepoker有没有机器人(透视)开挂辅助器(本来是有挂)-哔哩哔哩1、完成wepoker...
透视美元局!德普之星透视免费,... 透视美元局!德普之星透视免费,wepoker有插件吗,技法教程(其实真的有挂)-哔哩哔哩;1、玩家可...
透视最新版!模拟器打开hhpo... 透视最新版!模拟器打开hhpoker(透视)开挂辅助工具(原来有挂)-哔哩哔哩1)模拟器打开hhpo...
透视最新!wepoker数据分... 透视最新!wepoker数据分析,wepokerplus辅助,法门教程(本来存在有挂)-哔哩哔哩运w...