首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVCaptureMovieFileOutput --在写入时修剪文件

AVCaptureMovieFileOutput --在写入时修剪文件
EN

Stack Overflow用户
提问于 2011-08-24 03:02:25
回答 2查看 4.3K关注 0票数 3

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

我尝试通过将movieFragmentInterval设置为等于15秒来实现这一点。在缓冲这15秒时,MOV文件的前15秒将使用以下代码进行修剪:

代码语言:javascript
复制
//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时,我都会收到以下错误:

代码语言:javascript
复制
Failed:Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save" UserInfo=0x12e710 {NSLocalizedRecoverySuggestion=Try saving again., NSLocalizedDescription=Cannot Save}

这是因为AVCaptureMovieFileOutput已经在写入该文件吗

如果这种方法不起作用,如何达到无缝拖尾视频缓冲区的效果?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-24 03:07:59

我不确定你在这里想要达到的效果,这是因为就像你说的,你正在写文件,同时试图裁剪它,为什么你不能录制视频并在以后裁剪它呢?如果你真的想在任何时候只保留两分钟的视频,你可能想要尝试使用AVCaptureVideoDataOutput,使用它你会得到视频帧,你可以使用AVAssetWriter将它写入压缩和写入帧到文件中,请查看这个问题,它谈到了如何做This code to write video+audio through AVAssetWriter and AVAssetWriterInputs is not working. Why?

票数 3
EN

Stack Overflow用户

发布于 2011-09-27 15:22:16

我怀疑您收到的错误是因为您试图覆盖与导出URL中相同的文件。文档中写道:“如果您试图覆盖现有文件,或者在应用程序的沙箱之外写入文件,则导出将失败。如果您需要覆盖现有文件,则必须首先将其删除。”

要获取视频的最后两分钟,您可能希望首先使用loadValuesAsynchronouslyForKeys获取它的持续时间,这是另一个异步调用。使用此持续时间,您可以创建时间范围,并通过将视频导出到不同的URL来裁切视频。

代码语言:javascript
复制
CMTime start = CMTimeMakeWithSeconds(durationObtained - 120, 600); 
CMTime duration = CMTimeMakeWithSeconds(120, 600);
CMTimeRange range = CMTimeRangeMake(start, duration);  
exportSession.timeRange = range;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7166164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档