【问题标题】:Merge Videos Array in Swift在 Swift 中合并视频数组
【发布时间】:2018-03-15 10:52:06
【问题描述】:

我正在尝试合并来自 avsset 数组的视频,但我只能获得第一个和最后一个视频。阵列之间的视频显示黑色区域。 检查我正在使用的代码。

    func mergeVideoArray() {
    let mixComposition = AVMutableComposition()
    for videoAsset in videoURLArray {

        let videoTrack =
            mixComposition.addMutableTrack(withMediaType: AVMediaType.video,
                                           preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
        do {
            try videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration),
                                            of: videoAsset.tracks(withMediaType: AVMediaType.video).first!,
                                           at: totalTime)
            videoSize = (videoTrack?.naturalSize)!
        } catch let error as NSError {
            print("error: \(error)")
        }
        let trackArray = videoAsset.tracks(withMediaType: .audio)
        if trackArray.count > 0 {
            let audioTrack =
                mixComposition.addMutableTrack(withMediaType: AVMediaType.audio,
                                               preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
            do {
                try audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: AVMediaType.audio).first!, at: audioTime)
                audioTime = audioTime + videoAsset.duration
            }
            catch {

            }
        }
        totalTime = totalTime + videoAsset.duration
        let videoInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack!)
        if videoAsset != videoURLArray.last{
            videoInstruction.setOpacity(0.0, at: videoAsset.duration)
        }
        layerInstructionsArray.append(videoInstruction)
    }
    let mainInstruction = AVMutableVideoCompositionInstruction()

    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, totalTime)
    mainInstruction.layerInstructions = layerInstructionsArray

    let mainComposition = AVMutableVideoComposition()
    mainComposition.instructions = [mainInstruction]
    mainComposition.frameDuration = CMTimeMake(1, 30)
    mainComposition.renderSize = CGSize(width: videoSize.width, height: videoSize.height)

    let url = "merge_video".outputURL
    let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
    exporter!.outputURL = url
    exporter!.outputFileType = AVFileType.mov
    exporter!.shouldOptimizeForNetworkUse = false
    exporter!.videoComposition = mainComposition

    exporter!.exportAsynchronously {

        let video = AVAsset(url: url)
        let playerItem = AVPlayerItem(asset: video)
        let player = AVPlayer(playerItem: playerItem)
        self.playerViewController.player = player

        self.present(self.playerViewController, animated: true) {
            self.playerViewController.player!.play()
        }
    }
}

请帮我解决这个问题。提前致谢。

注意 我可以从数组中创建视频,但视频中只显示第一个和最后一个索引值。对于其余值,仅显示空白屏幕。

【问题讨论】:

  • 你能解决黑屏的问题吗,我也遇到了同样的问题。如果您有解决方案,请分享我

标签: iphone swift video video-processing


【解决方案1】:

我刚刚解决了我的问题,只需要更新代码中的一行。请查看代码。

    if videoAsset != videoURLArray.last{
        videoInstruction.setOpacity(0.0, at: totalTime)
    }

注意:数组的每一个值只需要改变下一个视频的at位置即可。

【讨论】:

  • 我已经尝试过你的解决方案但它不起作用,我的第二个视频时长添加了但视频内容没有添加
【解决方案2】:

正确的做法是改变

   if videoAsset != videoURLArray.last{
    videoInstruction.setOpacity(0.0, at: videoAsset.duration)
}

到:

videoInstruction.setOpacity(0.0, at: totalTime)

我想在这里强调一下,添加

        totalTime = totalTime + videoAsset.duration

在将不透明度设置为图层指令之前,一切都会有所不同。否则视频是黑屏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 2019-08-31
    • 2017-09-19
    • 2020-11-01
    • 2018-02-04
    相关资源
    最近更新 更多