DPVideoMerger-Swift
对于 Objective C : - DPVideoMerger
使用 CocoaPods 安装
CocoaPods 是 Objective-C 的依赖管理器。您可以使用以下命令安装:
$ gem install cocoapods
Podfile
为了使用 CocoaPods 将 DPVideoMerger 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TargetName' do
use_frameworks!
pod 'DPVideoMerger-Swift'
end
然后,运行以下命令:
$ pod install
使用 Carthage 安装
Carthage 是一个去中心化的依赖管理器,它会构建您的依赖项并为您提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage:
$ brew update
$ brew install carthage
要将 DPVideoMerger-Swift
集成到您的 Xcode 项目中使用 Carthage,请在您的 Cartfile
中指定它
github "Datt1994/DPVideoMerger-Swift"
运行 carthage
构建 framework 并将框架(DPVideoMerger_Swift.framework
)拖放到您的 Xcode 项目中。
使用Swift Package Manager进行安装
Swift Package Manager 是一个自动化分发Swift代码的工具,已集成到 swift
编译器中。
要将库作为包依赖项添加到您的Xcode项目中,请选择 File > Swift Packages > Add Package Dependency 并输入其存储库URL https://github.com/Datt1994/DPVideoMerger-Swift.git
手动添加
下载项目并将 DPVideoMerger.swift
文件复制粘贴到您的项目中。
用法
import AVKit
let fileURL = Bundle.main.url(forResource: "1", withExtension: "mp4")
let fileURL1 = Bundle.main.url(forResource: "2", withExtension: "mp4")
let fileURL2 = Bundle.main.url(forResource: "3", withExtension: "MOV")
let fileURL3 = Bundle.main.url(forResource: "4", withExtension: "mp4")
let fileURLs = [fileURL, fileURL1, fileURL2, fileURL3]
/// Multiple videos merge in one video with manage scale & aspect ratio
/// - Parameters:
/// - videoFileURLs: Video file path URLs, Array of videos that going to merge
/// - videoResolution: Output video resolution, (defult: CGSize(width: -1, height: -1), find max width and height from provided videos)
/// - videoQuality: AVAssetExportPresetMediumQuality(default) , AVAssetExportPresetLowQuality , AVAssetExportPresetHighestQuality
/// - completion: Completion give 2 optional values, 1)mergedVideoURL: URL path of successfully merged video 2)error: Gives Error object if some error occur in videos merging process
/// - mergedVideoURL: URL path of successfully merged video
/// - error: Gives Error object if some error occur in videos merging process
DPVideoMerger().mergeVideos(withFileURLs: fileURLs as! [URL], completion: {(_ mergedVideoFile: URL?, _ error: Error?) -> Void in
if error != nil {
let errorMessage = "Could not merge videos: \(error?.localizedDescription ?? "error")"
let alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (a) in
}))
self.present(alert, animated: true) {() -> Void in }
return
}
let objAVPlayerVC = AVPlayerViewController()
objAVPlayerVC.player = AVPlayer(url: mergedVideoFile!)
self.present(objAVPlayerVC, animated: true, completion: {() -> Void in
objAVPlayerVC.player?.play()
})
})
/// Merge videos to grid matrix layout
/// - Parameters:
/// - videoFileURLs: Video file path URLs, Array of videos that going to grid merge
/// - matrix: Video matrix position (eg 3x3, 4x2, 1x3, ...) (default:- 2x2)
/// - audioFileURL: Optional audio file for Merged Video
/// - videoResolution: Output video resolution
/// - isRepeatVideo: Repeat Video on grid if one or more video have shorter duartion time then output video duration
/// - isRepeatAudio: Repeat Audio if Merged video have longer duartion time then provided Audio duration
/// - isAudio: Allow Audio for grid video (default :- true)
/// - videoDuration: Output video duration (defult: -1, find max duration from provided videos)
/// - videoQuality: AVAssetExportPresetMediumQuality(default) , AVAssetExportPresetLowQuality , AVAssetExportPresetHighestQuality
/// - completion: completion give 2 optional values, 1)mergedVideoURL: URL path of successfully grid merged video 2)error: gives Error object if some error occur in videos merging process
/// - mergedVideoURL: URL path of successfully grid merged video
/// - error: gives Error object if some error occur in videos merging process
DPVideoMerger().gridMergeVideos(withFileURLs: fileURLs, videoResolution: CGSize(width: 1000, height: 1000), completion: {(_ mergedVideoFile: URL?, _ error: Error?) -> Void in
if error != nil {
let errorMessage = "Could not merge videos: \(error?.localizedDescription ?? "error")"
let alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (a) in
}))
self.present(alert, animated: true) {() -> Void in }
return
}
let objAVPlayerVC = AVPlayerViewController()
objAVPlayerVC.player = AVPlayer(url: mergedVideoFile!)
self.present(objAVPlayerVC, animated: true, completion: {() -> Void in
objAVPlayerVC.player?.play()
})
})