【问题标题】:Merging of two or more video in ios在 ios 中合并两个或多个视频
【发布时间】:2015-08-06 16:54:47
【问题描述】:

How to combine video clips with different orientation using AVFoundation

我已经接受了上述答案并且进展顺利。但我面临的问题是视频的音频被删除了。甚至我所有的视频都有声音。但是合并后导出的视频是静音的。任何人都可以帮忙。提前致谢。

【问题讨论】:

  • 你让音频工作了吗@Mayank?
  • 你得到解决方案了吗?
  • 是的,我解决了问题。

标签: ios video merge avvideocomposition


【解决方案1】:

我也面临同样的问题,但我得到了解决方案。 Swift 4.2 版本。

 // Merge All videos.
    func mergeAllVideos(completionHandler: @escaping(Bool)->Void){
        mixComposition = AVMutableComposition.init()

        // To capture video.
        let compositionVideoTrack = mixComposition?.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

        // To capture audio.
        let compositionAudioTrack = mixComposition?.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
        var nextCliptStartTime: CMTime = CMTime.zero

        // Iterate video array.
        for file_url in Constants.videoFileNameArr{
            // Do Merging here.
            let videoAsset = AVURLAsset.init(url: file_url)
            let timeRangeInAsset = CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration);
            do{
                // Merge video.
                try compositionVideoTrack?.insertTimeRange(CMTimeRange(start: CMTime.zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .video)[0], at: nextCliptStartTime)

                // Merge Audio
                try compositionAudioTrack?.insertTimeRange(CMTimeRange(start: CMTime.zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .audio)[0], at: nextCliptStartTime)
            }catch{
                print(error)
            }

            // Increment the time to which next clip add.
            nextCliptStartTime = CMTimeAdd(nextCliptStartTime, timeRangeInAsset.duration)
        }

        // Add rotation to make it portrait.
        let rotationTransform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
        compositionVideoTrack!.preferredTransform = rotationTransform

        // Save final file.
        self.saveFinalFile(mixComposition!){
            isDone in
            completionHandler(true)
        }
    }

【讨论】:

  • 谢谢,Ravi,但我需要目标 C 中的解决方案,我想通了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2016-05-22
  • 1970-01-01
相关资源
最近更新 更多