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,

相关内容

热门资讯

黑科技能赢(扑克之城)外挂透明... 黑科技能赢(扑克之城)外挂透明挂辅助神器(软件透明挂)果然真的是有挂(有挂实锤)-哔哩哔哩;1、该软...
黑科技挂(wpk大厅)外挂透明... 黑科技挂(wpk大厅)外挂透明挂辅助下载(智能ai)竟然是真的有挂(有挂秘籍)-哔哩哔哩1、让任何用...
黑科技有挂(聚星扑克)外挂透明... 黑科技有挂(聚星扑克)外挂透明挂辅助设备(智能ai)一贯是真的有挂(了解有挂)-哔哩哔哩;1)聚星扑...
黑科技app(wpk微扑克智能... 黑科技app(wpk微扑克智能)外挂透明挂辅助ai代打(透视)总是真的是有挂(有挂实锤)-哔哩哔哩;...
黑科技app(WPK代码)外挂... 黑科技app(WPK代码)外挂透明挂辅助ai代打(软件透明挂)原来是真的有挂(证实有挂)-哔哩哔哩1...
黑科技线上(Wepoke科技)... 黑科技线上(Wepoke科技)外挂透明挂辅助助手(软件透明挂)竟然存在有挂(真的有挂)-哔哩哔哩1、...
黑科技智能ai(wpk模拟器)... 黑科技智能ai(wpk模拟器)外挂透明挂辅助器(透明黑科技)竟然存在有挂(新版有挂)-哔哩哔哩1)w...
黑科技好友房(红龙poker)... 黑科技好友房(红龙poker)外挂透明挂辅助代打(透明黑科技)其实真的有挂(有挂攻略)-哔哩哔哩1、...
黑科技新版(云扑克德州)外挂透... 黑科技新版(云扑克德州)外挂透明挂辅助代打(透明挂)原来真的是有挂(今日头条)-哔哩哔哩云扑克德州辅...
黑科技好牌(哈糖大菠萝十三张)... 黑科技好牌(哈糖大菠萝十三张)外挂透明挂辅助软件(辅助挂)切实是有挂(果真有挂)-哔哩哔哩;1、哈糖...