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,

相关内容

热门资讯

wepoke透明挂!wepow... wepoke透明挂!wepower轻量版辅助器,(wEpoKe)其实真的是有挂,分享教程(黑科技辅助...
wepoke透明挂使用方法!w... wepoke透明挂使用方法!wepower微扑克有无外挂,(wePoKe)一贯存在有挂,系统教程(黑...
德扑之星ai代打!德扑ai软件... 德扑之星ai代打!德扑ai软件购买,(德扑ai)好像是有挂,详细教程(黑科技功能)1、德扑之星ai代...
aapoker有作弊吗!aa扑... aapoker有作弊吗!aa扑克辅助,(AAPOKEr)一贯是真的有挂,新2025教程(黑科技攻略)...
微扑克ai辅助神器!wpk智能... 微扑克ai辅助神器!wpk智能ai,(WpK)一贯是有挂,AI教程(黑科技教程)1、微扑克ai辅助神...
aapoker辅助工具ai!a... aapoker辅助工具ai!aapoker外挂实测,(aaPoker)好像是有挂,揭秘攻略(黑科技辅...
wepoke辅助挂!wepow... wepoke辅助挂!wepower有辅助器吗,(WEpoke)其实存在有挂,实用技巧(黑科技助手);...
wepoke计算辅助!wepo... wepoke计算辅助!wepoke辅助软件靠谱吗,(wePOke)确实存在有挂,安装教程(黑科技神器...
aapoker外挂实测!aap... aapoker外挂实测!aapoker透视辅助器,(aApoker)竟然存在有挂,科技教程(黑科技方...
wpkai辅助软件推荐!wpk... wpkai辅助软件推荐!wpk最新黑科技,(WPk)切实真的有挂,透视教程(黑科技软件)小薇(透视辅...