Load a movie > Filtering > Save to Camera Roll
How can I load a movie from Camera Roll, filtering it and save it to Camera Roll again?
I'm trying to do this, but I have some problem:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:@"public.movie"]){ NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; movieFile = [[GPUImageMovie alloc] initWithURL:videoURL]; sepiaFilter = [[GPUImageSepiaFilter alloc] init]; [movieFile addTarget:sepiaFilter]; //WHAT CAN I DO NEXT IF I'M USING ARC? } }
Thank you, Brad.
Thank you, Brad, great example! But I still have a question.
What can I use for saving a movie to camera roll: movieWriter or something else?
Once the movie has been recorded to disk, you can use code like the following to copy it into the user's media library:
UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, NULL, NULL);
Thanks, Brad. I'm using the following code, but it still not working:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:@"public.movie"]){ NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; movieFile = [[GPUImageMovie alloc] initWithURL:videoURL]; movieFile.runBenchmark = YES; filter = [[GPUImageColorInvertFilter alloc] init]; [movieFile addTarget:filter]; NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"]; unlink([pathToMovie UTF8String]); NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie]; movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)]; [filter addTarget:movieWriter]; movieWriter.shouldPassthroughAudio = YES; movieFile.audioEncodingTarget = movieWriter; [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter]; [movieWriter startRecording]; [movieFile startProcessing]; [movieWriter setCompletionBlock:^{ [filter removeTarget:movieWriter]; [movieWriter finishRecording]; }]; if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathToMovie)) //without it not working too { UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie, nil, NULL, NULL); NSLog(@"Saving"); } } }
Your problem is that you're trying to save the movie to the library right after it starts recording, rather than when the movie is done. You need to place the movie saving code within the completion block.
Thank you, Brad!
Look at the SimpleVideoFileFilter, it shows how to do the filtering to another file. From there, it's easy to write that movie into the user's media library.