0

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")
    }
}

}

Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
Faizul Karim
  • 333
  • 2
  • 13
  • why it's duplicate ?? i am unable to find this answer in swift – Faizul Karim Sep 17 '17 at 16:13
  • You will find answer in these links. can't give answer here... 1) https://stackoverflow.com/questions/34418005/how-to-fix-my-orientation-issue-with-merging-videos-from-front-and-back-camera 2) https://stackoverflow.com/questions/36663942/fixing-orientation-when-stitching-merging-videos-using-avmutablecomposition – S.M.Moinuddin. Shuvo Sep 21 '17 at 10:08
  • This two link didn't work for me – Faizul Karim Sep 21 '17 at 17:39

0 Answers0