How to detect if a video file was recorded in portrait orientation, or landscape in iOS

后端 未结 8 1791
孤城傲影
孤城傲影 2020-11-27 02:36

I am using AlAssetsGroup enumerateAssetsAtIndexes to list the assets in the Photos (Camera) app. For a given video asset I want to determine whether it was shot

8条回答
  •  孤街浪徒
    2020-11-27 03:35

    In my use case I only needed to know if a video is in portrait or not (landscape).

    guard let videoTrack = AVAsset(url: videoURL).tracks(withMediaType: AVMediaTypeVideo).first else {
        return ...
    }
    
    let transformedVideoSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
    let videoIsPortrait = abs(transformedVideoSize.width) < abs(transformedVideoSize.height)
    

    This has been tested with both front and rear cameras for all orientation possibilities.

提交回复
热议问题