天天看點

FFmpeg從視訊中提取音頻

FFmpeg從視訊中提取音頻

#include <iostream>



extern "C" {
#include <libavutil/log.h>
#include <libavformat/avformat.h>
}




enum class AUDIO_OP_ERRORS {
    NO_ERROR,   // 0, 無錯誤
    OPEN_FILE_ERROR, // 1, 打開檔案錯誤
    OPEN_INPUT_VIDEO_ERROR, // 2, 打開輸入的視訊檔案出錯
    FIND_STREAM_ERROR, // 3, 查找流資訊失敗錯誤
    FIND_AUDIO_ERROR, // 4, 查找音頻流失敗
};

struct AudioOpWrapper {
    // 輸出檔案句柄
    FILE *dst_fd{ nullptr };
    // 輸入檔案格式
    AVFormatContext* fmt_ctx{ nullptr };
    // 每次讀取的音頻包
    AVPacket pkt{};

    ~AudioOpWrapper() {
        if (pkt.buf != nullptr) {
            av_packet_unref(&pkt);
        }
        if (dst_fd) {
            fclose(dst_fd);
        }
        if (fmt_ctx) {
            avformat_close_input(&fmt_ctx);
        }
    }
};


// aac每幀開頭都要填寫對應的格式資訊
void adts_header(char *szAdtsHeader, int dataLen) {

    // 不同視訊的音頻流參數可能不一樣
    int audio_object_type = 2;  // 2是aac LC
    int sampling_frequency_index = 4;  // 采樣頻率 44100是0x4
    int channel_config = 2; // stero是雙聲道,左聲道+右聲道

    int adtsLen = dataLen + 7;

    szAdtsHeader[0] = 0xff;         //syncword:0xfff                          高8bits
    szAdtsHeader[1] = 0xf0;         //syncword:0xfff                          低4bits
    szAdtsHeader[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
    szAdtsHeader[1] |= (0 << 1);    //Layer:0                                 2bits 
    szAdtsHeader[1] |= 1;           //protection absent:1                     1bit

    szAdtsHeader[2] = (audio_object_type - 1) << 6;            //profile:audio_object_type - 1                      2bits
    szAdtsHeader[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index  4bits 
    szAdtsHeader[2] |= (0 << 1);                             //private bit:0                                      1bit
    szAdtsHeader[2] |= (channel_config & 0x04) >> 2;           //channel configuration:channel_config               高1bit

    szAdtsHeader[3] = (channel_config & 0x03) << 6;     //channel configuration:channel_config      低2bits
    szAdtsHeader[3] |= (0 << 5);                      //original:0                               1bit
    szAdtsHeader[3] |= (0 << 4);                      //home:0                                   1bit
    szAdtsHeader[3] |= (0 << 3);                      //copyright id bit:0                       1bit  
    szAdtsHeader[3] |= (0 << 2);                      //copyright id start:0                     1bit
    szAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11);           //frame length:value   高2bits

    szAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);     //frame length:value    中間8bits
    szAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5);       //frame length:value    低3bits
    szAdtsHeader[5] |= 0x1f;                                 //buffer fullness:0x7ff 高5bits
    szAdtsHeader[6] = 0xfc;
}


void extract_audio(const std::string& src_video, const std::string& dst_audio,
    AUDIO_OP_ERRORS& err) {

    AudioOpWrapper op_wrapper;
    int err_code = 0;
    char errors[1024];

    // 打開輸出檔案
    op_wrapper.dst_fd = fopen(dst_audio.c_str(), "wb");
    if (!op_wrapper.dst_fd) {
        av_log(nullptr, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_audio.c_str());
        err = AUDIO_OP_ERRORS::OPEN_FILE_ERROR;
        return;
    }
    // 打開輸入的視訊檔案,配置設定檔案操作上下文
    if ((err_code = avformat_open_input(&op_wrapper.fmt_ctx, src_video.c_str(), nullptr, nullptr)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(nullptr, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",
            src_video.c_str(),
            err_code,
            errors);
        err = AUDIO_OP_ERRORS::OPEN_INPUT_VIDEO_ERROR;
        return;
    }

    // 查找流資訊
    if ((err_code = avformat_find_stream_info(op_wrapper.fmt_ctx, nullptr)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
            src_video.c_str(),
            err_code,
            errors);

        err = AUDIO_OP_ERRORS::FIND_STREAM_ERROR;
        return;
    }

    // 顯示視訊流資訊
    av_dump_format(op_wrapper.fmt_ctx, 0, src_video.c_str(), 0);

    // 初始化音頻流解析包
    av_init_packet(&op_wrapper.pkt);
    op_wrapper.pkt.data = nullptr;
    op_wrapper.pkt.size = 0;

    // 查找最好的音頻流
    int audio_stream_index = av_find_best_stream(op_wrapper.fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
    if (audio_stream_index < 0) {
        av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",
            av_get_media_type_string(AVMEDIA_TYPE_AUDIO),
            src_video.c_str());
        err = AUDIO_OP_ERRORS::FIND_AUDIO_ERROR;
        return;
    }

    int len = 0;
    // 逐個包讀取,加上adts頭,然後寫入目标檔案
    while ((av_read_frame(op_wrapper.fmt_ctx, &op_wrapper.pkt)) >= 0) {
        // 剛好是音頻流
        if (op_wrapper.pkt.stream_index == audio_stream_index) {
            char adts_header_buf[7];
            adts_header(adts_header_buf, op_wrapper.pkt.size);
            fwrite(adts_header_buf, 1, 7, op_wrapper.dst_fd);
            len = fwrite(op_wrapper.pkt.data, 1, op_wrapper.pkt.size, op_wrapper.dst_fd);
            if (len != op_wrapper.pkt.size) {
                av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
                    len,
                    op_wrapper.pkt.size);
            }
        }
    }
}

/*
* 從多媒體檔案中抽取媒體資訊
* */

int main(int argc, char *argv[]) {

    const std::string src_video="D:\\BaiduNetdiskDownload\\165.mp4";
    const std::string dst_audio="D:\\BaiduNetdiskDownload\\165.mp3";
    AUDIO_OP_ERRORS err = AUDIO_OP_ERRORS::NO_ERROR;

    extract_audio(src_video, dst_audio, err);

    if (err != AUDIO_OP_ERRORS::NO_ERROR) {
        std::cout << "抽取音頻檔案失敗" << std::endl;
        return false;
    }

    return 0;
}      

效果如下:

FFmpeg從視訊中提取音頻