2012-01-23 17 views
0

我有一個讀取H264視頻文件的功能。我想用它來順序讀取許多視頻文件。它似乎適用於文件(隨機時間),部分文件失敗(讀取一些文件而不是全部文件),然後完全失敗(讀取0幀)。我通過循環播放相同的視頻文件來測試它,所以這種不確定性很奇怪。使用AVAssetReader順序讀取視頻文件在OSX Lion中失敗

該錯誤消息我得到的是:

錯誤域= AVFoundationErrorDomain代碼= -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]); 
} 
+0

該問題指出,您在閱讀多個視頻文件時遇到困難,但描述和示例代碼似乎不處理多個文件。除非我錯過了一些東西,否則你應該改善問題的語言。 –

+0

我已將其更改爲顯示我的文件背靠背而不是同時讀取多個文件 –

回答

0

這可能是有趣的嘗試調用copyNextSampleBuffer後添加CMSampleBufferMakeDataReady()。當你打算使用這些數據時,通常會調用它,但我想知道某些解碼器需要調用這些來正確處理後續塊。

您還應該嘗試在樂器中對此進行分析,並查看在此循環中內存是否正在增長。它看起來像你在樣本緩衝區正確調用了CFRelease,但是在構建的AVAssetReaderOutput中可能會有一些大的自動釋放對象。如果它看起來像內存正在建立,那麼你可能會嘗試在循環內部放置一個NSAutoreleasePool來清理它。

+0

CMSampleBufferMakeDataReady()沒有任何區別。另外,在這個循環中內存不會增長。 OTOH,VTDecoderXPCService似乎暫時變得巨大(對於虛擬內存> 2 GB),然後它就消失了。 –

+0

好的,那麼你可以試試看https://developer.apple.com/library/mac/#samplecode/avexporter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011051上的avexport示例,看看是什麼您的代碼中存在差異。您也可以嘗試通過該示例代碼運行相同的電影,並查看它是否具有相同的問題。 Mac開發者示例代碼頁上還有其他AVFoundation示例。 –

+0

忘記該示例,請嘗試使用https://developer.apple.com/library/mac/#samplecode/ReaderWriter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011124中的AVReaderWriter示例。 –