FFmpeg音頻播放器(6)-多輸入filter(amix)

FFmpeg音頻播放器(1)-簡(jiǎn)介
FFmpeg音頻播放器(2)-編譯動(dòng)態(tài)庫(kù)
FFmpeg音頻播放器(3)-將FFmpeg加入到Android中
FFmpeg音頻播放器(4)-將mp3解碼成pcm
FFmpeg音頻播放器(5)-單輸入filter(volume,atempo)
FFmpeg音頻播放器(6)-多輸入filter(amix)
FFmpeg音頻播放器(7)-使用OpenSLES播放音頻
FFmpeg音頻播放器(8)-創(chuàng)建FFmpeg播放器
FFmpeg音頻播放器(9)-播放控制
FFmpeg使用過(guò)濾器filter的另外一個(gè)場(chǎng)景就是處理多個(gè)輸入數(shù)據(jù),比如視頻添加水印忿等,添加字幕糟需,音視頻合并等鳞滨。這類場(chǎng)景需要兩個(gè)及以上輸入端奉瘤。本節(jié)講amix亲族,它可以將多個(gè)音頻混音种呐。

處理流程

輸入AVFrame1 -> abuffer  
                         -> amix -> aformat -> abuffersink -> 輸出AVFrame
輸入AVFrame2 -> abuffer  

處理流程和單輸入過(guò)濾器大致相同狠半,只不過(guò)接收了多個(gè)輸入端噩死。因此需要多個(gè)filter上下文作為輸入端。

初始化amix

//初始化amix filter
int init_amix_filter(AVFilterGraph **pGraph, AVFilterContext **srcs, AVFilterContext **pOut,
                     jsize len) {
    AVFilterGraph *graph = avfilter_graph_alloc();
    for (int i = 0; i < len; i++) {
        AVFilter *filter = avfilter_get_by_name("abuffer");
        char name[50];
        snprintf(name, sizeof(name), "src%d", i);
        AVFilterContext *abuffer_ctx = avfilter_graph_alloc_filter(graph, filter, name);
        if (avfilter_init_str(abuffer_ctx,
                              "sample_rate=48000:sample_fmt=s16p:channel_layout=stereo") < 0) {
            LOGE("error init abuffer filter");
            return -1;
        }
        srcs[i] = abuffer_ctx;
    }
    AVFilter *amix = avfilter_get_by_name("amix");
    AVFilterContext *amix_ctx = avfilter_graph_alloc_filter(graph, amix, "amix");
    char args[128];
    snprintf(args, sizeof(args), "inputs=%d:duration=first:dropout_transition=3", len);
    if (avfilter_init_str(amix_ctx, args) < 0) {
        LOGE("error init amix filter");
        return -1;
    }
    AVFilter *aformat = avfilter_get_by_name("aformat");
    AVFilterContext *aformat_ctx = avfilter_graph_alloc_filter(graph, aformat, "aformat");
    if (avfilter_init_str(aformat_ctx,
                          "sample_rates=48000:sample_fmts=s16p:channel_layouts=stereo") < 0) {
        LOGE("error init aformat filter");
        return -1;
    }
    AVFilter *sink = avfilter_get_by_name("abuffersink");
    AVFilterContext *sink_ctx = avfilter_graph_alloc_filter(graph, sink, "sink");
    avfilter_init_str(sink_ctx, NULL);
    for (int i = 0; i < len; i++) {
        if (avfilter_link(srcs[i], 0, amix_ctx, i) < 0) {
            LOGE("error link to amix");
            return -1;
        }
    }
    if (avfilter_link(amix_ctx, 0, aformat_ctx, 0) < 0) {
        LOGE("error link to aformat");
        return -1;
    }
    if (avfilter_link(aformat_ctx, 0, sink_ctx, 0) < 0) {
        LOGE("error link to sink");
        return -1;
    }
    if (avfilter_graph_config(graph, NULL) < 0) {
        LOGE("error config graph");
        return -1;
    }
    *pGraph = graph;
    *pOut = sink_ctx;
    return 0;
}

