我使用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
?
如何實現無縫拖尾視頻緩衝區的效果,如果此方法無法工作?
謝謝!
使用你的方法,我不會遇到像以前一樣的問題?在我嘗試編輯文件的同時,我仍然會寫入該文件。 – James
好,輸出沒有寫入文件了,現在你有緩衝區,正將其壓縮成一個文件,所以你控制什麼被writen的文件,而不是試圖訪問一個文件,這就是正在使用的AV基礎,所以你不會遇到同樣的問題 – Daniel
哦,好吧!感謝您的澄清! – James