合并视频片段后的视频拉伸

时间:2019-01-28 10:15:45

标签: swift video merge avfoundation

我遇到有关使用avfoundation合并视频剪辑的问题。实际上,当我使用相机(录制)制作视频剪辑时,以及合并这些剪辑后,它看起来很棒,工作正常,但是当我从图库中导入任何视频,然后将此导入的剪辑与录制的剪辑合并时。整个合并后的视频都拉长了。任何帮助,将不胜感激。谢谢。

func merge(arrayVideos:[AVAsset], completion:@escaping (_ exporter: AVAssetExportSession) -> ()) -> Void {


    let mainComposition = AVMutableComposition()
    let compositionVideoTrack = mainComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)



    var time:Double = 0.0
    for (index, videoAsset) in arrayVideos.enumerated() {



        let atTime = CMTime(seconds: time, preferredTimescale: 1000)

        try! compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .video)[0], at: atTime)

        compositionVideoTrack!.preferredTransform = (videoAsset.preferredTransform)




        time = time + videoAsset.duration.seconds
    }

    let outputFileURL = URL(fileURLWithPath: NSTemporaryDirectory() + "merge.mp4")
    print("final URL:\(outputFileURL)")
    let fileManager = FileManager()

    do {
        try fileManager.removeItem(at: outputFileURL)
    } catch let error as NSError {
        print("Error: \(error.domain)")
    }

    let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)

    exporter?.outputURL = outputFileURL
    exporter?.outputFileType = AVFileType.mp4
    exporter?.shouldOptimizeForNetworkUse = true

    exporter?.exportAsynchronously {
        DispatchQueue.main.async {
            completion(exporter!)
        }
    }

}

0 个答案:

没有答案