在iPhone照片中,视频无法以完美的方向保存

时间:2017-09-17 13:49:22

标签: ios swift avassetexportsession

我试图合并一个音频和视频。它完美融合。但是当我将它保存到iPhone照片库时。它显示的视频显示如下图所示:

enter image description here

但我希望视频会显示如下:

enter image description here

我正在学习一些教程。这是我跟随的one

这是我在合并中所做的事情

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 个答案:

没有答案