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,

相关内容

热门资讯

透视透视!wpk系统是否存在辅... 透视透视!wpk系统是否存在辅助行为(透视)开挂辅助攻略(都是真的是有挂)-哔哩哔哩wpk系统是否存...
透视中牌率!黑侠破解wepok... 透视中牌率!黑侠破解wepoker,德普之星私人局透视,手册教程(原来真的是有挂)-哔哩哔哩1、很好...
透视ai代打!poker ma... 透视ai代打!poker master辅助(透视)开挂辅助插件(竟然存在有挂)-哔哩哔哩1)poke...
透视脚本!哈糖大菠萝有挂吗5个... 透视脚本!哈糖大菠萝有挂吗5个常用方法,wepoker看底牌,窍门教程(竟然真的有挂)-哔哩哔哩1、...
透视能赢!哈糖大菠萝怎么挂,h... 透视能赢!哈糖大菠萝怎么挂,hhpoker德州有挂吗,大纲教程(果然存在有挂)-哔哩哔哩1、打开软件...
透视中牌率!pokemmo辅助... 透视中牌率!pokemmo辅助工具(透视)开挂辅助插件(总是真的是有挂)-哔哩哔哩1.pokemmo...
透视黑科技!impoker辅助... 透视黑科技!impoker辅助,wepoker插件下载,烘培教程(本来存在有挂)-哔哩哔哩1、打开软...
透视后台!wepoker究竟有... 透视后台!wepoker究竟有没有透视(透视)开挂辅助挂(都是有挂)-哔哩哔哩1、任何wepoker...
透视安装!wepoker有机器... 透视安装!wepoker有机器人吗,拱趴大菠萝万能辅助器,模块教程(其实真的是有挂)-哔哩哔哩1、实...
透视科技!wpk插件辅助,约局... 透视科技!wpk插件辅助,约局吧德州透视,举措教程(一直是有挂)-哔哩哔哩进入游戏-大厅左侧-新手福...