這里用一個(gè)數(shù)組保存輸入AVFilterContex神年,通過(guò)遍歷循環(huán)將每個(gè)輸入端鏈接到amix過(guò)濾器已维,這樣就可以接收多個(gè)輸入端了。

使用amix

為了能夠傳入多個(gè)音頻數(shù)據(jù)已日,我們需要同時(shí)解碼多個(gè)音頻文件垛耳,因此在Java層,傳入字符串?dāng)?shù)組飘千。

external fun mixAudio(arr: Array<String>,out:String)
val path = "${Environment.getExternalStorageDirectory()}/test"
val paths = arrayOf(
                    "$path/a.mp3",
                    "$path/b.mp3",
                    "$path/c.mp3",
                    "$path/d.mp3"
)
mixAudio(paths,"$path/mix.pcm")

在jni層使用多個(gè)解碼器解碼每個(gè)文件

extern "C" JNIEXPORT void
JNICALL
Java_io_github_iamyours_ffmpegaudioplayer_MainActivity_mixAudio(
        JNIEnv *env,
        jobject /* this */, jobjectArray _srcs, jstring _out) {
    //將java傳入的字符串?dāng)?shù)組轉(zhuǎn)為c字符串?dāng)?shù)組
    jsize len = env->GetArrayLength(_srcs);
    const char *out_path = env->GetStringUTFChars(_out, 0);
    char **pathArr = (char **) malloc(len * sizeof(char *));
    int i = 0;
    for (i = 0; i < len; i++) {
        jstring str = static_cast<jstring>(env->GetObjectArrayElement(_srcs, i));
        pathArr[i] = const_cast<char *>(env->GetStringUTFChars(str, 0));
    }
    //初始化解碼器數(shù)組
    av_register_all();
    AVFormatContext **fmt_ctx_arr = (AVFormatContext **) malloc(len * sizeof(AVFormatContext *));
    AVCodecContext **codec_ctx_arr = (AVCodecContext **) malloc(len * sizeof(AVCodecContext *));
    int stream_index_arr[len];
    for (int n = 0; n < len; n++) {
        AVFormatContext *fmt_ctx = avformat_alloc_context();
        fmt_ctx_arr[n] = fmt_ctx;
        const char *path = pathArr[n];

        if (avformat_open_input(&fmt_ctx, path, NULL, NULL) < 0) {//打開(kāi)文件
            LOGE("could not open file:%s", path);
            return;
        }
        if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {//讀取音頻格式文件信息
            LOGE("find stream info error");
            return;
        }
        //獲取音頻索引
        int audio_stream_index = -1;
        for (int i = 0; i < fmt_ctx->nb_streams; i++) {
            if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
                audio_stream_index = i;
                LOGI("find audio stream index");
                break;
            }
        }
        if (audio_stream_index < 0) {
            LOGE("error find stream index");
            break;
        }
        stream_index_arr[n] = audio_stream_index;
        //獲取解碼器
        AVCodecContext *codec_ctx = avcodec_alloc_context3(NULL);
        codec_ctx_arr[n] = codec_ctx;
        avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar);
        AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
        //打開(kāi)解碼器
        if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
            LOGE("could not open codec");
            return;
        }
    }
    //初始化SwrContext
    SwrContext *swr_ctx = swr_alloc();
    enum AVSampleFormat in_sample_fmt = codec_ctx_arr[0]->sample_fmt;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int in_sample_rate = codec_ctx_arr[0]->sample_rate;
    int out_sample_rate = in_sample_rate;
    uint64_t in_ch_layout = codec_ctx_arr[0]->channel_layout;
    uint64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
    swr_alloc_set_opts(swr_ctx, out_ch_layout, out_sample_fmt, out_sample_rate, in_ch_layout,
                       in_sample_fmt,
                       in_sample_rate, 0, NULL);
    swr_init(swr_ctx);
    int out_ch_layout_nb = av_get_channel_layout_nb_channels(out_ch_layout);//聲道個(gè)數(shù)
    uint8_t *out_buffer = (uint8_t *) av_malloc(MAX_AUDIO_SIZE);//重采樣數(shù)據(jù)
    //初始化amix filter
    AVFilterGraph *graph;
    AVFilterContext **srcs = (AVFilterContext **) malloc(len * sizeof(AVFilterContext *));
    AVFilterContext *sink;
    avfilter_register_all();
    init_amix_filter(&graph, srcs, &sink, len);
    //開(kāi)始解碼
    FILE *out_file = fopen(out_path, "wb");
    AVFrame *frame = av_frame_alloc();
    AVPacket *packet = av_packet_alloc();
    int ret = 0, got_frame;
    int index = 0;
    while (1) {
        for (int i = 0; i < len; i++) {
            ret = av_read_frame(fmt_ctx_arr[i], packet);
            if (ret < 0)break;
            if (packet->stream_index == stream_index_arr[i]) {
                ret = avcodec_decode_audio4(codec_ctx_arr[i], frame, &got_frame, packet);
                if (ret < 0)break;
                if (got_frame > 0) {
                    ret = av_buffersrc_add_frame(srcs[i], frame);//將解碼后的AVFrame加入到amix輸入端
                    if (ret < 0) {
                        LOGE("error add frame:%d", index);
                        break;
                    }
                }
            }
        }
        while (av_buffersink_get_frame(sink, frame) >= 0) {
            swr_convert(swr_ctx, &out_buffer, MAX_AUDIO_SIZE, (const uint8_t **) frame->data,
                        frame->nb_samples);
            int out_size = av_samples_get_buffer_size(NULL, out_ch_layout_nb, frame->nb_samples,
                                                      out_sample_fmt, 0);
            fwrite(out_buffer, 1, out_size, out_file);
        }
        if (ret < 0) {
            LOGI("error frame:%d", index);
            break;
        }
        LOGI("decode frame :%d", index);
        index++;
    }
    LOGI("finish");
}

