dev-labs-bg / swift-video-generator

MIT License
639 stars 121 forks source link

Video corruption after merge #55

Closed floriangbh closed 4 years ago

floriangbh commented 5 years ago

Hello,

I'm encoutering an issue when I'm trying to merge two .mov video. After merging them, the resulting video appear to be corrupted on other OS (Windows, Ubuntu) or browser (Firefox, Chrome), but working well with QuickTime on MacOS.

Here is the code :

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    guard let firstVideoUrl = Bundle.main.url(forResource: "part1", withExtension: "mov"),
        let secondVideoUrl = Bundle.main.url(forResource: "part2", withExtension: "mov") else {
            assertionFailure("Can't get videos url.")
            return
    }

    VideoGenerator.fileName = "mergedVideo"
    VideoGenerator.mergeMovies(videoURLs: [secondVideoUrl, firstVideoUrl], success: { (videoURL) in
        self.share(url: videoURL)
    }) { (error) in
        print(error)
    }
}

You can reproduce this issue with this sample project : https://github.com/floriangbh/video-merge-issue-demo The result video of the merge is available on this link : https://firebasestorage.googleapis.com/v0/b/speedtest-dev/o/mergedVideo.m4v?alt=media&token=c917f475-49a8-40e6-a5de-2bfdba8501fe (you can open it with Firefox or Chrome to see the corruption).

Have you any idea how to fix it ?

Thanks, Florian

SteliyanH commented 5 years ago

@floriangbh, The video file format after the merge is m4v which is Apple-specific (develop for Apple's platforms). That is why it can't be played on other OS (Windows, Ubuntu). You could try to convert the m4v file to an mp4 file using AVAsset.

floriangbh commented 5 years ago

Hello @SteliyanH I just tried to convert to mp4, but the result is the same...

Commit : https://github.com/floriangbh/video-merge-issue-demo/commit/b70497db64f88ad1d279b8ccd4de13e9a8b2918e Final video : https://firebasestorage.googleapis.com/v0/b/speedtest-dev/o/rendered-Video.mp4?alt=media&token=e5c7f7b6-48a4-45a8-87d3-b3c0ff6beb5c

SteliyanH commented 5 years ago

@floriangbh, If you noticed the produced video from the library is encoded using H.264 format for the video and AAC for the audio. These configurations can't be changed, because the merge is made using native Apple framework. To be able to play such kind of a format on other OS you will need additional codecs. Thus my previous suggestion won't help you :(

Here is a link describing how to play H.264 on windows - https://www.techwalla.com/articles/how-to-play-h264-in-windows-media-player

floriangbh commented 5 years ago

In fact the video need to be readable from web navigator like Chrome, Firefox or Safari. I can't ask to our user to install a specific codec :)