BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.22k stars 4.62k forks source link

Text(GPUImageUIElement) missing from recorded video. #2484

Open uditbatra opened 7 years ago

uditbatra commented 7 years ago

Hi,

My requirement is to record the video with some text. I have used code from sample "SimpleVideoFilter" to add GPUImageUIElement to video. While previewing/recording a video, I can see text on the video. But when it is saved to the Photo Library the text is missing and Video with a filter (Sepia) is saved.

My Code below -

- (void)viewDidLoad
{
    [super viewDidLoad];

    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];

    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    videoCamera.horizontallyMirrorFrontFacingCamera = NO;
    videoCamera.horizontallyMirrorRearFacingCamera = NO;

    filter = [[GPUImageSepiaFilter alloc] init];

    [videoCamera addTarget:filter];
    GPUImageView *filterView = (GPUImageView *)self.view;

    ////////////////Code Start to add text///////////////////
    GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
    blendFilter.mix = 1.0;

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 240.0f, 320.0f)];
    timeLabel.font = [UIFont systemFontOfSize:17.0f];
    timeLabel.text = @"Hello, My text here.";
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor whiteColor];

    uiElementInput = [[GPUImageUIElement alloc] initWithView:timeLabel];

    [filter addTarget:blendFilter];
    [uiElementInput addTarget:blendFilter];

    [blendFilter addTarget:filterView];

    __unsafe_unretained GPUImageUIElement *weakUIElementInput = uiElementInput;

    [filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
        [weakUIElementInput update];
    }];
    ///////////////Code End adding text////////////////////////////

    // Record a movie for 10 s and store it in /Documents, visible via iTunes file sharing

    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(480.0, 640.0)];
    movieWriter.encodingLiveVideo = YES;

    [filter addTarget:movieWriter];
    [filter addTarget:filterView];

    [videoCamera startCameraCapture];

    double delayToStartRecording = 0.5;
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, delayToStartRecording * NSEC_PER_SEC);
    dispatch_after(startTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Start recording");

        videoCamera.audioEncodingTarget = movieWriter;
        [movieWriter startRecording];

        double delayInSeconds = 10.0;
        dispatch_time_t stopTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(stopTime, dispatch_get_main_queue(), ^(void){

            [filter removeTarget:movieWriter];
            videoCamera.audioEncodingTarget = nil;
            [movieWriter finishRecording];
            NSLog(@"Movie completed");

            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:movieURL])
            {
                [library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error)
                 {
                     dispatch_async(dispatch_get_main_queue(), ^{

                         if (error) {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"
                                                                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                             [alert show];
                         } else {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"
                                                                            delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                             [alert show];
                         }
                     });
                 }];
            }
        });
    });
}

Thank you in advance for your help. Also the screen is flickering after adding code to add text.

uditbatra commented 7 years ago

@BradLarson Please help... Thank you in advance for your help.

amoghesturi commented 7 years ago

@uditbatra - We are facing the similar issue on iPhone 7 and iPad pro. We are trying to draw shapes on top of images. Shapes are visible initially but in the final image, the shape is not visible.

Would be really helpful if you could share your findings.

ivan-ushakov commented 6 years ago

Maybe this is still actual for somebody. You have routing error. Replace this code:

[_filter addTarget:_movieWriter];
[_filter addTarget:filterView];

With this:

[blendFilter addTarget:_movieWriter];

With this fix it works fine and video clip has text.

anonym24 commented 6 years ago

any example how to add text in swift? (GPUImage2)

https://github.com/BradLarson/GPUImage2