音频和视频合成(视频带背景音)

其中有些代码为数据库的存储,音视频的核心代码都在下面!无非就是取出视频的vedio,录制的音频或者其它的音频,再加上视频的自带的音轨,最终都加入- (BOOL)insertTimeRange:(CMTimeRange)timeRange ofTrack:(AVAssetTrack *)track atTime:(CMTime)startTime error:(NSError * _Nullable * _Nullable)outError;这个方法里整合!

#pragma mark - 音视频合成
- (void)merge{
    // mbp提示框
    //[MBProgressHUD ];
    // 路径
    NSString *savePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true).firstObject;
    NSString *recordVedio = [savePath stringByAppendingPathComponent:@"MJDownload/recordVideos"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:recordVedio]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:recordVedio withIntermediateDirectories:true attributes:nil error:nil];
    }
    // 声音来源
    NSURL *audioInputUrl = [NSURL fileURLWithPath:self.videoModel.video_list[self.currentPage].record_url_path];
    // 视频来源
    NSURL *videoInputUrl = [NSURL fileURLWithPath:self.videoModel.video_list[self.currentPage].video_no_sound_url_path];
    // 最终合成输出路径
    NSString *saveRecordName =  [recordVedio stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",self.videoModel.video_list[self.currentPage].video_id]];
    //写进model
    self.videoModel.video_list[self.currentPage].dubbing_url_path = saveRecordName;
    NSLog(@"音视频合成路径  =  %@",saveRecordName);
    // 添加合成路径
    NSURL *outputFileUrl = [NSURL fileURLWithPath:saveRecordName];
    // 时间起点
    CMTime nextClistartTime = kCMTimeZero;
    // 创建可变的音视频组合
    AVMutableComposition *comosition = [AVMutableComposition composition];
    
    
    // 视频采集
    AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoInputUrl options:nil];
    // 视频时间范围
    CMTimeRange videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
    // 视频通道 枚举 kCMPersistentTrackID_Invalid = 0
    AVMutableCompositionTrack *videoTrack = [comosition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    // 视频采集通道
    AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    //  把采集轨道数据加入到可变轨道之中
    [videoTrack insertTimeRange:videoTimeRange ofTrack:videoAssetTrack atTime:nextClistartTime error:nil];
    
    
    // 声音采集
    AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audioInputUrl options:nil];
    // 因为视频短这里就直接用视频长度了,如果自动化需要自己写判断
    CMTimeRange audioTimeRange = videoTimeRange;
    // 音频通道
    AVMutableCompositionTrack *audioTrack = [comosition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    // 音频采集通道
    AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
    // 加入合成轨道之中
    [audioTrack insertTimeRange:audioTimeRange ofTrack:audioAssetTrack atTime:nextClistartTime error:nil];
    
    
    //加入视频的原音-加入合成轨道之中
    AVMutableCompositionTrack *originalAudioCompositionTrack = nil;
        AVAssetTrack *originalAudioAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
        originalAudioCompositionTrack = [comosition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [originalAudioCompositionTrack insertTimeRange:audioTimeRange ofTrack:originalAudioAssetTrack atTime:nextClistartTime error:nil];
    
    
    // 创建一个输出
    AVAssetExportSession *assetExport = [[AVAssetExportSession alloc] initWithAsset:comosition presetName:AVAssetExportPresetMediumQuality];
    // 输出类型
    assetExport.outputFileType = AVFileTypeMPEG4;
    //[assetExport setOutputFileType:AVFileTypeMPEG4];
    // 输出地址
    assetExport.outputURL = outputFileUrl;
    // 优化
    assetExport.shouldOptimizeForNetworkUse = YES;
    // 音量控制-制作混合音
    assetExport.audioMix = [self buildAudioMixWithVideoTrack:originalAudioCompositionTrack VideoVolume:1.0 BGMTrack:audioTrack BGMVolume:1.0 atTime:kCMTimeZero];
    // 合成完毕
    [assetExport exportAsynchronouslyWithCompletionHandler:^{
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            //数据库进行更新操作
           BOOL isOk = [[PublicDataBaseMethod sharedStaticDataBaseManager] updateEnglishVideoList:self.videoModel.sutdent_uuid andBookId:self.videoModel.book_id andVideoId:self.videoModel.video_list[self.currentPage].video_id andRecordUrlPath:self.videoModel.video_list[self.currentPage].record_url_path andDubbingUrlPath:self.videoModel.video_list[self.currentPage].dubbing_url_path andCloudStarCount:self.videoModel.video_list[self.currentPage].cloud_star_count andDuuingDataBase:YES];
            if (isOk) {
                NSLog(@"数据库写入成功");
                //更新model
                self.videoModel.video_list[self.currentPage].isDuuingDataBase = @"1";
                CGFloat totalStarNum = 0;
                //计算配音完成度
                for (int i=0; i0) {
                        if([self.videoModel.video_list[i].isDuuingDataBase isEqualToString:@"1"]){//1
                                totalStarNum++;
                        }
                    }
                }
                
                //进度条更新
                [self.progressView setProgress:totalStarNum/self.videoModel.video_list.count animated:YES];
                if (self.progressView.progress == 1.0) {
                    self.finishButton.userInteractionEnabled = YES;
                    [self.finishButton setImage:[UIImage imageNamed:@"active_finish_btn"] forState:UIControlStateNormal];
                }
                //记录进度条进度
                //[[PublicDataBaseMethod sharedStaticDataBaseManager] updateEnglishVideoList:self.videoModel.sutdent_uuid andBookId:self.videoModel.book_id andProgressCount:[NSString stringWithFormat:@"%f",(float)(float)totalStarNum/(float)self.videoModel.video_list.count]];
                //更新UI
                [self.dubbingCollectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForItem:self.currentPage inSection:0],nil]];
                self.hud.labelText = @"合成完成";
                [self.hud hide:YES];
            }else{
                NSLog(@"更新失败");
                self.hud.labelText = @"合成失败";
                [self.hud hide:YES];
            }
        });
    }];
}

你可能感兴趣的:(音频和视频合成(视频带背景音))