admin管理员组

文章数量:1530063

在短视频app源码开发中,音视频数据的处理是关键,尤其是音视频合成处理,只有有声音的短视频内容才更有吸引力,在短视频app源码中如何实现音视频的合成呢?

音频合成

调用方法

//音视频合成
    func audioMerge() {
        // 声音来源1
        let audioInputPath1 = NSBundle.mainBundle().pathForResource("五环之歌", ofType: "mp3")!
        // 音频来源2
        let audioInputPath2 = NSBundle.mainBundle().pathForResource("爱的人", ofType: "mp3")!

        let ext = ExtAudioFileMixer()
        ext.audioInputPath1 = audioInputPath1;
        ext.audioInputPath2 = audioInputPath2;

        ext.process {url in

            self.playWithUrl(url)
        }



    }

实现

 AVMutableComposition *composition =[AVMutableComposition composition];
    audioMixParams =[[NSMutableArray alloc]init];

    //本地音乐1
    NSURL *audioInputUrl1 =[NSURL fileURLWithPath:self.audioInputPath1];
    AVURLAsset *audioAsset =[AVURLAsset URLAssetWithURL:audioInputUrl1 options:nil];
    CMTime startTime =CMTimeMakeWithSeconds(0,audioAsset.duration.timescale);
    CMTime trackDuration =audioAsset.duration;
    //获取本地音乐1素材
    [self setUpAndAddAudioAtPath:audioInputUrl1 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(0,44100)];
    //本地音乐2
    NSURL *audioInputUrl2 = [NSURL fileURLWithPath:self.audioInputPath2];
    //获取本地音乐2素材
    [self setUpAndAddAudioAtPath:audioInputUrl2 toComposition:composition start:startTime dura:trackDuration offset:CMTimeMake(0,44100)];

    //创建一个可变的音频混合
    AVMutableAudioMix *audioMix =[AVMutableAudioMix audioMix];
    audioMix.inputParameters =[NSArray arrayWithArray:audioMixParams];//从数组里取出处理后的音频轨道参数

    //创建一个输出
    AVAssetExportSession *exporter =[[AVAssetExportSession alloc]
                                     initWithAsset:composition
                                     presetName:AVAssetExportPresetAppleM4A];
    exporter.audioMix = audioMix;
    exporter.outputFileType = AVFileTypeAppleM4A;
    NSString* fileName =[NSString stringWithFormat:@"%@.m4a",@"overMix"];
    //输出路径
    NSString *exportFile =[NSString stringWithFormat:@"%@/%@",[self getLibarayPath], fileName];
    self.audioOutputPath = exportFile;

    if([[NSFileManager defaultManager]fileExistsAtPath:self.audioOutputPath]) {
        [[NSFileManager defaultManager]removeItemAtPath:self.audioOutputPath error:nil];
    }
    NSLog(@"输出路径===%@",self.audioOutputPath);

    NSURL *exportURL =[NSURL fileURLWithPath:self.audioOutputPath];
    exporter.outputURL = exportURL;

    [exporter exportAsynchronouslyWithCompletionHandler:^{
        int exportStatus =(int)exporter.status;
        switch (exportStatus){
            case AVAssetExportSessionStatusFailed:{
                NSError *exportError = exporter.error;
                NSLog(@"错误,信息: %@", exportError);

                break;
            }
            case AVAssetExportSessionStatusCompleted:{

                unsigned long long size = [[NSFileManager defaultManager] attributesOfItemAtPath:exportFile error:nil].fileSize;
                NSLog(@"是否在主线程2%d,,%llu",[NSThread isMainThread],size/1024);

                finished(exportURL);
                break;
            }
        }
    }];

以上就是在短视频app源码开发中,音视频合并完成的全部内容了。

本文标签: 音视频源码视频app