视频方向纵向模式不起作用

时间:2017-09-22 07:22:19

标签: ios swift avasset

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

  

我知道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")
            }
        }

1 个答案:

答案 0 :(得分:0)

只需从代码中删除以下行。

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