使用audition打開(kāi)輸出文件mix.pcm堂鲜,可以聽(tīng)到四個(gè)文件混音后的音頻。
具體的音頻在assets目錄下护奈,可以自行對(duì)比下效果
項(xiàng)目地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末缔莲,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子逆济,更是在濱河造成了極大的恐慌酌予,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奖慌,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡松靡,警方通過(guò)查閱死者的電腦和手機(jī)简僧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)雕欺,“玉大人岛马,你說(shuō)我怎么就攤上這事棉姐。” “怎么了啦逆?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵伞矩,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我夏志,道長(zhǎng)乃坤,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任沟蔑,我火速辦了婚禮湿诊,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘瘦材。我一直安慰自己厅须,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布食棕。 她就那樣靜靜地躺著朗和,像睡著了一般。 火紅的嫁衣襯著肌膚如雪簿晓。 梳的紋絲不亂的頭發(fā)上例隆,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音抢蚀,去河邊找鬼镀层。 笑死,一個(gè)胖子當(dāng)著我的面吹牛皿曲,可吹牛的內(nèi)容都是我干的唱逢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼屋休,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼坞古!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起劫樟,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤痪枫,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后叠艳,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體奶陈,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年附较,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吃粒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拒课,死狀恐怖徐勃,靈堂內(nèi)的尸體忽然破棺而出事示,到底是詐尸還是另有隱情,我是刑警寧澤僻肖,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布肖爵,位于F島的核電站,受9級(jí)特大地震影響臀脏,放射性物質(zhì)發(fā)生泄漏劝堪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一谁榜、第九天 我趴在偏房一處隱蔽的房頂上張望幅聘。 院中可真熱鬧,春花似錦窃植、人聲如沸帝蒿。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)葛超。三九已至,卻和暖如春延塑,著一層夾襖步出監(jiān)牢的瞬間绣张,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工关带, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留侥涵,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓宋雏,卻偏偏與公主長(zhǎng)得像芜飘,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子磨总,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355