使用AVCaptureSession录制视频不会保存任何视频

时间:2015-01-20 03:21:43

标签: ios ios8 avcapturesession

我正在尝试录制视频并使用AVCaptureSession将其保存到文件中。我正在使用单例来处理我的捕获会话,因为我的应用程序中有几个VC,我正在显示视频预览。

视频预览显示正确,但当我尝试录制视频时,一切似乎都有效,但我没有收到任何错误,但是当我查看应用容器时,没有保存任何视频文件。

以下是我的Capture Session代码

中的相关功能
-(void)recordVideoSetup{
    // Code adapted from
    // www.ios-developer.net/iphone-ipad-programmer/development/camera/record-video-with-avcapturesession-2

    NSLog(@"Adding Movie File Output");

    MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

    Float64 TotalSeconds = 600; // 10 minutes max
    int32_t preferredTimeScale = 30; //fps
    CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale); // Set Max Duration

    MovieFileOutput.maxRecordedDuration = maxDuration;
    MovieFileOutput.minFreeDiskSpaceLimit = 1024*1024*10; // 10 MB

    if ([captureSession canAddOutput:MovieFileOutput]) {
        NSLog(@"Added output to capture session");
        [captureSession addOutput:MovieFileOutput];
    }


    AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];

    // SET LANDSCAPE ORIENTATION
    if ([CaptureConnection isVideoOrientationSupported]) {
        AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight;
        [CaptureConnection setVideoOrientation:orientation];
    }


    NSLog(@"Setting image quality");
    [captureSession setSessionPreset:AVCaptureSessionPresetMedium];
    if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480])
        [captureSession setSessionPreset:AVCaptureSessionPreset640x480];

}

-(NSURL*)startRecordingVideo:(NSString *)surveyID {
    DDLogCInfo(@"Video Recording Started");

    // Get URL to record to

    NSString *documentsDirPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    documentsDirPath = [documentsDirPath stringByAppendingPathComponent:@"video"];
    NSURL *documentsDirUrl = [NSURL fileURLWithPath:documentsDirPath isDirectory:YES];
    NSString *filename = [NSString stringWithFormat:@"%@.mp4", surveyID];
    NSURL *url = [NSURL URLWithString:filename relativeToURL:documentsDirUrl];

    DDLogCInfo(@"Path to video: %@", url.path);

    NSFileManager *filemanager = [NSFileManager defaultManager];
    if ([filemanager fileExistsAtPath:[documentsDirPath stringByAppendingString:filename]]) {
        NSError *error;
        if ([filemanager removeItemAtURL:url error:&error] == NO) {
            DDLogCError(@"Could not record video");
            return nil;
        }
    }

    [MovieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];

    return url;
}

-(void)stopRecordingVideo {
    DDLogCInfo(@"Video Recording Ended");
    [MovieFileOutput stopRecording];

}


-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {

    // Check if Recording was successfull

    BOOL recordedSuccessfully = YES;

    if ([error code] != noErr) {
        NSLog(@"A Problem occurred with recording");

        // A Problem Occurred
        id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
        if (value) {
            recordedSuccessfully = [value boolValue];
        }
    }

    if (!recordedSuccessfully) {
        DDLogCInfo(@"!!! - VIDEO RECORDING ERROR");
    }

}

编辑:一些额外的信息 这是我得到的日志输出:

SafeCurveSpeed[9060:3691230] Adding Movie File Output
SafeCurveSpeed[9060:3691230] Added output to capture session
SafeCurveSpeed[9060:3691230] Setting image quality
SafeCurveSpeed[9060:807] Video Recording Started
SafeCurveSpeed[9060:807] Path to video: "/var/mobile/Containers/Data/Application/44A52ADB-0E36-4303-9DE3-99E9CCCAF3FC/Documents/video/survey-video-10.mp4"
SafeCurveSpeed[9060:807] Video Recording Ended

编辑2: 我已经添加了一种方法来观察" AVCaptureSessionRuntimeErrorNotification"通知,但在录制期间没有被解雇。我还在captureOutput didFinishRecordingToOutputFileAtURL:方法的顶部添加了一个日志调用,发现录制完成后根本没有达到该方法。但仍然不确定原因。

1 个答案:

答案 0 :(得分:0)

之后添加此代码 视频路径:

UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

在VC中添加此方法

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo {
    if (error == nil) {
       [[[UIAlertView alloc] initWithTitle:@"Saved to camera roll" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
}

使用此第三方库

https://www.cocoacontrols.com/controls/screcorder

https://github.com/rFlex/SCRecorder

http://www.ios-developer.net/iphone-ipad-programmer/development/camera/record-video-with-avcapturesession-2