Ios 合并不同方向的视频

Ios 合并不同方向的视频,ios,swift,video,merge,Ios,Swift,Video,Merge,我想合并多个视频,并希望在最终视频中保持它们的实际方向。这是我的密码 func mergeSelectedVideos() { let composition = AVMutableComposition() let videoTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_

我想合并多个视频,并希望在最终视频中保持它们的实际方向。这是我的密码

    func mergeSelectedVideos() {
            let composition = AVMutableComposition()
            let videoTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
            let audioTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

            var time: Double = 0.0

            for video in self.arrayOfSelectedVideoURL {
                let sourceAsset = AVAsset(url: video as URL)
                let videoAssetTrack = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
                let audioAssetTrack = sourceAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
                let atTime = CMTime(seconds: time, preferredTimescale:1)            
                do{
                    try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, sourceAsset.duration) , of: videoAssetTrack, at: atTime)
                    try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, sourceAsset.duration) , of: audioAssetTrack, at: atTime)
                }catch{
                    print("-----Something wrong.")
                }
                time +=  sourceAsset.duration.seconds
            }

            let mergedVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/mergedVideo.mp4")
            let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
            exporter.outputFileType = AVFileTypeMPEG4
            exporter.outputURL = mergedVideoURL as URL
            removeFileAtURLIfExists(url: mergedVideoURL)
            exporter.exportAsynchronously(completionHandler:
                {
                    switch exporter.status
                    {
                        case AVAssetExportSessionStatus.failed:
                            print("failed \(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.cancelled:
                            print("cancelled \(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.unknown:
                            print("unknown\(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.waiting:
                            print("waiting\(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.exporting:
                            print("exporting\(String(describing: exporter.error))")
                        default:
                            print("-----Merged video exportation complete.\(self.mergedVideoURL)")
                    }
                })
}

有了这段代码,我可以合并多个视频,但肖像视频会在合并视频中旋转。是否有可能以某种方式将它们合并,其中纵向视频将保持纵向,横向视频将在最终视频中保持横向?我搜索了很多,但没有找到任何内容。

您需要对视频执行变换才能旋转它们。此外,你还需要决定如何呈现肖像视频——是侧面空白的完整视频,还是只呈现覆盖整个屏幕的部分视频。您需要执行的转换取决于此。谢谢。我想提出的肖像视频作为完整的视频与空白屏幕在其两侧。。。如何改变这一点?任何代码片段都会对@maxpevsner有很大帮助检查这个问题和答案。这应该会有帮助。我遵循了@dizy的答案……但对我来说不起作用。检查接受的答案。你需要对视频执行变换才能旋转它们。此外,你还需要决定如何呈现肖像视频——是侧面空白的完整视频,还是只呈现覆盖整个屏幕的部分视频。您需要执行的转换取决于此。谢谢。我想提出的肖像视频作为完整的视频与空白屏幕在其两侧。。。如何改变这一点?任何代码片段都会对@maxpevsner有很大帮助检查这个问题和答案。这应该会有帮助。我遵循了@dizy…的答案,但它对我不起作用。检查接受的答案。