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,

相关内容

热门资讯

解谜透视!聚星ai辅助工具激活... 解谜透视!聚星ai辅助工具激活码!必备科技辅助挂(发现有挂)-哔哩哔哩1)聚星ai辅助工具激活码免费...
曝光透视!wpk作弊!科普辅助... 曝光透视!wpk作弊!科普辅助方法(有挂解惑)-哔哩哔哩1、下载好wpk作弊透视辅助下载之后点击打开...
有挂透视!wepoker免费脚... 有挂透视!wepoker免费脚本咨询!一分钟带你了解辅助攻略(有挂教学)-哔哩哔哩wepoker免费...
详细透视!德州透视是真的吗!指... 详细透视!德州透视是真的吗!指导大家辅助器(有挂实锤)-哔哩哔哩1、点击下载安装,德州透视是真的吗脚...
教你透视!hhpoker外挂靠... 教你透视!hhpoker外挂靠谱吗!分享给玩家辅助攻略(有挂方法)-哔哩哔哩hhpoker外挂靠谱吗...
详情透视!wepoker有辅助... 详情透视!wepoker有辅助吗!每日必备辅助挂(详细教程)-哔哩哔哩一、wepoker有辅助吗游戏...
必备透视!wepoker有插件... 必备透视!wepoker有插件吗!热点推荐辅助脚本(有挂透视)-哔哩哔哩1、点击下载安装,wepok...
揭幕透视!hardrock透视... 揭幕透视!hardrock透视工具!终于知道辅助app(有挂秘籍)-哔哩哔哩1、不需要AI权限,帮助...
开挂透视!wepoker有人用... 您好,wepoker有人用过吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
解迷透视!哈糖大菠萝开挂!玩家... 解迷透视!哈糖大菠萝开挂!玩家交流辅助教程(揭秘有挂)-哔哩哔哩该软件可以轻松地帮助玩家将哈糖大菠萝...