Objective-c 如何向现有视频(如 Instagram 应用程序)添加过滤器?

Objective-c how to add filter to an existing video like Instagram Application?

我正在尝试在录制视频后为视频添加滤镜,就像 Instagram 一样。在搜索了一种方法后,我找到了 GPUImage,但这并没有解决它,因为在这段代码中,视频在显示之前被写入目录(导致延迟):

//Setting path for temporary storing the video in document directory
NSURL *movieURL = [self dataFilePath: @"tempVideo.mp4"]; // url where we want to save our new edited video

有没有办法在保存之前预览过滤后的视频,以免浪费时间?如果有,是怎么做到的?

此外,我在 Apple 文档中找到了有关 CIFilter 的内容,但仍然找不到说明如何向视频添加滤镜的方法。

此外,还有一些代码是用swift写的。

提前致谢。

我找不到停止 GPUImage 出现的延迟的方法。所以我尝试使用 SCRecorder.

我所做的是:

[_player setItemByUrl:videoURL];

而不是:

[_player setItemByAsset:_recordSession.assetRepresentingSegments];

通过这种方式,我可以像 Instagram 一样播放现有视频并为其添加滤镜。

要使用所选滤镜导出视频,我使用了以下代码:

 - (void)saveToCameraRoll {

  NSString *fileName = videoURL.lastPathComponent;
  NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *docsDir = [dirPaths objectAtIndex:0];
  NSString *videoPath = [NSString stringWithFormat:@"%@/%@",docsDir,fileName];
  NSURL *urlPath = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", videoPath]];

  NSURL *assetUrl = urlPath;
  AVAsset *asset = [AVAsset assetWithURL:assetUrl];
  SCFilter *exportFilter = [self.filterSwitcherView.selectedFilter copy];

  SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:asset];
  NSURL *urlFile = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@.mov",docsDir,fileName]];
  exportSession.outputUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",urlFile]];

  filterURL = exportSession.outputUrl;

  exportSession.videoConfiguration.filter = exportFilter;
  exportSession.videoConfiguration.preset = SCPresetHighestQuality;
  exportSession.audioConfiguration.preset = SCPresetHighestQuality;
  exportSession.videoConfiguration.maxFrameRate = 35;
  exportSession.outputFileType = AVFileTypeMPEG4;
  exportSession.delegate = self;
  exportSession.contextType = SCContextTypeAuto;
  self.exportSession = exportSession;

  [exportSession exportAsynchronouslyWithCompletionHandler:^{
      if (exportSession.error == nil) {
          [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
          [exportSession.outputUrl saveToCameraRollWithCompletion:^(NSString * _Nullable path, NSError * _Nullable error) {
              [[UIApplication sharedApplication] endIgnoringInteractionEvents];

              if (error == nil) {
                //Success
              }
          }];

      } else {
          NSLog(@"Error: %@", exportSession.error);
      }
  }];
}