iosswift4video-processinggpuimageavasset

How to detect selected Media is Time-Lapse, Slow-Motion or .mp4


Creating an application for image and video processing, where app requirement is set device Orientation according to selected video content orientation from the gallery. So I have used some line of code to get selected media current orientation kindly check Below code for orientation get from selected content.

var video_orientation: UIInterfaceOrientation {
    guard let transform = tracks(withMediaType: AVMediaType.video).first?.preferredTransform else {
        return .portrait
    }
    switch (transform.tx, transform.ty) {
    case (0, 0):
        return .landscapeLeft
    case (videoFirstFrameSize.width, videoFirstFrameSize.height):
        return .landscapeRight
    case (0, videoFirstFrameSize.width):
        return .portraitUpsideDown
    default:
        return .portrait
    }
}

Now, the problem is when user select video (slow-Mo, Normal) from Gallery its working fine. But when user select Time-Lapse video from gallery orientation going to changed, I have done too many research related to get orientation also have create many experiment and then finally I got some solution, and the solution is if we selected Time-lapse we need to pass AVMediaType.timecode in video type but now the problem is, how should I set it on selected assets. Any help is really appreciable and will save my life :P but its going little frustrating.

NOTE:- var video_orientation Is the Extension property of AVAssets


Solution

  • Here is the code how you can detect media type easily.

    enum AssetType {
        case photo
        case video
        case livePhoto
        case sloMo
        case timeLapse
        case burst
        case gif
    }
    
    func getMediaType(asset : PHAsset)-> AssetType? {
            if asset.mediaType == .image {
                if asset.representsBurst {
                    return .burst
                } else if asset.playbackStyle == .imageAnimated {
                    return .gif
                } else if (asset.mediaType == PHAssetMediaType.image && ((asset.mediaSubtypes.rawValue & PHAssetMediaSubtype.photoLive.rawValue) != 0)) {
                    return .livePhoto
                } else {
                    return .photo
                }
            } else if asset.mediaType == .video {
                if (asset.mediaType == PHAssetMediaType.video && ((asset.mediaSubtypes.rawValue & PHAssetMediaSubtype.videoTimelapse.rawValue) != 0)) {
                    return .timeLapse
                } else if (asset.mediaType == PHAssetMediaType.video && ((asset.mediaSubtypes.rawValue & PHAssetMediaSubtype.videoHighFrameRate.rawValue) != 0)) {
                    return .sloMo
                } else {
                    return .video
                }
            } else {
                return nil
            }
        }