FFMPEG提取视频中的音频

前言

出于项目要求,需要识别视频中的声音,转成文字后,以字幕的形式再嵌入视频中。完成项目需要分成三步。第一步需要提取中的视频中的音频,第二步把音频转成文字保存成字幕文件,第三部把字幕嵌入原视频。
本文主要介绍第一步:提取视频中的音频。

FFMPEG命令行提取音频

ffmpeg -i d:\123.mp4 -vn 123.mp3
# d:123.mp4 源视频
# 123.mp3 导出音频

代码方式提取音频

    av_register_all();
    avcodec_register_all();
    AVFormatContext* ifmt_ctx = NULL;
    int a_stream_index = -1;
    AVCodec* a_dec = NULL;
    AVCodecContext* a_dec_ctx = NULL;
    AVPacket* pkt;
    AVFrame* frame;
    SwrContext* swr_ctx;
    int ret;
    ret = avformat_open_input(&ifmt_ctx, m_srcFilePath.toStdString().c_str(), NULL, NULL);
    if (ret < 0) {
        return;
    }
    avformat_find_stream_info(ifmt_ctx, NULL);
    a_stream_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &a_dec, 0);
    if (a_stream_index < 0) {
        goto __FAIL;
    }
    a_dec_ctx = avcodec_alloc_context3(a_dec);
    a_dec_ctx->pkt_timebase = ifmt_ctx->streams[a_stream_index]->time_base;
    avcodec_parameters_to_context(a_dec_ctx, ifmt_ctx->streams[a_stream_index]->codecpar);
    if (!a_dec_ctx) {
        goto __FAIL;
    }
    ret = avcodec_open2(a_dec_ctx, a_dec, NULL);
    if (ret < 0) {
        goto __FAIL;
    }
    frame = av_frame_alloc();
    pkt = av_packet_alloc();
    //输出采样格式
    int64_t out_ch_layout = AV_CH_LAYOUT_MONO;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
    int out_sample_rate = OUT_SAMPLE;

    swr_ctx = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt, out_sample_rate, a_dec_ctx->channels, a_dec_ctx->sample_fmt, a_dec_ctx->sample_rate, 0, NULL);
    ret = swr_init(swr_ctx);
    uint8_t* out_buffer = (uint8_t*)av_malloc(OUT_SAMPLE * 2);
    FILE* fp_pcm = _wfopen(m_destFilePath.toStdWString().c_str(), L"wb");//输出到文件
    if (fp_pcm == NULL) {
        return;
    }
    while (1) {
        ret = av_read_frame(ifmt_ctx, pkt);
        if (ret < 0) {
            break;
        }
        if (pkt->stream_index != a_stream_index) {
            printf("%d\n", pkt->size);
            continue;
        }
        ret = avcodec_send_packet(a_dec_ctx, pkt);
        if (ret < 0) {
            printf("avcodec_send_packet出错\n");
            break;
        }
        while (1) {
            ret = avcodec_receive_frame(a_dec_ctx, frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }

            int n = swr_convert(swr_ctx, &out_buffer, OUT_SAMPLE * 2, (const uint8_t**)frame->data, frame->nb_samples);

            //int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);

            fwrite(out_buffer, 2, n, fp_pcm);
        }
    }
    while (1) {
        pkt->data = NULL;
        pkt->size = 0;
        ret = avcodec_send_packet(a_dec_ctx, pkt);
        if (ret < 0) {
            printf("avcodec_send_packet出错\n");
            break;
        }
        while (1) {
            ret = avcodec_receive_frame(a_dec_ctx, frame);
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
                break;
            }

            int n = swr_convert(swr_ctx, &out_buffer, OUT_SAMPLE * 2, (const uint8_t**)frame->data, frame->nb_samples);

            //int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);
            fwrite(out_buffer, 2, n, fp_pcm);

        }
    }

__FAIL:
    fclose(fp_pcm);
    if (ifmt_ctx) {
        avformat_close_input(&ifmt_ctx);
        avformat_free_context(ifmt_ctx);
    }
    if (out_buffer) {
        av_free(out_buffer);
    }
    if (frame) {
        av_frame_free(&frame);
    }
    if (pkt) {
        av_packet_free(&pkt);
    }
    if (swr_ctx) {
        swr_free(&swr_ctx);
    }
    if (a_dec_ctx) {
        avcodec_close(a_dec_ctx);
        avcodec_free_context(&a_dec_ctx);
    }

源码下载
下一篇介绍如何 把字幕嵌入视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值