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,

相关内容

热门资讯

科普透视!pokemmo脚本辅... 科普透视!pokemmo脚本辅助器下载,sohoopoker辅助(透视)原来存在有辅助脚本(哔哩哔哩...
曝光透视!约局吧德州透视,德州... 曝光透视!约局吧德州透视,德州私人局脚本(透视)好像是真的有辅助神器(哔哩哔哩)1、进入游戏-大厅左...
教你透视!拱趴大菠萝十三水作必... 教你透视!拱趴大菠萝十三水作必弊,xpoker辅助神器(透视)真是真的有辅助软件(哔哩哔哩)运拱趴大...
必备透视!智星德州插件2024... 必备透视!智星德州插件2024最新版,拱趴大菠萝机器人(透视)都是有辅助app(哔哩哔哩)智星德州插...
总结透视!智星德州插件2024... 总结透视!智星德州插件2024最新版,哈糖大菠萝有挂吗(透视)原来一直都是有辅助教程(哔哩哔哩)1、...
解密透视!pokemmo辅助工... 解密透视!pokemmo辅助工具,拱趴大菠萝十三水作必弊(透视)确实真的是有辅助方法(哔哩哔哩)1、...
普及透视!sohoo poke... 普及透视!sohoo poker辅助,pokemmo内置修改器(透视)果然有辅助神器(哔哩哔哩)1、...
专业透视!约局吧app有挂吗,... 专业透视!约局吧app有挂吗,佛手大菠萝辅助(透视)好像真的有辅助技巧(哔哩哔哩)1、任何约局吧ap...
了解透视!德州局透视脚本免费版... 了解透视!德州局透视脚本免费版下载手机版,约局吧如何查看是否有挂(透视)总是是有辅助教程(哔哩哔哩)...
了解透视!德州透视是真的吗,c... 了解透视!德州透视是真的吗,cloudpoker怎么开挂(透视)竟然是有辅助攻略(哔哩哔哩)1、每一...