Applying filters on a video file Applying filters on a video file ios ios

Applying filters on a video file


Use GPUImage framework only that you are using... that is best framework until now for video filters. GO through the documentation of the framework https://github.com/BradLarson/GPUImage scroll down the page you will find the details of the filters available...

this filters are applied on the video and to write the video you have to use the GPUImageMovieWriter class...It automatically handles audio ..

you don't have to maintain it...Use shouldPassThroughAudio property of GPUImageMovieWriter and it will manage the audio on its own.

Use this tutorial for help http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework

Here is the code where I am using GPUImage framework to crop the video and audio is stored not removed after editing.

NSURL *videoUrl = [selectedAsset defaultRepresentation].url;

GPUImageMovie *movieUrl = [[GPUImageMovie alloc] initWithURL:videoUrl];self.cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:videoArea];movieUrl.runBenchmark = YES;movieUrl.playAtActualSpeed = YES;[movieUrl addTarget:self.cropFilter];//Setting path for temporary storing the video in document directoryNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:                         [NSString stringWithFormat:@"CroppedVideo-%d.mov",arc4random() % 1000]];NSURL *movieURL = [NSURL fileURLWithPath:myPathDocs];AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(videoAssetTrack.naturalSize.width, videoAssetTrack.naturalSize.height)];[_cropFilter addTarget:movieWriter];movieWriter.shouldPassthroughAudio = YES;movieUrl.audioEncodingTarget = movieWriter;[movieUrl enableSynchronizedEncodingUsingMovieWriter:movieWriter];[self.movieWriter startRecordingInOrientation:videoTransform];[self.movieWriter startRecording];[movieUrl startProcessing];__block BOOL completeRec = NO;__unsafe_unretained typeof(self) weakSelf = self;[self.movieWriter setCompletionBlock:^{    [weakSelf.cropFilter removeTarget:weakSelf.movieWriter];    [weakSelf.movieWriter finishRecording];    [movieUrl removeTarget:weakSelf.cropFilter];    if (!completeRec)    {        [weakSelf videoCropDoneUrl:movieURL];        completeRec = YES;    }}];


Although solution 2 sounds like an "OK" solution if it was my app I'd still prefer using GPUImage Video filters.

Solution:

  1. Get the original video and tear the audio track out of it. (preferably in a background thread) with something like https://github.com/unixpickle/MP4Audio
  2. Let the user choose his video filters with GPUImage, process the video and save it.
  3. Once the video is ready, merge the new edited filtered video with the audio track you saved before GPUImage intervention. (Many ways to do the merge, here's one of them: Merging Audio with Video Objective-C)

Shana Tova