2011-08-23 61 views
3

我使用AVCaptureMovieFileOutput來錄製視頻。然而,我並不是在整個錄製時間保留捕獲的視頻,而是隻保留最後2分鐘的視頻。實質上,我想創建視頻的尾隨緩衝區。AVCaptureMovieFileOutput - 在寫入時修剪文件

我試圖通過設置movieFragmentInterval等於15秒來實現此目的。由於這些15秒緩衝時,MOV文件的前15秒將使用這段代碼被修剪掉:

//This would be called 7 seconds after the video stream started buffering. 
-(void)startTrimTimer 
{ 
    trimTimer = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(trimFlashbackBuffer) userInfo:nil repeats:YES]; 
} 

    -(void)trimFlashbackBuffer 
    { 
     //make sure that there is enough video before trimming off 15 seconds 
     if(trimReadyCount<3){ 
      trimReadyCount++; 
      return; 
     } 

     AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/flashbackBuffer.MOV",tripDirectory]] options:nil]; 

     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:AVAssetExportPresetHighestQuality]; 
     exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/flashbackBuffer.MOV",tripDirectory]]; 
     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
     CMTimeRange timeRange = CMTimeRangeMake(CMTimeMake(15000, 1000), CMTimeMake(120000, 1000)); 
     exportSession.timeRange = timeRange; 

     [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
      switch (exportSession.status) { 
       case AVAssetExportSessionStatusCompleted: 
        // Custom method to import the Exported Video 
        [self loadAssetFromFile:exportSession.outputURL]; 
        break; 
       case AVAssetExportSessionStatusFailed: 
        // 
        NSLog(@"Failed:%@",exportSession.error); 
        break; 
       case AVAssetExportSessionStatusCancelled: 
        // 
        NSLog(@"Canceled:%@",exportSession.error); 
        break; 
       default: 
        break; 
      } 
     }]; 

    } 

不過,我收到以下錯誤每次trimFlashbackBuffer被稱爲:

Failed:Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save" UserInfo=0x12e710 {NSLocalizedRecoverySuggestion=Try saving again., NSLocalizedDescription=Cannot Save} 

這是因爲該文件已被寫入AVCaptureMovieFileOutput

如何實現無縫拖尾視頻緩衝區的效果,如果此方法無法工作?

謝謝!

回答

3

不確定你試圖在這裏實現的效果是什麼,這是因爲像你說的那樣,你正在試圖修剪它時寫文件,爲什麼不能記錄視頻並在以後修剪它?如果你真的想只保留隨時視頻2分鐘你可能想使用AVCaptureVideoDataOutput嘗試,利用這一點,你會得到的視頻幀,您可以使用AVAssetWriter寫它壓縮並寫入幀到一個文件,看看這個SO這個問題談到如何做到這一點This code to write video+audio through AVAssetWriter and AVAssetWriterInputs is not working. Why?

+0

使用你的方法,我不會遇到像以前一樣的問題?在我嘗試編輯文件的同時,我仍然會寫入該文件。 – James

+0

好,輸出沒有寫入文件了,現在你有緩衝區,正將其壓縮成一個文件,所以你控制什麼被writen的文件,而不是試圖訪問一個文件,這就是正在使用的AV基礎,所以你不會遇到同樣的問題 – Daniel

+0

哦,好吧!感謝您的澄清! – James

2

我懷疑你得到的錯誤是因爲你正在嘗試覆蓋導出網址相同的文件。 文檔說:「如果你試圖覆蓋現有文件,或寫應用程序沙箱之外的文件導出將失敗。如果你需要覆蓋現有文件,則必須先刪除它。」

要獲得視頻的最後兩分鐘,您可能需要先使用另一個異步調用loadValuesAsynchronouslyForKeys獲取它的持續時間。使用此持續時間,您可以創建時間範圍,並通過將視頻導出到其他網址來修剪視頻。

CMTime start = CMTimeMakeWithSeconds(durationObtained - 120, 600); 
CMTime duration = CMTimeMakeWithSeconds(120, 600); 
CMTimeRange range = CMTimeRangeMake(start, duration); 
exportSession.timeRange = range;