khoavd-dev / MergeVideos

A demo about merging videos & images in iOS
MIT License
110 stars 29 forks source link

Cannot Open error while merging two videos #1

Closed ibhavin closed 6 years ago

ibhavin commented 6 years ago

I am merging two videos with this code:

let urlVideo1 = Bundle.main.url(forResource: "gif2vid", withExtension: "mp4")
let urlVideo2 = Bundle.main.url(forResource: "mergedVideo", withExtension: "mp4")
let videoAsset1 = AVAsset(url: urlVideo1!)
let videoAsset2 = AVAsset(url: urlVideo2!)
KVVideoManager.shared.merge(arrayVideos: [videoAsset1, videoAsset2], completion: { (final_URL, error) in
        print("final_URL:=",final_URL as Any)
        print("Error:=",error?.localizedDescription ?? "")
})

It prints final_URL but video is not available there. It prints error: Cannot Open

khoavd-dev commented 6 years ago

Could you provide more details about 2 videos "gif2vid.mp4" and "mergedVideo.mp4" ? (dimension). I can still merge 2 mp4 videos normally.

ibhavin commented 6 years ago

@khoavd-dev

Basically I am converting a gif to video file from this ref. link: https://gist.github.com/powhu/00acd9d34fa8d61d2ddf5652f19cafcf and after that I am merging that video to other normal video.

Here are the samples of videos: http://www.yourfilelink.com/get.php?fid=1472967 http://www.yourfilelink.com/get.php?fid=1472968

khoavd-dev commented 6 years ago

The gif-to-video file is too short (0.4s), I guess that's the problem. Have you tried the gif-to-video file with duration over 1 second ?

ibhavin commented 6 years ago

@khoavd-dev
Yes, I have also tried with gif-to-video file which is about 6 second long.

http://www.yourfilelink.com/get.php?fid=1472983

let data = try! Data(contentsOf: Bundle.main.url(forResource: "giphy3", withExtension: "gif")!)
        let tempUrl = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("bean.mp4")
        GIF2MP4(data: data)?.convertAndExport(to: tempUrl, completion: {
            print("=========================")
            print("Completed...")

            let asset1 = AVAsset(url: tempUrl)
            let asset2 = AVAsset(url: single_Image_Video_Url!)
            KVVideoManager.shared.mergeWithAnimation(arrayVideos: [asset1, asset2], completion: { (final_Output, error) in
                print("=========================")
                print("final_Output:=",final_Output as Any)
                print("Error:=",error?.localizedDescription ?? "")
            })
        })
khoavd-dev commented 6 years ago

Ahh, I found out that your videos have no sound. We can't export video without sound track. I fixed that issue by adding silence sound. Please pull source code again.

ibhavin commented 6 years ago

@khoavd-dev can you tell me how to repeat audio if video length is greater than audio length while merging video with audio?

khoavd-dev commented 6 years ago

@ibhavin you can compare the audio length and video length, then insert the corresponding time range for audio track: try audioCompositionTrack?.insertTimeRange(CMTimeRangeMake(startTime, duration), of: audioTrack, at: insertTime)

All you need is to find out the correct value for startTime and duration