首页 > 解决方案 > 合并后视频旋转问题

问题描述

我正在使用 AVFoundation 框架合并视频。合并后一些视频正在改变位置并旋转(横向到纵向,反之亦然)。如何解决这个问题。

我的代码:

  func mergeVideos(arrayVideos:[AVAsset])
   {
    var atTimeM: CMTime = CMTimeMake(0, 0)
    var layerInstructionsArray = [AVVideoCompositionLayerInstruction]()
    var completeTrackDuration: CMTime = CMTimeMake(0, 1)
    var videoSize: CGSize = CGSize(width: 0.0, height: 0.0)
    var totalTime : CMTime = CMTimeMake(0, 0)

    let mixComposition = AVMutableComposition.init()
    for videoAsset in arrayVideos{

        let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
        do {
            if videoAsset == arrayVideos.first {
                atTimeM = kCMTimeZero
            } else {
                atTimeM = totalTime // <-- Use the total time for all the videos seen so far.
            }
            try videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration),
                                            of: videoAsset.tracks(withMediaType: AVMediaType.video)[0],
                                            at: completeTrackDuration)
            videoSize = (videoTrack?.naturalSize)!

        } catch let error as NSError {
            print("error: \(error)")
        }
        totalTime = CMTimeAdd(totalTime, videoAsset.duration)  
    let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)

标签: iosobjective-cswiftavfoundation

解决方案


推荐阅读