Search code examples
iosgpuimage

GPUImageMovie not respecting imageOrientation


I am using GPUImageMovie which is playing a movie file, this file was recorded on a iOS device.

However when GPUImageMovie plays it it is in the wrong orientation so the video isn't rotated to be shown correctly.

How do I get it to respect it's orientation ? I've tried modifying the OpenGL code with no luck.


Solution

  • I had same problem when playing a video recorded in iOS device using GPUImageMovie. I solved it using following functions :

    Call this setRotationForFilter: method by passing you filter. The orientationForTrack: will return your current video orientation.

    - (void)setRotationForFilter:(GPUImageOutput<GPUImageInput> *)filterRef {
        UIInterfaceOrientation orientation = [self orientationForTrack:self.playerItem.asset];
        if (orientation == UIInterfaceOrientationPortrait) {
            [filterRef setInputRotation:kGPUImageRotateRight atIndex:0];
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            [filterRef setInputRotation:kGPUImageRotate180 atIndex:0];
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            [filterRef setInputRotation:kGPUImageRotateLeft atIndex:0];
        }
    }
    
    - (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset
    {
        AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        CGSize size = [videoTrack naturalSize];
        CGAffineTransform txf = [videoTrack preferredTransform];
    
        if (size.width == txf.tx && size.height == txf.ty)
            return UIInterfaceOrientationLandscapeRight;
        else if (txf.tx == 0 && txf.ty == 0)
            return UIInterfaceOrientationLandscapeLeft;
        else if (txf.tx == 0 && txf.ty == size.width)
            return UIInterfaceOrientationPortraitUpsideDown;
        else
            return UIInterfaceOrientationPortrait;
    }
    

    Hope this solves your issue.