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 Write not working #32

Closed R3l0ad3d closed 4 years ago

R3l0ad3d commented 4 years ago

i just copy video writing code from here . but its say "Asset writer can not start writing" . i change only source video url . its play but no write func setup() { // Set up video writer let filePath = NSTemporaryDirectory() + "test.mp4" let outputUrl = URL(fileURLWithPath: filePath) videoWriter = BBMetalVideoWriter(url: outputUrl, frameSize: BBMetalIntSize(width: 1080, height: 1920)) print(outputUrl) // Set up video source let sourceURL = Bundle.main.url(forResource: "video", withExtension: "mp4")! videoSource = BBMetalVideoSource(url: sourceURL)

    // Set video source audio consumer to write audio data
    videoSource!.audioConsumer = videoWriter

    // Set up 3 filters to process image
    //let contrastFilter = BBMetalContrastFilter(contrast: 1)
    //let sharpenFilter = BBMetalSharpenFilter(sharpeness: 1)
    let blur = BBMetalGaussianBlurFilter(sigma: 10)

    // Set up filter chain
    videoSource!.add(consumer: blur)
        //.add(consumer: sharpenFilter)
        .add(consumer: metalView!)
    videoSource?.add(consumer: videoWriter!)

    // Start receiving Metal texture and writing video file
    videoWriter!.start()

    // Start reading and processing video frame and auido data
    videoSource!.start { [weak self] (_) in
        // All video data is processed
        guard let self = self else { return }

        // Finish writing video file
        self.videoWriter!.finish {
            // Do something after writing the video file
        }
    }
}
Silence-GitHub commented 4 years ago

Maybe there is a file with the same outputUrl, so the video writer can not start writing. Make sure the output url is not used. You can try to remove the file with the same output url before video writer starting.

try? FileManager.default.removeItem(at: videoWriter.url)
R3l0ad3d commented 4 years ago

thanks its working after add try? FileManager.default.removeItem(at: videoWriter.url) another issue i face thats audio . only video play but no sound . how can i solve this . video with audio , and if i want to add extra audio to video how can i add ??

Silence-GitHub commented 4 years ago

BBMetalVideoWriter can record video with audio. To play audio, you can use AVPlayer. There may be a problem for audio playing.

See https://github.com/Silence-GitHub/BBMetalImage/issues/19#issuecomment-540114600