快速,视频叠加不起作用

时间:2018-07-24 04:40:29

标签: swift video merge

我试图通过以下方式查找和处理代码。但是,视频不会覆盖,而是作为普通视频输出。需要帮助解决此问题。

static func mergeVideos(firestUrl: URL , SecondUrl: URL, saveUrl: URL, completion: @escaping (_ error: Error?, _ url: URL?) -> Void) {

        let firstAssets = AVAsset(url: firestUrl)
        let secondAssets = AVAsset(url: SecondUrl)

        let mixComposition = AVMutableComposition()

        let firstTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
        let secondTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

        do{
            try firstTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, firstAssets.duration), of: firstAssets.tracks(withMediaType: .video)[0], at: kCMTimeZero)
            try secondTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, secondAssets.duration), of: secondAssets.tracks(withMediaType: .video)[0], at: kCMTimeZero)
        }catch{
            print("insertTimeRange error: \(error.localizedDescription)")
        }

        let mainInstraction = AVMutableVideoCompositionInstruction()

        mainInstraction.timeRange  = CMTimeRangeMake(kCMTimeZero, firstAssets.duration)

        let firstLayerInstraction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstTrack!)
        let scale = CGAffineTransform(scaleX: 0.6, y: 0.6)
        let move = CGAffineTransform(translationX: 200, y: 200)
        let transform = scale.concatenating(move)
        firstLayerInstraction.setTransform(transform, at: kCMTimeZero)

        let secondLayerInstraction = AVMutableVideoCompositionLayerInstruction(assetTrack: secondTrack!)
        let secondScale = CGAffineTransform(scaleX: 1.2, y: 1.2)
        let secondMove = CGAffineTransform(translationX: 0, y: 0)
        let secondTransform = secondScale.concatenating(secondMove)
        secondLayerInstraction.setTransform(secondTransform, at: kCMTimeZero)

        mainInstraction.layerInstructions = [firstLayerInstraction , secondLayerInstraction]

        let mainCompositonInst = AVMutableVideoComposition()
        mainCompositonInst.instructions = [mainInstraction]
        mainCompositonInst.frameDuration = CMTimeMake(1, 30)
        mainCompositonInst.renderSize = CGSize(width:320, height:480 )


        // Exporting
        do { // delete old video
            try FileManager.default.removeItem(at: saveUrl)
        } catch { print(error.localizedDescription) }


        let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
        assetExport.outputFileType = AVFileType.mp4
        assetExport.outputURL = saveUrl
        assetExport.shouldOptimizeForNetworkUse = true
        assetExport.exportAsynchronously {
            switch assetExport.status {
            case .completed:
                completion(nil, saveUrl)
            case .unknown, .waiting, .exporting, .failed, .cancelled:
                completion(assetExport.error, nil)
            }
        }
    }

0 个答案:

没有答案