合并两个录制的视频会产生相反的方向输出

时间:2015-06-04 10:35:28

标签: ios swift avfoundation video-processing

我正在尝试使用此代码合并两个录制的视频:

func exportVideo3() {

    transprentView.hidden = false
    activityIndicator.startAnimating()
    let composition = AVMutableComposition()
    let trackVideo = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
    let trackAudio = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
    var insertTime = kCMTimeZero
    let path = NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil)!.path!
    for index in 1...2 {
        let movieUrl = index == 1 ? video1URL : video2URL
        let sourceAsset = AVURLAsset(URL: movieUrl, options: nil)

        let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
        let audios = sourceAsset.tracksWithMediaType(AVMediaTypeAudio)

        if tracks.count > 0{
            let assetTrack:AVAssetTrack = tracks[0] as! AVAssetTrack
            trackVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrack, atTime: insertTime, error: nil)
            let assetTrackAudio:AVAssetTrack = audios[0] as! AVAssetTrack
            trackAudio.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrackAudio, atTime: insertTime, error: nil)
            insertTime = CMTimeAdd(insertTime, sourceAsset.duration)
        }
    }

    let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
    exporter.outputURL = documentsDirectoryURL.URLByAppendingPathComponent("mergedMovie.mp4")
    if NSFileManager.defaultManager().fileExistsAtPath(exporter.outputURL.path!) {
        NSFileManager.defaultManager().removeItemAtURL(exporter.outputURL, error: nil)
    }

    mergedVideoURL = exporter.outputURL
    exporter.outputFileType = AVFileTypeMPEG4
    exporter.exportAsynchronouslyWithCompletionHandler({
        switch exporter.status{
        case  AVAssetExportSessionStatus.Failed:
            println("failed \(exporter.error)")
        case AVAssetExportSessionStatus.Cancelled:
            println("cancelled \(exporter.error)")
        default:
            self.transprentView.hidden = true
            self.activityIndicator.stopAnimating()
            println("complete")
        }
    })
}

工作正常,但新视频的方向相反。即当我以纵向方式同时录制视频并合并时,新视频将以横向模式播放,当我以横向方式录制并合并时,新视频将以纵向模式播放。

那么如何才能解决这个问题,就像我在横向模式下录制视频一样,我希望以横向模式输出并与肖像相同。

HERE是我的示例项目,供更多参考。

0 个答案:

没有答案