视频方向肖像模式不起作用

问题描述:

我试图合并一个音频和视频。一切都很好。但视频导出为横向模式。但我希望它能用于肖像模式。视频方向肖像模式不起作用

我知道这个问题已经在StackOverflow中得到了解答。但这些答案是 不适合我。谁能帮帮我吗。我需要它迅速3 版本。

这就是答案列表我跟随但不工作 我

First

second

3rd

4th

5th

6th

注:请不要投票复制它。我知道这个问题对我来说已经有了,但不是正确的答案。 请帮助我。我在这里呆了一整天。

这里是我尝试实施

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) 

     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 videoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack) 
     videoCompositionLayerInstruction.setTransform(videoAssetTrack.preferredTransform, at: kCMTimeZero) 
     let videoCompositionInstuction = AVMutableVideoCompositionInstruction() 
     videoCompositionInstuction.timeRange = CMTimeRangeMake(kCMTimeZero, mainComposition.duration) 
     videoCompositionInstuction.layerInstructions = [ videoCompositionLayerInstruction ] 



     var renderSize = videoAssetTrack.naturalSize 
     renderSize = renderSize.applying(videoAssetTrack.preferredTransform) 
     renderSize = CGSize(width: fabs(renderSize.width), height: fabs(renderSize.height)) 

     let videoComposition = AVMutableVideoComposition() 
     videoComposition.renderSize = renderSize 
     videoComposition.renderSize = CGSize(width: 1280, height: 720) 
     videoComposition.frameDuration = CMTimeMake(1, 30) 
     videoComposition.instructions = [ videoCompositionInstuction ] 




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

只是删除以下从您的代码行。

videoComposition.renderSize = CGSize(width: 1280, height: 720) 
+0

只是删除。仍然没有工作.. –

+0

这很有趣。在我结束时删除了该行后,它按预期工作。 –