ios视频、图片翻转问题

项目中有做视频导入的功能,有些经过软件处理后的视频在判断视频方向上会出现一些问题。
这个特殊视频的 CGAffineTransform为 {a = 0 , b = 1.0 , c =1.0 , d =0 }

+ (AVAssetTrack *)videoTrackWithAsset:(AVAsset *)asset
{
    return [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
}

判断视频的方向
方法一:

+ (UIImageOrientation)orientationWithAsset:(AVAsset *)asset
{
   AVAssetTrack *videoTrack = [self videoTrackWithAsset:asset];
    CGSize size = [videoTrack naturalSize];
  CGAffineTransform txf = [videoTrack preferredTransform];
      if (size.width == txf.tx && size.height == txf.ty)
           return UIImageOrientationLeft;  // return UIInterfaceOrientationLandscapeLeft;
     else if (txf.tx == 0 && txf.ty == 0)
          return UIImageOrientationRight;  // return UIInterfaceOrientationLandscapeRight;
     else if (txf.tx == 0 && txf.ty == size.width)
         return UIImageOrientationDown;  // return UIInterfaceOrientationPortraitUpsideDown;
     else
        return UIImageOrientationUp;  // return UIInterfaceOrientationPortrait;
}

方法二:

+ (UIImageOrientation)orientationWithAsset:(AVAsset *)asset
{

    AVAssetTrack *videoTrack = [self videoTrackWithAsset:asset];
        CGAffineTransform t = videoTrack.preferredTransform;

        if (t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0)
        {
            // Portrait
            return UIImageOrientationUp ;
        }
        else if (t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)
        {
            // PortraitUpsideDown
            return UIImageOrientationDown;
        }
        else if (t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
        {
            // LandscapeRight
           return UIImageOrientationRight;
        }
        else
        {
            // LandscapeLeft
            return UIImageOrientationLeft;
        }
}

该视频实际的方向是正方形的,但是经过别的软件处理后 CGAffineTransform 的值被修改了,通过上面的两个方法得出的偏转方向都不正确,因此该视频在后面做视频翻转的时候转向并不正确。
最后找到一个新的解决方法,记录在下面:

typedef enum {
    LBVideoOrientationUp,               //Device starts recording in Portrait
    LBVideoOrientationDown,             //Device starts recording in Portrait upside down
    LBVideoOrientationLeft,             //Device Landscape Left  (home button on the left side)
    LBVideoOrientationRight,            //Device Landscape Right (home button on the Right side)
    LBVideoOrientationNotFound = 99     //An Error occurred or AVAsset doesn't contains video track
} LBVideoOrientation;
static inline CGFloat RadiansToDegrees(CGFloat radians) {
    return radians * 180 / M_PI;
};

判断视频方向,会根据视频第一帧的 CGAffineTransform 的 b / a 的反正切值,然后再算出视频偏转角度

+ (LBVideoOrientation)videoOrientationWithAsset:(AVAsset *)asset
{
    NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
    if ([videoTracks count] == 0) {
        return LBVideoOrientationNotFound;
    }

    AVAssetTrack* videoTrack    = [videoTracks objectAtIndex:0];
    CGAffineTransform txf       = [videoTrack preferredTransform];
    CGFloat videoAngleInDegree  = RadiansToDegrees(atan2(txf.b, txf.a));

    LBVideoOrientation orientation = 0;
    switch ((int)videoAngleInDegree) {
        case 0:
            orientation = LBVideoOrientationRight;
            break;
        case 90:
            orientation = LBVideoOrientationUp;
            break;
        case 180:
            orientation = LBVideoOrientationLeft;
            break;
        case -90:
            orientation     = LBVideoOrientationDown;
            break;
        default:
            orientation = LBVideoOrientationNotFound;
            break;
    }

    return orientation;
}

最后根据视频的角度旋转视频

 LBVideoOrientation  videoOrientation = [self videoOrientationWithAsset:asset];

    CGAffineTransform t1 = CGAffineTransformIdentity;
    CGAffineTransform t2 = CGAffineTransformIdentity;

    NSLog(@" --- 视频转向 -- %ld",(long)videoOrientation);
    switch (videoOrientation)
    {
            case LBVideoOrientationUp:
            t1 = CGAffineTransformMakeTranslation(videoTrack.naturalSize.height - cropOffX, 0 - cropOffY);
            t2 = CGAffineTransformRotate(t1, M_PI_2);
            break;
            case LBVideoOrientationDown:
            t1 = CGAffineTransformMakeTranslation(
                0 - cropOffX,
                videoTrack.naturalSize.width - cropOffY);  // not fixed width is the real height in upside down
            t2 = CGAffineTransformRotate(t1, -M_PI_2);
            break;
            case  LBVideoOrientationRight:
            t1 = CGAffineTransformMakeTranslation(0 - cropOffX, 0 - cropOffY);
            t2 = CGAffineTransformRotate(t1, 0);
            break;
        case LBVideoOrientationLeft:
            t1 = CGAffineTransformMakeTranslation(videoTrack.naturalSize.width - cropOffX,
                                                  videoTrack.naturalSize.height - cropOffY);
            t2 = CGAffineTransformRotate(t1, M_PI);
            break;
        default:
            NSLog(@"【该视频未发现设置支持的转向】");
            break;
    }

    CGAffineTransform finalTransform = t2;
    [transformer setTransform:finalTransform atTime:kCMTimeZero];

你可能感兴趣的:(ios视频、图片翻转问题)