gpt4 book ai didi

iphone - AVCaptureMovieFileOutput -- 写入时修剪文件

转载 作者:行者123 更新时间:2023-12-03 19:39:15 25 4
gpt4 key购买 nike

我正在使用 AVCaptureMovieFileOutput 录制视频。然而,我只想保留最后 2 分钟的视频,而不是在整个录制时间内保留捕获的视频。本质上,我想创建一个视频的尾随缓冲区。

我尝试通过将 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 写入吗?

如果此方法不起作用,如何才能实现无缝尾部视频缓冲区的效果?

谢谢!

最佳答案

不确定您在这里想要实现的目标是否有效,这是因为就像您所说,您在尝试修剪文件的同时编写文件,为什么不能录制视频并稍后修剪它?如果你真的想随时保留两分钟的视频,你可能想尝试使用 AVCaptureVideoDataOutput,使用它你将获得视频帧,你可以使用 AVAssetWriter 将其写入压缩并将帧写入文件,请查看此SO关于如何做到这一点的问题 This code to write video+audio through AVAssetWriter and AVAssetWriterInputs is not working. Why?

关于iphone - AVCaptureMovieFileOutput -- 写入时修剪文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7166164/

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