Imran
Imran

Reputation: 1715

How to Edit Brightness,Contrast and Saturation and add subtitles of already created video saved in library?

I am working on a video app in which I have to Adjust brightness,Contrast and saturation of already created video.Also I have to add subtitle like in movies.I have read a lot about it and came to know regarding videos that we can add brightness,contrast and saturation at the time of creating video but can not edit in a already created video.Also I have came to know how I can add text in video but I want it to come like subtitles at intervals when video plays like movies.

Using the GPUImage I changed brightness like this at the time of recording.

GPUImageFilter *selectedFilter = nil ;
selectedFilter = [[GPUImageBrightnessFilter alloc] init];
[(GPUImageBrightnessFilter*)selectedFilter setBrightness:brightnesSlider.value];

But I need to edit the video which is already made and saved in the gallery.Any Clue.

References:

Apple Edit Demo

RAY WENDERLICH

Brightness,Contrast and saturation

Upvotes: 1

Views: 2013

Answers (1)

Imran
Imran

Reputation: 1715

Here is the code which worked for me.I used GPUImage.

viewController.h

#import "GPUImage.h"

GPUImageMovie *movieFile;
GPUImageOutput<GPUImageInput> *filter;
GPUImageMovieWriter *movieWriter;

int ArrayIndex;
UISlider *mSlider;

ViewController.m

NSURL *sampleURL = [[NSBundle mainBundle] URLForResource:@"sample_iPod" withExtension:@"m4v"];

    mSlider=[[UISlider alloc]initWithFrame:CGRectMake(60,380,200, 30)];
    mSlider.continuous=YES;
    [mSlider addTarget:self action:@selector(updatePixelWidth:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mSlider];

    movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
    movieFile.runBenchmark = YES;
    movieFile.playAtActualSpeed = YES;

    if(ArrayIndex==0)
    {
         filter=[[GPUImageBrightnessFilter alloc]init];

        mSlider.maximumValue=1.0;
        mSlider.minimumValue=-1.0;
        mSlider.value=0.0;
    }
    else if(ArrayIndex==1)
    {
         filter=[[GPUImageContrastFilter alloc]init];

        mSlider.minimumValue=0.0;
        mSlider.maximumValue=4.0;
        mSlider.value=1.0;

    }
    else if(ArrayIndex==2)
    {
         filter=[[GPUImageSaturationFilter alloc]init];

        mSlider.minimumValue=0.0;
        mSlider.maximumValue=2.0;
        mSlider.value=1.0;

    }

    [movieFile addTarget:filter];

    // Only rotate the video for display, leave orientation the same for recording
    GPUImageView *filterView = (GPUImageView *)self.view;
    [filter addTarget:filterView];

    // In addition to displaying to the screen, write out a processed version of the movie to disk
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
    [filter addTarget:movieWriter];

    // Configure this for video from the movie file, where we want to preserve all video frames and audio samples
    movieWriter.shouldPassthroughAudio = YES;
    movieFile.audioEncodingTarget = movieWriter;
    [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

    [movieWriter startRecording];
    [movieFile startProcessing];

    [movieWriter setCompletionBlock:^{
        [filter removeTarget:movieWriter];
        [movieWriter finishRecording];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (pathToMovie)) {
            UISaveVideoAtPathToSavedPhotosAlbum (pathToMovie,self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        }

    }];



- (void)updatePixelWidth:(id)sender
{
    if(ArrayIndex==0)
    {
        [(GPUImageBrightnessFilter *)filter setBrightness:[(UISlider *)sender value]];
    }
    else if (ArrayIndex==1)
    {
          [(GPUImageContrastFilter *)filter setContrast:[(UISlider *)sender value]];
    }
    else if (ArrayIndex==2)
    {
        [(GPUImageSaturationFilter *)filter setSaturation:[(UISlider *)sender value]];
    }
}

Upvotes: 1

Related Questions