14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-Cで動画の向き(orientation)を取得する

Posted at

チャオ!

動画の向きが取得できなとアレやコレやが出来なくて、アレゲな時があるよね!

ウブだった頃の私は

ダメパターン

+ (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;
    }
}

とかやっていたんだけど、これじゃー取れない動画ががががが・・・
でも下記のやり方でやったら取れないヤツも無事とれたからこっちがオススメ!

オススメパターン

static inline CGFloat RadiansToDegrees(CGFloat radians) {
    return radians * 180 / M_PI;
};

+ (UIInterfaceOrientation)orientationForTrackWithAsset:(AVAsset *)asset
{
    AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    CGAffineTransform txf = [videoTrack preferredTransform];
    CGFloat videoAngleInDegree  = RadiansToDegrees(atan2(txf.b, txf.a));
    
    switch ((int)videoAngleInDegree) {
        case 0:
            return UIInterfaceOrientationLandscapeRight;
        case 90:
            return UIInterfaceOrientationPortrait;
        case 180:
            return UIInterfaceOrientationLandscapeLeft;
        case -90:
            return UIInterfaceOrientationPortraitUpsideDown;
        default:
            return UIInterfaceOrientationUnknown;
    }
}

動画の横幅がおかしいとかあったら試してみて!

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?