gpt4 book ai didi

objective-c - 从 AVAssetWriter 读取实时 H.264 输出时数据损坏

转载 作者:太空狗 更新时间:2023-10-30 03:52:41 26 4
gpt4 key购买 nike

我正在使用一些技巧来尝试在将 AVAssetWriter 写入磁盘时读取其原始输出。当我通过连接它们重新组装单个文件时,生成的文件与 AVAssetWriter 的输出文件的字节数完全相同。但是,重组后的文件不会在 QuickTime 中播放,也不会被 FFmpeg 解析,因为存在数据损坏。此处和那里的几个字节已更改,导致生成的文件无法使用。我假设这是在每次读取的 EOF 边界上发生的,但这不是一致的损坏。

我计划最终使用与此类似的代码从编码器中解析出单个 H.264 NAL 单元,将它们打包并通过 RTP 发送它们,但是如果我不相信从磁盘读取的数据,我可能不得不使用其他解决方案。

是否有针对此数据损坏的解释/修复?您是否找到了关于如何解析 NAL 单元以通过 RTP 打包的任何其他资源/链接?

完整代码在这里:AVAppleEncoder.m

// Modified from
// http://www.davidhamrick.com/2011/10/13/Monitoring-Files-With-GCD-Being-Edited-With-A-Text-Editor.html
- (void)watchOutputFileHandle
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
int fildes = open([[movieURL path] UTF8String], O_EVTONLY);

source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes,
DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
queue);
dispatch_source_set_event_handler(source, ^
{
unsigned long flags = dispatch_source_get_data(source);
if(flags & DISPATCH_VNODE_DELETE)
{
dispatch_source_cancel(source);
//[blockSelf watchStyleSheet:path];
}
if(flags & DISPATCH_VNODE_EXTEND)
{
//NSLog(@"File size changed");
NSError *error = nil;
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:movieURL error:&error];
if (error) {
[self showError:error];
}
[fileHandle seekToFileOffset:fileOffset];
NSData *newData = [fileHandle readDataToEndOfFile];
if ([newData length] > 0) {
NSLog(@"newData (%lld): %d bytes", fileOffset, [newData length]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *movieName = [NSString stringWithFormat:@"%d.%lld.%d.mp4", fileNumber, fileOffset, [newData length]];
NSString *path = [NSString stringWithFormat:@"%@/%@", basePath, movieName];
[newData writeToFile:path atomically:NO];
fileNumber++;
fileOffset = [fileHandle offsetInFile];
}
}
});
dispatch_source_set_cancel_handler(source, ^(void)
{
close(fildes);
});
dispatch_resume(source);
}

以下是我发现的一些类似问题,但没有完全回答我的问题:

当我最终解决这个问题时,我将发布一个开源库来帮助将来尝试这样做的人。

谢谢!

更新:损坏不会发生在 EOF 边界。在调用 finishWriting 之后,文件的某些部分似乎被重写了。第一个文件被分块为 4KB,因此更改的区域不在 EOF 边界附近。当启用 movieFragmentInterval 时,它似乎也在新的“moov”元素附近损坏。

broken file左边是正确的文件,右边是损坏的文件。

最佳答案

我最终放弃了“边写边读”的方法,转而采用手动分块方法,我在后台线程上每 5 秒调用一次 finishWriting。使用方法 originally described here,我能够丢弃可忽略不计的帧数:

- (void) segmentRecording:(NSTimer*)timer {
AVAssetWriter *tempAssetWriter = self.assetWriter;
AVAssetWriterInput *tempAudioEncoder = self.audioEncoder;
AVAssetWriterInput *tempVideoEncoder = self.videoEncoder;
self.assetWriter = queuedAssetWriter;
self.audioEncoder = queuedAudioEncoder;
self.videoEncoder = queuedVideoEncoder;
//NSLog(@"Switching encoders");

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[tempAudioEncoder markAsFinished];
[tempVideoEncoder markAsFinished];
if (tempAssetWriter.status == AVAssetWriterStatusWriting) {
if(![tempAssetWriter finishWriting]) {
[self showError:[tempAssetWriter error]];
}
}
if (self.readyToRecordAudio && self.readyToRecordVideo) {
NSError *error = nil;
self.queuedAssetWriter = [[AVAssetWriter alloc] initWithURL:[self newMovieURL] fileType:(NSString *)kUTTypeMPEG4 error:&error];
if (error) {
[self showError:error];
}
self.queuedVideoEncoder = [self setupVideoEncoderWithAssetWriter:self.queuedAssetWriter formatDescription:videoFormatDescription bitsPerSecond:videoBPS];
self.queuedAudioEncoder = [self setupAudioEncoderWithAssetWriter:self.queuedAssetWriter formatDescription:audioFormatDescription bitsPerSecond:audioBPS];
//NSLog(@"Encoder switch finished");

}
});
}

完整源代码:https://github.com/chrisballinger/FFmpeg-iOS-Encoder/blob/master/AVSegmentingAppleEncoder.m

关于objective-c - 从 AVAssetWriter 读取实时 H.264 输出时数据损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13166341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com