Silence-GitHub / BBMetalImage

A high performance Swift library for GPU-accelerated image/video processing based on Metal.
MIT License
988 stars 126 forks source link

Video is zoomed automatically when use videoWriter? #63

Closed lygialiem closed 3 years ago

lygialiem commented 4 years ago

Hi Silence-GitHub.

I'm now facing this issues with your library. I'm trying to filter a video and then save it to the UserCamera Photo so that I can test it easily. But the video after filtering is automatically zoom and have some laggy. I'll give you the link of original video and after filered video below and this is my code:

private var videoWriter: BBMetalVideoWriter!
private var videoSourceWriter: BBMetalVideoSource!
let outputURL = URL.documentDirectory()?.appendingPathComponent("test.mov", isDirectory: false)

func saveVideoToFolder() {
        guard !FileManager.default.fileExists(atPath: outputURL!.path) else {
            try? FileManager.default.removeItem(atPath: outputURL!.path)
            print("remove")
            return
        }
        //Setup VideSource
        let url = Bundle.main.url(forResource: "test_video", withExtension: "mov")!
        videoSourceWriter = BBMetalVideoSource.init(url: url)
        videoSourceWriter.playWithVideoRate = false

        //Setup Writer
        let outputUrl = outputURL!
        let bbMetalSize = BBMetalIntSize(width: 1080, height: 1920)
        self.videoWriter = BBMetalVideoWriter(url: outputUrl, frameSize: bbMetalSize)

        // Set up 3 filters to process image
        let contrastFilter = BBMetalContrastFilter(contrast: 3)
        let sharpenFilter = BBMetalSharpenFilter(sharpeness: 1)
        let saturation = BBMetalSaturationFilter.init(saturation: 0.0)

        videoSourceWriter
            .add(consumer: contrastFilter)
            .add(consumer: sharpenFilter)
            .add(consumer: saturation)
            .add(consumer: videoWriter)

        videoWriter.start()
        videoSourceWriter.start{ [unowned self] (type) in
            self.videoWriter.finish {
                print("Finish")
                guard let videoUrl = self.outputURL else { return }

                //Save filtered Video to UserCamera Photos.
                PHPhotoLibrary.shared().performChanges({
                    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoUrl)
                }) { saved, error in
                    if saved {
                        let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
                        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(defaultAction)
                        self.present(alertController, animated: true, completion: nil)
                    }
                }
            }
        }
    }

Link of videos: https://drive.google.com/drive/folders/1w0cO5E1AJnzqCSITquRyEPBC57bIWiMB?usp=sharing

Silence-GitHub commented 4 years ago

Version 1.1.7 fixes it. Please try 1.1.7