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四人开挂教程书!wepo... WPK四人开挂教程书!wepoker脚本,AApokerai多功能开挂脚本wepoker脚本辅助器是...
重大来袭辅助!好友赣南ai辅助... 重大来袭辅助!好友赣南ai辅助多功能插件,本然有辅助方法学习是一款可以让一直输的玩家,快速成为一个“...
WPK有透视开挂积累!wpk可... WPK有透视开挂积累!wpk可以作弊吗,德扑之星ai多功能开挂技巧1、用户打开应用后不用登录就可以直...
每日必看推荐透视挂!奥云柳河麻... 每日必看推荐透视挂!奥云柳河麻将ai透视挂多功能插件,本然真的是有透视挂方法演示是一款可以让一直输的...
WPK控制牌辅助操作!wepo... WPK控制牌辅助操作!wepoker透视脚本是什么,AApokerai多功能辅助插件wepoker透...
解密教程透视挂!和悦游戏ai透... 您好:和悦游戏这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
WPK官网辅助方式!wepok... WPK官网辅助方式!wepoker免费脚本弱密码,德扑之星ai多功能辅助插件1、每一步都需要思考,不...
我来分享辅助器!来来舟山麻将a... 我来分享辅助器!来来舟山麻将ai辅助器多功能插件,一贯是有辅助器技巧方针是一款可以让一直输的玩家,快...
WPK四人辅助器指引!wpk安... WPK四人辅助器指引!wpk安卓下载辅助,WPKai多功能辅助器技巧1、许多玩家不知道wpk安卓下载...
今日公布透视!旺旺绍兴麻将ai... 旺旺绍兴麻将AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网...