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,

相关内容

热门资讯

攻略讲解!掌中乐游戏中心有辅助... 攻略讲解!掌中乐游戏中心有辅助吗(透视)一直是有挂(2020已更新)(哔哩哔哩)1、掌中乐游戏中心有...
九分钟了解!心悦麻将软挂神器,... 九分钟了解!心悦麻将软挂神器,八闽十三张十三水有外挂吗,AI教程(有挂软件);1、很好的工具软件,可...
必备教程!wepoke打伙牌(... 必备教程!wepoke打伙牌(wepoke真的有挂)wepoke(好像真的有挂)需要回顾用户提供的搜...
教程攻略!八闽状元郎在哪里开挂... 教程攻略!八闽状元郎在哪里开挂(透视)一直是有挂(2020已更新)(哔哩哔哩)1、让任何用户在无需八...
we辅助poker德之星!we... we辅助poker德之星!wepoke软件透明,wepOke原来真的有挂,2025版教程(有挂揭秘)...
五分钟了解!掌中乐游戏中心吗,... 五分钟了解!掌中乐游戏中心吗,兴动辅助器免费版v3.0,技巧教程(有挂ai代打)1、兴动辅助器免费版...
微扑克系统发牌规律!微扑克辅助... 微扑克系统发牌规律!微扑克辅助是真的,微扑克可以加入俱乐部,揭秘攻略(有挂脚本)1、这是跨平台的微扑...
技术分享!狂飙娱乐辅助器(辅助... 技术分享!狂飙娱乐辅助器(辅助)好像真的有挂(2023已更新)(哔哩哔哩)1)狂飙娱乐辅助器辅助挂:...
七分钟了解!闲玩游戏外 挂,蛮... 七分钟了解!闲玩游戏外 挂,蛮籽麻将有没有套路,AI教程(有挂解密);暗藏猫腻,小编详细说明蛮籽麻将...
wepoke有挂!wepoke... wepoke有挂!wepoke模拟器,wepoke有挂,透明教程(有挂介绍);亲真的是有正版授权,小...