1

我有读取 H264 视频文件的功能。我想用它来顺序读取许多视频文件。它似乎适用于文件(随机时间),部分文件失败(读取一些而不是所有帧)然后完全失败(读取 0 帧)。我通过循环播放同一个视频文件来测试这个,所以这种不确定性很奇怪。

我得到的错误信息是:

Error Domain=AVFoundationErrorDomain Code=-11800“操作无法完成” UserInfo=0x105d07480 {NSLocalizedFailureReason=发生未知错误(-6662),NSLocalizedDescription=操作无法完成,NSUnderlyingError=0x100159350“操作无法完成已完成。(OSStatus 错误 -6662。)"}

我使用 ARC 和 OSX Lion。任何帮助深表感谢:

void uncompressMovie(NSString *moviePath) { 
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:moviePath]];

NSArray* video_tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *video_track = [video_tracks objectAtIndex:0];

// Decompress to ARGB with the asset reader
NSDictionary *decompressionVideoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB], (id)kCVPixelBufferPixelFormatTypeKey,
                                            [NSDictionary dictionary], (id)kCVPixelBufferIOSurfacePropertiesKey,
                                            nil];
AVAssetReaderTrackOutput *asset_reader_output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:video_track outputSettings:decompressionVideoSettings];

NSError *error = [[NSError alloc] init];
AVAssetReader *asset_reader = [[AVAssetReader alloc] initWithAsset:asset error:&error]; 

if ([asset_reader canAddOutput:asset_reader_output]) {
    [asset_reader addOutput:asset_reader_output];

    if ([asset_reader startReading] == YES) {
        int count = 0;

        while ( [asset_reader status]==AVAssetReaderStatusReading ) {
            sampleBuffer = [asset_reader_output copyNextSampleBuffer];
            if (sampleBuffer == NULL) {
                if ([asset_reader status] == AVAssetReaderStatusFailed) 
                    break;
                else    
                    continue;
            }
            count++;

            // Will do some work here            

            CFRelease(sampleBuffer);
        }

        if (count == 0) {
            NSLog(@"I am doomed %@", [asset_reader error]);
            exit(1);
        }

        NSLog(@"Processed %d frames from %@", count, moviePath);
    } else
        NSLog(@"Couldn't start");
}

if ([asset_reader status] != AVAssetReaderStatusCompleted)
    [asset_reader cancelReading];
// unlink([moviePath UTF8String]);
}
4

1 回答 1

0

在调用 copyNextSampleBuffer 之后尝试添加 CMSampleBufferMakeDataReady() 可能会很有趣。这通常在您要使用数据时调用,但我想知道某些解码器需要调用它以正确处理后续块。

您还应该尝试在 Instruments 中对此进行分析,并查看在此循环中内存是否在增长。看起来您在示例缓冲区上正确调用了 CFRelease,但 AVAssetReaderOutput 中可能有一些大型的自动释放对象正在建立。如果看起来内存在增加,那么您可以尝试在循环中放置一个 NSAutoreleasePool 来清理它。

于 2012-01-23T04:06:16.777 回答