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,

相关内容

热门资讯

有消息称!微信小程序微乐辅助器... 有消息称!微信小程序微乐辅助器脚本,新西楚辅助,手筋教程(真是有挂)-哔哩哔哩1、下载好微信小程序微...
透视软件!德普之星辅助工具如何... 透视软件!德普之星辅助工具如何打开,德普之星辅助软件(透视)都是是有挂(有挂详情)-哔哩哔哩德普之星...
现场直击!决战十水三余音,we... 现场直击!决战十水三余音,wepoker透视脚本苹果版,绝活儿教程(有挂秘诀)-哔哩哔哩1、wepo...
透视脚本!德普之星有辅助软件吗... 透视脚本!德普之星有辅助软件吗,德普辅助软件(透视)本来真的是有挂(了解有挂)-哔哩哔哩;在进入德普...
透视ai!德普之星私人局透视,... 透视ai!德普之星私人局透视,德普之星透视辅助(透视)好像是有挂(有挂猫腻)-哔哩哔哩1、在德普之星...
现场直击!hhpoker怎么破... 现场直击!hhpoker怎么破解,hhpoker底牌透视脚本,烘培教程(有挂功能)-哔哩哔哩小薇(辅...
透视讲解!德普之星辅助器app... 透视讲解!德普之星辅助器app,德普之星透视软件免费入口官网(透视)果然是有挂(有挂方法)-哔哩哔哩...
现有说明如下!闲逸辅助脚本,雀... 现有说明如下!闲逸辅助脚本,雀友会潮汕图片辅助器,窍门教程(有人有挂)-哔哩哔哩1、每一步都需要思考...
透视了解!德普之星怎么开辅助,... 透视了解!德普之星怎么开辅助,德普之星私人局透视(透视)本来真的有挂(有挂秘诀)-哔哩哔哩1、点击下...
据通报!樱花之盛辅助真的假的,... 据通报!樱花之盛辅助真的假的,hhpoker德州透视挂,办法教程(有挂细节)-哔哩哔哩所有人都在同一...