video is not saving with perfect orientation in iPhone photos

65 Views Asked by At

i was trying to merge one audio and video. its merge perfectly. But when i save it to iPhone photo library. it's shows video show like this image:

enter image description here

but i want video will show like this:

enter image description here

i was following some tutorial. here is one that i follow.

here's what i have done in my merge

func megrtTestl(videoUrl:NSURL, audioUrl:NSURL){
let aVideoAsset : AVAsset = AVAsset(url: videoUrl as URL)
let aAudioAsset : AVAsset = AVAsset(url: audioUrl as URL)

let mainComposition = AVMutableComposition()

let videoTrack = mainComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
let videoAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeVideo).first!
try? videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAsset.duration), of: videoAssetTrack, at: kCMTimeZero)
videoTrack.preferredTransform = videoAssetTrack.preferredTransform // THIS LINE IS IMPORTANT

let audioTrack = mainComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
let audioAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio).first!
try? audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, aAudioAsset.duration), of: audioAssetTrack, at: kCMTimeZero)



let savePathUrl : NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/newVideo.mp4")

let assetExport = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)
assetExport?.outputURL = savePathUrl as URL
assetExport?.outputFileType = AVFileTypeQuickTimeMovie
assetExport?.shouldOptimizeForNetworkUse = true
assetExport?.exportAsynchronously { () -> Void in
    switch assetExport?.status {

    case AVAssetExportSessionStatus.completed?:

        //Uncomment this if u want to store your video in asset

        let assetsLib = ALAssetsLibrary()
        assetsLib.writeVideoAtPath(toSavedPhotosAlbum: savePathUrl as URL!, completionBlock: nil)

        print("success")
    case  AVAssetExportSessionStatus.failed?:
        print("failed \(String(describing: assetExport?.error))")
    case AVAssetExportSessionStatus.cancelled?:
        print("cancelled \(String(describing: assetExport?.error))")
    default:
        print("complete")
    }
}

}

0

There are 0 best solutions below