FFmpeg 音頻解碼

基礎(chǔ)知識(shí)預(yù)備

  • 物體震動(dòng)引起空氣震動(dòng),震動(dòng)波形在空氣中傳播到耳朵的時(shí)候,造成耳膜的震動(dòng),從而接受到聲音信號(hào),自然界中的聲音是基于時(shí)間的連續(xù)信號(hào),屬于模擬信號(hào),聲音的三要素,音色由由波形決定,音高由振幅決定,音高由頻率決定,但是模擬信號(hào)傳輸不方便,需要把它轉(zhuǎn)化為數(shù)字信號(hào),根據(jù)奈奎斯特采樣定律,只要采樣頻率高于原信號(hào)最高頻率的兩倍就能把數(shù)字信號(hào)還原為模擬信號(hào),從而不會(huì)造成太多的失真,人類的耳朵能感知到的頻率范圍為2khz~20khz,所以一般使用的采樣頻率為44.1khz
  • 模擬信號(hào)到數(shù)字信號(hào)的過(guò)程經(jīng)過(guò)了量化,采樣,和調(diào)制,人類對(duì)某些部分的聲音不敏感,所以去掉這些部分的聲音能有效減少音頻文件的大小而不會(huì)對(duì)音頻質(zhì)量有大的影響,經(jīng)過(guò)處理的壓縮文件....
 //注冊(cè)所有組件
    av_register_all();
    //打開文件封裝格式,需要先獲取文件封裝格式上下文
    AVFormatContext * avFormat_context =  avformat_alloc_context();
    const char * cinFilePath = [inputFilePath UTF8String];
    if (avformat_open_input(&avFormat_context,cinFilePath , NULL, NULL) != 0) {
        NSLog(@"打開文件失敗");
        return;
    }
    //找到對(duì)應(yīng)的流(音頻流, 視頻流, 字幕流)
    if(avformat_find_stream_info(avFormat_context, NULL) < 0){
        NSLog(@"查找失敗");
        return;
    }
     int av_stream_index = -1;
        //查找音頻解碼器
    for (int i = 0; i < avFormat_context->nb_streams; i++) {
            if (avFormat_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
                av_stream_index = i;
            }
    }
    //音頻解碼器上下文
    AVCodecContext * av_codec_context = avFormat_context->streams[av_stream_index]->codec;
    
    //獲取解碼器
    AVCodec * av_codec = avcodec_find_decoder(av_codec_context->codec_id);
    
    //打開解碼器
    if (avcodec_open2(av_codec_context, av_codec, NULL) != 0) {
        NSLog(@"打開解碼器失敗");
        return ;
    }
    NSLog(@"---解碼器名稱:%s",av_codec->name);
    //循環(huán)讀取數(shù)據(jù)
    AVPacket * avPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
    AVFrame * avFrame = av_frame_alloc();
    
    //初始化p音頻采樣數(shù)據(jù)上下文
    SwrContext * swrContext = swr_alloc();
    //設(shè)置默認(rèn)配置
    int64_t in_ch_layout = av_get_default_channel_layout(av_codec_context->channels);
    //設(shè)置采樣精度
    swr_alloc_set_opts(swrContext, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, av_codec_context->sample_rate, in_ch_layout, av_codec_context->sample_fmt, av_codec_context->sample_rate, 0, NULL);
    swr_init(swrContext);
    
    int MAX_AUDIO_SIZE  = 44100 * 2;
    uint8_t * out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_SIZE);
    //獲取緩沖區(qū)事假大小
    int out_nb_channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
    //打開文件
    const char * outfile = [outFilepath UTF8String];
    FILE * file_pcm = fopen(outfile, "wb+");
    if (file_pcm == NULL) {
        NSLog(@"打開文件失敗");
        return;
    }
    
    int current_index = 0;
    while (av_read_frame(avFormat_context, avPacket) >= 0) {
        //判斷這幀數(shù)據(jù)的類型
        if (avPacket->stream_index == av_stream_index) {
            avcodec_send_packet(av_codec_context, avPacket);
            int ret = avcodec_receive_frame(av_codec_context, avFrame);
            if (ret == 0) {//轉(zhuǎn)碼成功
                swr_convert(swrContext, &out_buffer, MAX_AUDIO_SIZE, (const uint8_t **)avFrame->data, avFrame->nb_samples);
                //獲取緩沖區(qū)實(shí)際大小
                int buffer_size = av_samples_get_buffer_size(NULL, out_nb_channels, avFrame->nb_samples, av_codec_context->sample_fmt, 1);
                fwrite(out_buffer, 1, buffer_size, file_pcm);
               // float progress = current_index ;
                current_index++;
                NSLog(@"當(dāng)前解碼到了第%d幀",current_index);
            }
        }
    }
    completionBlock(nil, true);
    //釋放資源
    av_packet_free(&avPacket);
    fclose(file_pcm);
    av_frame_free(&avFrame);
    free(out_buffer);
    avcodec_close(av_codec_context);
  //  avcodec_close(vidio_codec_context);
    avformat_free_context(avFormat_context);
運(yùn)行代碼,解碼成功
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容