我正致力于合并视频功能。
在我的应用程序中,我有一个带有多个轨道的视频(总共10个轨道9个视频轨道和一个音轨很常见)。 现在我想从这个多轨道获得3个视频。 第一个视频与1,4,7轨道相结合 第二个视频与2,5,8轨道和 第三个视频与3,6,9轨道相结合 音频轨道对于这三个视频很常见。
我想使用以下代码执行此操作。
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:urlSubItem.path]];
NSLog(@"%@",playerItem);
NSArray* arrTracks = [playerItem.asset tracksWithMediaType:AVMediaTypeVideo];
NSLog(@"%@",arrTracks);
NSArray* arrTracksText = [playerItem.asset tracksWithMediaType:AVMediaTypeText];
NSArray* arrTracksAudio = [playerItem.asset tracksWithMediaType:AVMediaTypeAudio];
NSLog(@"%@:%@",arrTracks,arrTracksAudio);
for (int i=0; i<3; i++) {
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, playerItem.asset.duration) ofTrack:[[playerItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
CMTime currentTime = kCMTimeZero;
for (int k=0;k<[arrTracks count];k++) {
NSLog(@"%d",k%3);
if(k%3==i){
AVAssetTrack* trackCombineVideo = [arrTracks objectAtIndex:k];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError* errors;
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(currentTime, trackCombineVideo.timeRange.duration) ofTrack:[arrTracks objectAtIndex:k] atTime:currentTime error:&errors];
if (errors) {
NSLog(@"wait error:%@",errors);
}
currentTime = trackCombineVideo.timeRange.duration;
}
}
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
NSLog(@"file type %@",_assetExport.outputFileType);
_assetExport.outputURL = exportUrl // Document Directory Path;
_assetExport.shouldOptimizeForNetworkUse = YES;
使用此代码创建3个单独的视频,但每个视频都有3个不同的曲目。 现在,我的问题是如何只用一个曲目创建视频?
答案 0 :(得分:1)
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition
addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
for (int k=0;k<[arrTracks count];k++) {
NSLog(@"%d",k%3);
if(k%3==i){
AVAssetTrack* trackCombineVideo = [arrTracks objectAtIndex:k];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(currentTime, trackCombineVideo.timeRange.duration) ofTrack:[arrTracks objectAtIndex:k] atTime:currentTime error:&errors];
if (errors) {
NSLog(@"wait error:%@",errors);
}
currentTime = trackCombineVideo.timeRange.duration;
}
}
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
NSLog(@"file type %@",_assetExport.outputFileType);
_assetExport.outputURL = exportUrl // Document Directory Path;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.timeRange = CMTimeRangeMake(kCMTimeZero, playerItem.asset.duration);