DPVideoMerger
针对 Swift:- DPVideoMerger-Swift
使用 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
pod 'DPVideoMerger'
end
然后,运行以下命令:
$ pod install
手动添加
下载项目,并将 DPVideoMerger.h
及 DPVideoMerger.m
文件复制粘贴到您的项目中。
用法
#import "DPVideoMerger.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp4"];
NSURL *fileURL1 = [NSURL fileURLWithPath:filePath1];
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"MOV"];
NSURL *fileURL2 = [NSURL fileURLWithPath:filePath2];
NSString *filePath3 = [[NSBundle mainBundle] pathForResource:@"4" ofType:@"mp4"];
NSURL *fileURL3 = [NSURL fileURLWithPath:filePath3];
NSArray *fileURLs = @[fileURL, fileURL1,fileURL2,fileURL3];
[DPVideoMerger mergeVideosWithFileURLs:fileURLs completion:^(NSURL *mergedVideoFile, NSError *error) {
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Could not merge videos: %@", [error localizedDescription]];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:errorMessage preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
return;
}
AVPlayerViewController *objAVPlayerVC = [[AVPlayerViewController alloc] init];
objAVPlayerVC.player = [AVPlayer playerWithURL:mergedVideoFile];
[self presentViewController:objAVPlayerVC animated:YES completion:^{
[objAVPlayerVC.player play];
}];
}];
[DPVideoMerger gridMergeVideosWithFileURLs:fileURLs andVideoResolution:CGSizeMake(2000, 2000) andRepeatVideo:true completion:^(NSURL *mergedVideoURL, NSError *error) {
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Could not merge videos: %@", [error localizedDescription]];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:errorMessage preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
AVPlayerViewController *objAVPlayerVC = [[AVPlayerViewController alloc] init];
objAVPlayerVC.player = [AVPlayer playerWithURL:mergedVideoURL];
[self presentViewController:objAVPlayerVC animated:YES completion:^{
[objAVPlayerVC.player play];
}];
}];