Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find the video orientation of an AVAsset. (Useful if you need to send the video to a remote server)
//
// AVAsset+VideoOrientation.h
//
// Created by Luca Bernardi on 19/09/12.
// Copyright (c) 2012 Luca Bernardi. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
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;
@interface AVAsset (VideoOrientation)
/**
Returns a LBVideoOrientation that is the orientation
of the iPhone / iPad whent starst recording
@return A LBVideoOrientation that is the orientation of the video
*/
@property (nonatomic, readonly) LBVideoOrientation videoOrientation;
@end
//
// AVAsset+VideoOrientation.m
//
// Created by Luca Bernardi on 19/09/12.
// Copyright (c) 2012 Luca Bernardi. All rights reserved.
//
#import "AVAsset+VideoOrientation.h"
static inline CGFloat RadiansToDegrees(CGFloat radians) {
return radians * 180 / M_PI;
};
@implementation AVAsset (VideoOrientation)
@dynamic videoOrientation;
- (LBVideoOrientation)videoOrientation
{
NSArray *videoTracks = [self 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;
}
@end
@Morkrom

This comment has been minimized.

Copy link

commented Jan 7, 2014

This is wonderful.

@danoli3

This comment has been minimized.

Copy link

commented Mar 28, 2014

I love you!

@andrei4002

This comment has been minimized.

Copy link

commented Mar 8, 2015

awesome, thank you!!!

@ivanlesko

This comment has been minimized.

Copy link

commented Sep 29, 2016

works like a charm

@stayHF

This comment has been minimized.

Copy link

commented Apr 28, 2017

very good!

@Sumit-2singh

This comment has been minimized.

Copy link

commented Dec 14, 2017

this is not working with Time-Lapse

@wvdk

This comment has been minimized.

@svanichkin

This comment has been minimized.

Copy link

commented Jan 24, 2019

not working for Time-Lapse and some vertical video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.