我的应用程序使用uiimagepickerController录制多个视频,我想将它们合并为一个,一个接一个。
我还想将此合并的视频保存到照片库中。
有人知道该怎么做吗?谢谢。
在我的一个旧遗留项目中,我在Objective-C中合并了两个视频。你可以激励(但可能有更好、更聪明的方法)。我的解决方案是:
+ (void) mergeVideos:(NSString*) firstVideo withVideo:(NSString*) secondVideo
{
AVMutableComposition* composition = [AVMutableComposition composition];
AVURLAsset* video1 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:firstVideo] options:nil];
AVURLAsset* video2 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:secondVideo] options:nil];
AVMutableCompositionTrack * composedTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration)
ofTrack:[[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero
error:nil];
[composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2.duration)
ofTrack:[[video2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:video1.duration
error:nil];
NSString *finalPath = [folderPath stringByAppendingPathComponent:@"/final.mov"];
NSURL *lastURL = [NSURL fileURLWithPath:finalPath];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=lastURL;
exporter.outputFileType = AVFileTypeMPEG4;
exporter.shouldOptimizeForNetworkUse = NO;
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch ([exporter status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"failed");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"cancelled");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"fjuhuuuuu");
break;
default:
break;
}
}];
}
TLDR - 您应该在 AVFoundation 框架中查找 AVMutableComposition 和 AVAssetExportSession。