音频

iOS屏幕录制步骤二:合成音频和视频

2017-03-01  本文已影响583人  OC笔记

前面的文章我介绍了如何将多张图片合成视频,这篇文章将继续介绍如何将同时录制的音频合成到视频文件中(音频的录制就不做介绍了,使用AVAudioRecorder就可录制音频)。

下面直接贴代码介绍吧

+ (void)mergeVideo:(NSString *)videoPath andAudio:(NSString *)audioPath withCompletion:(ZYSExportVideoCompletion)completion {
    
    // 视频和音频源文件
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
    
    // 最终合成后文件的输出路径
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *outputPath = [docPath stringByAppendingPathComponent:@"ScreenRecord.mp4"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:outputPath]) {
        if (![fm removeItemAtPath:outputPath error:nil]) {
            NSLog(@"remove old output file failed.");
        }
    }
    NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
    
    
    // 时间起点
    CMTime startTime = kCMTimeZero;
    
    // 创建音视频组合操作类
    AVMutableComposition *composition = [AVMutableComposition composition];
    
    
    /// 视频采集
    AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    
    // 获取视频时间范围
    CMTimeRange videoTimeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
    
    // 创建视频通道
    AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    
    // 创建视频采集通道
    AVAssetTrack *videoAssetTrack = [videoAsset tracksWithMediaType:AVMediaTypeVideo].firstObject;
    
    // 将采集的视频数据加入到可变轨道中
    [videoTrack insertTimeRange:videoTimeRange ofTrack:videoAssetTrack atTime:startTime error:nil];
    
    
    /// 音频采集
    AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audioURL options:nil];
    
    // 音频时间范围
    CMTimeRange audioTimeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
    
    // 创建音频通道
    AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    
    // 获取音频采集通道
    AVAssetTrack *audioAssetTrack = [audioAsset tracksWithMediaType:AVMediaTypeAudio].firstObject;
    
    // 将采集的音频数据添加到可变轨道中
    [audioTrack insertTimeRange:audioTimeRange ofTrack:audioAssetTrack atTime:startTime error:nil];
    
    // 创建一个视频输出
    AVAssetExportSession *assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
    
    // 视频输出类型
    assetExport.outputFileType = AVFileTypeMPEG4;
    
    // 视频输出地址
    assetExport.outputURL = outputURL;
    
    // 是否优化视频
    assetExport.shouldOptimizeForNetworkUse = YES;
    
    // 导出视频
    [assetExport exportAsynchronouslyWithCompletionHandler:^{
        // 删除原视频、音频文件
        if ([fm fileExistsAtPath:videoPath]) {
            if (![fm removeItemAtPath:videoPath error:nil]) {
                NSLog(@"remove video.mp4 failed.");
            }
        }
        
        if ([fm fileExistsAtPath:audioPath]) {
            if (![fm removeItemAtPath:audioPath error:nil]) {
                NSLog(@"remove audio.wav failed.");
            }
        }
        
        if (completion) {
            completion(outputPath);
        }
    }];
}
上一篇下一篇

猜你喜欢

热点阅读