SCRecorder:如何从本地视频创建SCRecordSession

问题描述:

我使用SCRecorder来自定义视频。我想导入一些本地视频来制作更长的视频并保存到相机胶卷。我使用SCRecord lib来实现它。首先,我让每个SCRecordSegment每个本地视频:SCRecorder:如何从本地视频创建SCRecordSession

filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,[NSString stringWithFormat:@"%@.mp4",videoTitle]]; 
     // Create 
     SCRecordSessionSegment *record = [[SCRecordSessionSegment alloc] initWithURL:[NSURL URLWithString:filePath] info:nil]; 

然后创建RecordSession:

SCRecordSession *recordSession = [SCRecordSession recordSession]; 
[recordSession addSegment:record]; 

但它崩溃了,当我保存到相机胶卷。

- (void)saveToCameraRoll : (SCRecordSession *)recordSession{ 
self.navigationItem.rightBarButtonItem.enabled = NO; 


SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:recordSession.assetRepresentingSegments]; 
exportSession.videoConfiguration.filter = nil; 
exportSession.videoConfiguration.preset = SCPresetHighestQuality; 
exportSession.audioConfiguration.preset = SCPresetHighestQuality; 
exportSession.videoConfiguration.maxFrameRate = 35; 
exportSession.outputUrl = recordSession.outputUrl; 

exportSession.outputFileType = AVFileTypeMPEG4; 
exportSession.delegate = self; 
exportSession.contextType = SCContextTypeAuto; 



NSLog(@"Starting exporting"); 

CFTimeInterval time = CACurrentMediaTime(); 
__weak typeof(self) wSelf = self; 
[exportSession exportAsynchronouslyWithCompletionHandler:^{ 
    __strong typeof(self) strongSelf = wSelf; 

    if (!exportSession.cancelled) { 
     NSLog(@"Completed compression in %fs", CACurrentMediaTime() - time); 
    } 


    NSError *error = exportSession.error; 
    if (exportSession.cancelled) { 
     NSLog(@"Export was cancelled"); 
    } else if (error == nil) { 
     [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
     [exportSession.outputUrl saveToCameraRollWithCompletion:^(NSString * _Nullable path, NSError * _Nullable error) { 
      [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

      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]; 
      } 
     }]; 
    } else { 
     if (!exportSession.cancelled) { 
      [[[UIAlertView alloc] initWithTitle:@"Failed to save" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
     } 
    } 
}]; 

}

你是不是第一个合并的视频剪辑。以下是我的代码样本

- (IBAction)saveToCameraRoll:(id)sender { 
    [[self recorder] pause:^{ 
     [[HPRecordSessionManager sharedInstance] saveRecordSession:[[self recorder] session]]; 
     [self setRecordSession:[[self recorder] session]]; 

     [[self saveIndicator] startAnimating]; 
     [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

     void(^completionHandler)(NSURL *url, NSError *error) = ^(NSURL *url, NSError *error) { 

      if (error == nil) { 

       UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil); 
      } else { 

       [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
       [UIViewController displayErrorAlertWithTitle:kClipSaveToCameraRallFailedTitle message:[error localizedDescription] cancelButtonText:kClipSaveToCameraRallFailedDissmissButton]; 
       [[self saveIndicator] stopAnimating]; 
      } 
     }; 

     [[self recordSession] mergeSegmentsUsingPreset:AVAssetExportPresetHighestQuality completionHandler:completionHandler]; 
    }]; 
} 

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo { 
    [[self saveIndicator] stopAnimating]; 
    [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

    if (error == nil) { 
     [UIViewController displayErrorAlertWithTitle:kClipSaveToCameraRallSuccessTitle message:kClipSaveToCameraRallSuccessMessage cancelButtonText:kClipSaveToCameraRallSuccessDissmissButton]; 
    } else { 
     [UIViewController displayErrorAlertWithTitle:kClipSaveToCameraRallFailedTitle message:[error localizedDescription] cancelButtonText:kClipSaveToCameraRallFailedDissmissButton]; 
    } 

}