Search code examples
ioscocoa-touchgpuimage

Add borders to a video using GPUImage


I'm trying to add borders of specified colors to a video to get something like this http://d.pr/i/1WXs

Here is what I'm doing

GPUImageNormalBlendFilter *blendFilter = [GPUImageNormalBlendFilter new];

_movie = [[GPUImageMovie alloc] initWithURL:_movieUrl];

_sourcePicture = [[GPUImagePicture alloc] initWithImage:[self imageWithColor:RGBACOLOR(255, 0, 0, 0.5f)]];
[_sourcePicture processImage];

[_sourcePicture addTarget:blendFilter atTextureLocation:0];
[_movie addTarget:blendFilter atTextureLocation:1];

[blendFilter addTarget:_videoView];

[_movie startProcessing];

As I understand, transform filter can't resize video to add a whitespace? How can I achieve this?

Thanks.


Solution

  • Solved using chain of Transform & Crop filter.

    [transformFilter setBackgroundColorRed:1 green:0 blue:0 alpha:0]; // For example
    
    // Calculate scale for exact crop size
    // For 16x9 scale = 16 * videoSize.height / (9 * videoSize.width);
    CGFloat scale = [self scaleForCropSize:self.cropSize];
    transformFilter.affineTransform = CGAffineTransformMakeScale(scale, scale);
    
    CGSize videoSize = [self.paramsContainer.videoTrack naturalSize];
    CGRect cropRegion;
    if (videoSize.width > videoSize.height) {
        cropRegion = CGRectMake((1 - scale) / 2, 0, scale, 1);
    } else {
        cropRegion = CGRectMake(0, (1 - scale) / 2, 1, scale);
    }
    
    cropFilter.cropRegion = cropRegion;