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

标签 iphone ios4 avfoundation avcapture

我正在使用 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/

相关文章:

iphone - XCode 4.4如何将文件永久添加到项目中

iphone - 创建我的第一个 iPhone 应用程序.. 重定位 block

ios - 在 Xcode 7 中为所有 iPhone 设备准确设置 Assets

iphone - 我如何在 iPhone sdk 中对 NSMutableArray 进行排序

iphone - 从 AVAssetReaderOutput 读取数据时 iOS 5.0 崩溃

ios - 无法在 swift 2 中使用 AVCaptureVideoDataOutput 通过 CGDataProviderCopyData 获取像素数据

iphone - 是否有一个通用的媒体查询来设置所有 iphone 4、4s、5 的样式

xcode - 错误 : Terminating app due to uncaught exception 'NSUnknownKeyException' , 原因:'[<UIApplication 0x6887a30> setValue:forUndefinedKey:]

iphone - 从 iPhone 应用程序以编程方式调用电话并在结束通话后返回应用程序

ios - 无法使 AVPortraitEffectsMatte 代码正常工作