AVMutableComposition是否支持保存透明视频?

时间:2019-05-06 11:51:49

标签: swift video overlay transparent avvideocomposition

我一直在尝试在UIImageView上添加 透明视频 。 UIImageView正在播放来自数组的图像的动画。我能够实现。现在,我尝试使用 AVMutableVideoComposition

将确切的视频保存到图库中

尝试保存来自Github项目MyTransparentVideoExample的视频!

我一直在使用 createVideoComposition 函数来获取 AVVideoComposition ,然后将该构成提供给< strong> AVMutableVideoCompostion


var mainComposition : AVMutableVideoComposition!

func createVideoComposition(for playerItem: AVPlayerItem) -> AVVideoComposition
    {
        let videoSize = CGSize(width: playerItem.presentationSize.width / 2.0, height: playerItem.presentationSize.height)
        let composition = AVMutableVideoComposition(asset: playerItem.asset, applyingCIFiltersWithHandler: { request in
            let sourceRect = CGRect(origin: .zero, size: videoSize)
            let alphaRect = sourceRect.offsetBy(dx: 0, dy: 0)
            let filter = AlphaFrameFilter()
            filter.inputImage = request.sourceImage.cropped(to: alphaRect)
               .transformed(by: CGAffineTransform(translationX: 0, y:0))
            filter.maskImage = request.sourceImage.cropped(to: sourceRect)
            return request.finish(with: filter.outputImage!, context: nil)
        })
        composition.renderSize = videoSize
        mainComposition = composition
        return composition
    }



//function to create video

 func createVideo()
    {

        let path = Bundle.main.path(forResource: "template", ofType:"mp4")
        let fileURL = NSURL(fileURLWithPath: path!)
        let composition = AVMutableComposition()
        var vidAsset = AVURLAsset(url: fileURL as URL, options: nil)



        // get video track
        let vtrack =  vidAsset.tracks(withMediaType: AVMediaType.video)
        let videoTrack:AVAssetTrack = vtrack[0]
        let vid_duration = videoTrack.timeRange.duration
        let vid_timerange = CMTimeRangeMake(start: CMTime.zero, duration: vidAsset.duration)

        var error: NSError?
        let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())!
        do {
            try compositionvideoTrack.insertTimeRange(vid_timerange, of: videoTrack, at: CMTime.zero)
        }catch {

        }
        compositionvideoTrack.preferredTransform = videoTrack.preferredTransform



        var tempLayer = CALayer()
        let videolayer = CALayer()
        videolayer.frame = CGRect(x: 0,y:  0,width: videoSize.width,height: videoSize.height)
        videolayer.contentsGravity = CALayerContentsGravity.resize
        videolayer.backgroundColor = UIColor.clear.cgColor



        let parentlayer = CALayer()
        parentlayer.frame = CGRect(x: 0,y: 0,width: videoSize.width,height: videoSize.height)
        tempLayer.frame = parentlayer.bounds
        parentlayer.backgroundColor = UIColor.clear.cgColor
        parentlayer.addSublayer(videolayer)



       mainComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
       mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, in: parentlayer)



        try? FileManager.default.removeItem(at: getPath())

        // use AVAssetExportSession to export video
        let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
        assetExport?.videoComposition = mainComposition

        assetExport?.outputFileType = AVFileType.mp4
        assetExport?.outputURL = getPath()

        var exportProgressBarTimer = Timer() // initialize timer
        if #available(iOS 10.0, *)
        {
            exportProgressBarTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true)
            { timer in
                // Get Progress
                let progress = Float((assetExport!.progress));
                //print(progress)
                if (progress < 1.0)
                {
                    print(progress)

                    let dict:[String: Float] = ["progress": progress]
                    NotificationCenter.default.post(name: Notification.Name("ProgressBarPercentage"), object: nil, userInfo: dict)
                }else
                {
                    exportProgressBarTimer.invalidate()
                }
            }
        }




        assetExport?.exportAsynchronously(completionHandler:
            {
            switch assetExport!.status{
            case  AVAssetExportSession.Status.failed:
                //println("failed \(assetExport.error)")
                print()
            case AVAssetExportSession.Status.cancelled:
                print()  //println("cancelled \(assetExport.error)")
            default:
                PHPhotoLibrary.shared().saveVideo(url: self.getPath(), albumName: "TransparentVideo",completion: nil)


                DispatchQueue.main.async {
                }
            }
        })
    }



上面的代码保存了视频,但它不是透明的,而且如我所愿。保存视频之前我得到的预览是 预览 https://imgur.com/lxeaR8O)。保存视频后,我没有将背景图像动画添加到视频中。 SavedVideoSS https://imgur.com/vF7lVCk

尝试保存来自Github项目MyTransparentVideoExample的视频!

0 个答案:

没有答案