Code Monkey home page Code Monkey logo

mergevideos's Introduction

MergeVideos

This is a sample implementation for merging multiple videos and/or images using AVFoundation, fixed orientation issues.

Features

  • Merge videos.
  • Merge videos with transition animation.
  • Add background music to a video.
  • Merge videos and images with transition animation.
  • Add text with fade in / fade out animation into a video.

Requirements

  • iOS 13.0+
  • Xcode 13.0+
  • Swift 5+

Updates

20/10/2021

  • Update source code to run on Xccode 13 and Swift 5.
  • Refactor code.
  • Fix issue: merging videos shows black screen sometimes. (update videoCompositionInstructionForTrack function)

Usage

Drag the files in VideoManager folder into your project.

Please refer to the sample project MergeVideos for more details. (Don't forget to run pod install before opening the project).

  • Merge videos
let videoAsset1 = AVAsset(url: urlVideo1)
let videoAsset2 = AVAsset(url: urlVideo2)
        
KVVideoManager.shared.merge(arrayVideos: [videoAsset1, videoAsset2]) { (outputURL, error) in
      if let error = error {
           print("Error:\(error.localizedDescription)")
      }
      else {
           if let url = outputURL {
               print("Output video file:\(url)")
           }
     }
}
  • Merge videos with transition animation
let videoAsset1 = AVAsset(url: urlVideo1)
let videoAsset2 = AVAsset(url: urlVideo2)
        
KVVideoManager.shared.mergeWithAnimation(arrayVideos: [videoAsset1, videoAsset2]) { (outputURL, error) in
      if let error = error {
           print("Error:\(error.localizedDescription)")
      }
      else {
           if let url = outputURL {
               print("Output video file:\(url)")
           }
     }
}
  • Add background music to a video
let videoAsset = AVAsset(url: urlVideo)
let musicAsset = AVAsset(url: urlMusic)
        
KVVideoManager.shared.merge(video:videoAsset, withBackgroundMusic:musicAsset) { (outputURL, error) in
      if let error = error {
           print("Error:\(error.localizedDescription)")
      }
      else {
           if let url = outputURL {
               print("Output video file:\(url)")
           }
     }
}
  • Merge videos and images and text with transition animation
let videoData = VideoData()
videoData.isVideo = true
videoData.asset = AVAsset(url: urlVideo)

let imageData = VideoData()
imageData.isVideo = false
imageData.image = UIImage(named: "sample-image")
        
let textData = TextData(text: "HELLO WORLD",
                        fontSize: 50,
                        textColor: UIColor.green,
                        showTime: 3,
                        endTime: 5,
                        textFrame: CGRect(x: 0, y: 0, width: 400, height: 300))
        
KVVideoManager.shared.makeVideoFrom(data: [videoData, imageData], textData: [textData]) { (outputURL, error) in
     if let error = error {
           print("Error:\(error.localizedDescription)")
      }
      else {
           if let url = outputURL {
               print("Output video file:\(url)")
           }
     }      
}

Note

This is a sample implementation to demonstrate the functions in AVFoundation with just some simple animations, but you got the idea !

You would be able to add more complicated transition animation, text showing animation by using Core Animation !!!

mergevideos's People

Contributors

khoavd-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mergevideos's Issues

Not working when you set custom duration

Video merge is not working if you set let duration = 0.5.toCMTime(). It says Error:Operation Stopped. (although it's working if you're not exporting it to URL but use this video as PlayerItem).
Another problem is that it's also not working if you want to merge for example 20-30 videos or more.(not working in all scenarios).

Any thoughts why?

How to make videos fill desired size?

I really like this framework but I need help with extra functionality. I want videos to fill the desired size (for example: 1080x1920). Because now landscape videos are not being filled. It would be great to get tips how I could achieve that. (basically something similar like videoGravity - aspectFill).

merge issue

i am merging image with video but i's not working.

Black BG Clip issue

Hi,
Whenever I try to merge a video with an image, There is a blank clip added at the start of the video. How I can get rid of it?

Video Output Size Not Adjusted

Great job on incorporating video orientation! However, the output is not correctly adjusted when merging multiple videos. So when one video is portrait the size is adjusted, but the output is never adjusted again when the video is landscape. The result is correct orientation but the width is cut-off for landscape videos.

My change was simply to set outputSize equal to videoSize when the video is not portrait.

 if assetInfo.isPortrait == true {
                videoSize.width = videoTrack.naturalSize.height
                videoSize.height = videoTrack.naturalSize.width
            }
            else
            {
                outputSize = videoSize
            }

Black frame

It seems you haven't updated the source code with this gap. the problem remains. file date 19/10/21
Fix issue: merging videos shows black screen sometimes. (update videoCompositionInstructionForTrack function)

Landscape issue

When the video is saved in the device or we play in AVPlayerController after selecting landscape videos for merging. When I rotate the device to landscape its landscape aspect ratio is breaking.
IMG_0316

Audio Issue

I found that the audio would cut-off in the second merged video. I believe the issue is with the implementation of the audioCompositionTrack. For that the startTime should be kCMTimeZero in all cases. Here is the correction I made which fixed the issue:

// Add audio track to audio composition at specific time

            if let audioTrack = audioTrack {
                try audioCompositionTrack?.insertTimeRange(CMTimeRangeMake(startTime, duration),
                                                           of: audioTrack,
                                                           at: insertTime)
            }

Landscape video merging issue

Hi,
While merging one or more landscape videos shows a black screen . its audio get merged but the video is just a black screen

Video background noise

Hi,

I am merging 2 3 videos, final merged video contains some background noise in it,
Please update, is there a way to remove noise?
I was thinking that there might be some issue with UIIMagePickerController.
But it's the final video that contains noise.

Cannot Open error while merging two videos

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

merge 15 to 20 images

i have used MergeVideos library making 15 to 20 images from video but issue come

Domain = AVFoundationErrorDomain Code=-11839 "Cannot Decode" UserInfo={NSLocalizedDescription=Cannot Decode, NSUnderlyingError=0x10e16d6c0 {Error Domain=NSOSStatusErrorDomain Code=-12913 "(null)"}, NSLocalizedRecoverySuggestion=Stop any other actions that decode media and try again., NSLocalizedFailureReason=The decoder required for this media is busy.}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.