iOS-单个视频进行滤镜处理

视频预览层
self.preview = [[GPUImageView alloc] initWithFrame:self.view.bounds];
self.preview.layer.contentsScale = 2.0;
[self.preview setBackgroundColorRed:0.0 green:0.0 blue:0.0 alpha:1.0];
// self.preview.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
[self.view addSubview:self.preview];

GPUImageViewMove预览播放的视频没有声音 如果需要播放声音 使用AVPlayer进行处理

  • (AVPlayerItem *)playerItem
    {
    if (!_videoPlayerItem)
    {
    _videoPlayerItem = [[AVPlayerItem alloc]initWithURL:self.videoUrl];
    [_videoPlayerItem addObserver:self forKeyPath:@"status" options:0 context:(__bridge void * _Nullable)VideoPlayerItemStatusContext];

    }
    return _videoPlayerItem;
    }

  • (AVPlayer *)videoPlayer
    {
    if (!_videoPlayer)
    {
    _videoPlayer =[[AVPlayer alloc]initWithPlayerItem:self.playerItem];

      @weakify(self);
      [_videoPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, self.videoPlayerItem.asset.duration.timescale) queue:nil usingBlock:^(CMTime time) {
          @strongify(self);
          
          if (self.videoCMTimeCallBack) {
              self.videoCMTimeCallBack(self.videoPlayerItem.currentTime, self.videoPlayerItem.asset.duration);
          }
          
          if (CMTimeGetSeconds(time) >= CMTimeGetSeconds(self.videoPlayerItem.asset.duration))
          {
              if (self.videoPlayEndCallBack && self.isVideoPlayEndCallBackNowBool == NO)
              {
                  self.playIng = NO;
                  self.videoPlayEndCallBack();
                  self.isVideoPlayEndCallBackNowBool = YES;
                  @weakify(self);
                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                      @strongify(self);
                      self.isVideoPlayEndCallBackNowBool = NO;
                  });
              }
          }else
          {
              //                NSLog(@"播放中");
          }
      }];
    

    }
    return _videoPlayer;
    }

  • (GPUImageMovie *)movie
    {
    if (!_movie)
    {
    _movie =[[GPUImageMovie alloc]initWithPlayerItem:self.playerItem];
    _movie.runBenchmark =NO;
    _movie.playAtActualSpeed =YES;
    }
    return _movie;
    }

manger处理

//开始进行滤镜渲染

  • (void)saveEcodingVideoActionWithPlayURL:(NSURL *)url lutImage:(UIImage *)lutImage callback:(void(^)(NSURL *backURL))callBack {

    // 要转换的视频
    // NSURL * pathUrl = [[NSBundle mainBundle] URLForResource:@"sample_iPod" withExtension:@"m4v"];
    //
    _movieFile = [[GPUImageMovie alloc] initWithURL:url];
    _movieFile.runBenchmark = YES;
    _movieFile.playAtActualSpeed = NO;

    // 创建滤镜
    // _filter = [[GPUImagePixellateFilter alloc] init];
    self.filter = [[GLImageLutFilter alloc]init];
    [self.filter setLutImage:lutImage ? :[UIImage imageNamed:@"filter_xiatian"]];
    [_movieFile addTarget:self.filter];

// 设置输出路径
NSString * pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
// - 如果文件已存在,AVAssetWriter不允许直接写进新的帧,所以会删掉老的视频文件
unlink([pathToMovie UTF8String]);
self.movieURL = [NSURL fileURLWithPath:pathToMovie];

// 输出 后面的size可改 ~ 现在来说480*640有点太差劲了
AVAsset *asset = [AVAsset assetWithURL:url];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
//    CGAffineTransform t = videoTrack.preferredTransform;//这里的矩阵有旋转角度,转换一下即可

NSLog(@"=====hello  width:%f===height:%f",videoTrack.naturalSize.width,videoTrack.naturalSize.height);//宽高

_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:self.movieURL size:videoTrack.naturalSize];

[self.filter addTarget:_movieWriter];

_movieWriter.shouldPassthroughAudio = YES;
_movieFile.audioEncodingTarget = _movieWriter;
[_movieFile enableSynchronizedEncodingUsingMovieWriter:_movieWriter];

// 完成之后的回调 - 为啥100%了之后还会待一会才调用这个completeBlock
__weak typeof(self) weakself = self;
[_movieWriter setCompletionBlock:^{
    __strong typeof (weakself) strongSelf = weakself;
    [strongSelf.filter removeTarget:strongSelf.movieWriter];
    [strongSelf.movieWriter finishRecording];
    if (callBack) {
        callBack(strongSelf.movieURL);
    }

// // 异步写入相册
// dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// dispatch_async(concurrentQueue, ^{
// [strongSelf saveVideo:strongSelf.movieURL];
// });

}];
[self.movieWriter startRecording];
[_movieFile startProcessing];

}

//videoPath为视频下载到本地之后的本地路径

  • (void)saveVideo:(NSURL *)videoPath{
    NSString *urllStr = videoPath.path;

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urllStr)) {
    //保存相册核心代码
    UISaveVideoAtPathToSavedPhotosAlbum(urllStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    }

}

  • (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    if (error) {
    NSLog(@"保存视频失败%@", error.localizedDescription);
    }
    else {
    NSLog(@"保存视频成功");

    }

}

你可能感兴趣的:(iOS-单个视频进行滤镜处理)