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,

相关内容

热门资讯

透视中牌率!决战卡五星怎样才能... 透视中牌率!决战卡五星怎样才能赢,福建天天开心辅助,AA德州教程!(有挂方式)-哔哩哔哩;1、这是跨...
一秒答解!边锋小程序辅助器免费... 一秒答解!边锋小程序辅助器免费,樱花之盛正版辅助器免费,2026新版技巧(有挂教学)-哔哩哔哩;樱花...
普及知识!小逸碰胡插件脚本,小... 普及知识!小逸碰胡插件脚本,小旋风辅助器,专业教程(有挂秘笈)-哔哩哔哩;小逸碰胡插件脚本软件透视开...
透视讲解!微信闲来辅助神器,瓜... 透视讲解!微信闲来辅助神器,瓜瓜丰城双剑旧版攻略,透明挂教程!(有挂教学)-哔哩哔哩;瓜瓜丰城双剑旧...
透视有挂!518互游破解,哈糖... 透视有挂!518互游破解,哈糖大菠萝辅助,解密教程(发现有挂)-哔哩哔哩;哈糖大菠萝辅助是一项哈糖大...
科技分享!微信小程序辅助器免费... 科技分享!微信小程序辅助器免费2.0苹果版,闲来辅助神器,实用技巧(有挂讲解)-哔哩哔哩;人气非常高...
技术分享!约局吧辅助辅助外开挂... 技术分享!约局吧辅助辅助外开挂,来趣广西辅助,AA德州教程!(证实有挂)-哔哩哔哩;1、很好的约局吧...
透视工具!随意玩辅助器透视挂,... 透视工具!随意玩辅助器透视挂,四川熊猫辅助软件下载,攻略教程(有挂助手)-哔哩哔哩;AI辅助机器人普...
我来分享!奕乐辅助软件,中至江... 我来分享!奕乐辅助软件,中至江西插件,揭秘教程(证实有挂)-哔哩哔哩;是一款可以让一直输的玩家,快速...
重要通知!花花生活圈怎么挂辅助... 重要通知!花花生活圈怎么挂辅助,决胜辅助,必胜教程(有挂讲解)-哔哩哔哩;相信小伙伴都知道这个花花生...