项目作者: khoavd-dev

项目描述 :
A demo about merging videos & images in iOS
高级语言: Swift
项目地址: git://github.com/khoavd-dev/MergeVideos.git
创建时间: 2017-12-21T13:07:02Z
项目社区:https://github.com/khoavd-dev/MergeVideos

开源协议:MIT License

下载


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
    ```swift
    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)”)
}
}
}

  1. - Merge videos with transition animation
  2. ```swift
  3. let videoAsset1 = AVAsset(url: urlVideo1)
  4. let videoAsset2 = AVAsset(url: urlVideo2)
  5. KVVideoManager.shared.mergeWithAnimation(arrayVideos: [videoAsset1, videoAsset2]) { (outputURL, error) in
  6. if let error = error {
  7. print("Error:\(error.localizedDescription)")
  8. }
  9. else {
  10. if let url = outputURL {
  11. print("Output video file:\(url)")
  12. }
  13. }
  14. }
  • Add background music to a video
    ```swift
    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)”)
}
}
}

  1. - Merge videos and images and text with transition animation
  2. ```swift
  3. let videoData = VideoData()
  4. videoData.isVideo = true
  5. videoData.asset = AVAsset(url: urlVideo)
  6. let imageData = VideoData()
  7. imageData.isVideo = false
  8. imageData.image = UIImage(named: "sample-image")
  9. let textData = TextData(text: "HELLO WORLD",
  10. fontSize: 50,
  11. textColor: UIColor.green,
  12. showTime: 3,
  13. endTime: 5,
  14. textFrame: CGRect(x: 0, y: 0, width: 400, height: 300))
  15. KVVideoManager.shared.makeVideoFrom(data: [videoData, imageData], textData: [textData]) { (outputURL, error) in
  16. if let error = error {
  17. print("Error:\(error.localizedDescription)")
  18. }
  19. else {
  20. if let url = outputURL {
  21. print("Output video file:\(url)")
  22. }
  23. }
  24. }

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 !!!