ffmpeg把pcm编码为mp3

version

#define LIBSWRESAMPLE_VERSION_MAJOR 2

#define LIBSWRESAMPLE_VERSION_MINOR 9

#define LIBSWRESAMPLE_VERSION_MICRO 100

#define LIBAVCODEC_VERSION_MINOR 31

#define LIBAVCODEC_VERSION_MICRO 102

code

复制代码
void CFfmpegOps::EncodePCMToMP3(const char *pcm_file, const char *mp3_file)
{
    if ((!pcm_file) || (!mp3_file))
    {
        return;
    }

    FILE *in_fp = nullptr;
    FILE *out_fp = nullptr;
    AVCodecContext *codec_ctx = nullptr;
    const AVCodec *codec = nullptr;
    AVFrame *pcm_avframe = nullptr;   // pcm file里的AVFrame
    AVFrame *codec_avframe = nullptr; // 编码器接受的AVFrame
    int pcm_data_bytes = 0;
    AVPacket *mp3_avpacket = nullptr;
    int ret = -1;
    size_t n = 0;
    int sample_fmt_is_planar = 0;
    int64_t pts = 0;
    struct SwrContext *swr_ctx = nullptr; // 音频帧转换器
    AVChannelLayout src_channel_layout;
    AVChannelLayout dest_channel_layout;

    in_fp = fopen(pcm_file, "rb");
    if (!in_fp)
    {
        printf("fopen error\n");
        goto END;
    }

    out_fp = fopen(mp3_file, "wb");
    if (!out_fp)
    {
        printf("fopen error\n");
        goto END;
    }

    codec = avcodec_find_encoder(AV_CODEC_ID_MP3);
    if (!codec)
    {
        printf("avcodec_find_encoder error\n");
        goto END;
    }

    // mp3编码器是否支持变化的采样样本数量
    if (codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
    {
        printf("codec AV_CODEC_CAP_VARIABLE_FRAME_SIZE\n");
    }

    codec_ctx = avcodec_alloc_context3(codec);
    if (!codec_ctx)
    {
        printf("avcodec_alloc_context3 error\n");
        goto END;
    }
    codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
    codec_ctx->sample_rate = 44100;
    codec_ctx->channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);
    codec_ctx->bit_rate = 128000;
    codec_ctx->time_base.num = 1;
    codec_ctx->time_base.den = codec_ctx->sample_rate;

    // 打开编码器
    ret = avcodec_open2(codec_ctx, codec, nullptr);
    if (ret < 0)
    {
        printf("avcodec_open2 error:%s\n", GetFfmpegERR(ret));
        goto END;
    }

    codec_avframe = av_frame_alloc();
    if (!codec_avframe)
    {
        printf("av_frame_alloc error\n");
        goto END;
    }
    codec_avframe->format = codec_ctx->sample_fmt;
    codec_avframe->sample_rate = codec_ctx->sample_rate;
    codec_avframe->channel_layout = codec_ctx->channel_layout;
    codec_avframe->channels = codec_ctx->channels;
    codec_avframe->nb_samples = codec_ctx->frame_size;

    ret = av_frame_get_buffer(codec_avframe, 0);
    if (ret < 0)
    {
        printf("av_frame_get_buffer error(%s)\n", CFfmpegOps::GetFfmpegERR(ret));
        goto END;
    }

    pcm_avframe = av_frame_alloc();
    if (!pcm_avframe)
    {
        printf("av_frame_alloc error\n");
        goto END;
    }
    pcm_avframe->format = AV_SAMPLE_FMT_FLT;
    pcm_avframe->sample_rate = 44100;
    pcm_avframe->channel_layout = AV_CH_LAYOUT_STEREO;
    pcm_avframe->channels = av_get_channel_layout_nb_channels(pcm_avframe->channel_layout);
    pcm_avframe->nb_samples = 1024;

    // 一帧pcm的相关参数计算
    pcm_data_bytes = av_get_bytes_per_sample((AVSampleFormat)(pcm_avframe->format)) * pcm_avframe->channels * pcm_avframe->nb_samples;
    ret = av_samples_get_buffer_size(nullptr,
                                     pcm_avframe->channels,
                                     pcm_avframe->nb_samples,
                                     (AVSampleFormat)(pcm_avframe->format),
                                     0);
    if (ret != pcm_data_bytes)
    {
        printf("pcm_data_bytes != av_samples_get_buffer_size\n");
        goto END;
    }

    ret = av_frame_get_buffer(pcm_avframe, 0);
    if (ret < 0)
    {
        printf("av_frame_get_buffer error(%s)\n", CFfmpegOps::GetFfmpegERR(ret));
        goto END;
    }

    mp3_avpacket = av_packet_alloc();
    if (!mp3_avpacket)
    {
        printf("av_packet_alloc error\n");
        goto END;
    }

    ret = av_channel_layout_from_mask(&src_channel_layout, pcm_avframe->channel_layout);
    if (ret < 0)
    {
        printf("av_channel_layout_from_mask error(%s)\n", GetFfmpegERR(ret));
        goto END;
    }

    ret = av_channel_layout_from_mask(&dest_channel_layout, codec_avframe->channel_layout);
    if (ret < 0)
    {
        printf("av_channel_layout_from_mask error(%s)\n", GetFfmpegERR(ret));
        goto END;
    }

    ret = swr_alloc_set_opts2(&swr_ctx,
                              &dest_channel_layout, (AVSampleFormat)(codec_avframe->format), codec_avframe->sample_rate,
                              &src_channel_layout, (AVSampleFormat)(pcm_avframe->format), pcm_avframe->sample_rate,
                              0, nullptr);
    if (ret < 0)
    {
        printf("swr_alloc_set_opts2 error(%s)\n", GetFfmpegERR(ret));
        goto END;
    }

    ret = swr_init(swr_ctx);
    if (ret < 0)
    {
        printf("swr_init error(%s)\n", GetFfmpegERR(ret));
        goto END;
    }

    while (1)
    {
        if (feof(in_fp))
        {
            printf("feof\n");
            break;
        }

        sample_fmt_is_planar = av_sample_fmt_is_planar((AVSampleFormat)(pcm_avframe->format));

        if (sample_fmt_is_planar)
        {
            n = fread(pcm_avframe->data[0], sizeof(uint8_t), pcm_data_bytes / 2, in_fp);
            if ((int)n != (pcm_data_bytes / 2))
            {
                printf("n != pcm_data_bytes/2\n");
            }

            n = fread(pcm_avframe->data[1], sizeof(uint8_t), pcm_data_bytes / 2, in_fp);
            if ((int)n != (pcm_data_bytes / 2))
            {
                printf("n != pcm_data_bytes/2\n");
            }
        }
        else
        {
            n = fread(pcm_avframe->data[0], sizeof(uint8_t), pcm_data_bytes, in_fp);
            if ((int)n != pcm_data_bytes)
            {
                printf("n != bytes_per_frame\n");
            }
        }

#if 0
        ret = swr_convert_frame(swr_ctx, codec_avframe, pcm_avframe);
        if (ret < 0)
        {
            printf("swr_convert_frame error(%s)\n", GetFfmpegERR(ret));
            goto END;
        }
#else
        ret = swr_convert(swr_ctx, 
                        (uint8_t**)(codec_avframe->data), codec_avframe->nb_samples, 
                        (const uint8_t**)(pcm_avframe->data), pcm_avframe->nb_samples);
        if (ret < 0)
        {
            printf("swr_convert error(%s)\n", GetFfmpegERR(ret));
            goto END;
        }
#endif

        codec_avframe->pts = pts;
        pts++;

        ret = avcodec_send_frame(codec_ctx, codec_avframe);
        if (ret < 0)
        {
            if (ret == AVERROR(EAGAIN))
            {
                printf("read output first\n");
            }
            else
            {
                printf("avcodec_send_frame error(%s)\n", GetFfmpegERR(ret));
                goto END;
            }
        }
        else if (ret == 0)
        {
            // printf("avcodec_send_frame succeed\n");
        }

        while (1)
        {
            ret = avcodec_receive_packet(codec_ctx, mp3_avpacket);
            if (ret < 0)
            {
                if (ret == AVERROR(EAGAIN))
                {
                    printf("send input first\n");
                    break;
                }
                else
                {
                    printf("avcodec_receive_packet error(%s)\n", GetFfmpegERR(ret));
                    break;
                }
            }
            else if (ret == 0)
            {
                // printf("avcodec_receive_packet succeed\n");

                n = fwrite(mp3_avpacket->data, sizeof(uint8_t), mp3_avpacket->size, out_fp);
                if ((int)n != mp3_avpacket->size)
                {
                    printf("n != mp3_avpacket->size\n");
                }

                av_packet_unref(mp3_avpacket);
            }
        }
    }

    ret = avcodec_send_frame(codec_ctx, nullptr);
    while (1)
    {
        ret = avcodec_receive_packet(codec_ctx, mp3_avpacket);
        if (ret < 0)
        {
            if (ret == AVERROR(EAGAIN))
            {
                break;
            }
            else
            {
                break;
            }
        }
        else if (ret == 0)
        {
            n = fwrite(mp3_avpacket->data, sizeof(uint8_t), mp3_avpacket->size, out_fp);
            av_packet_unref(mp3_avpacket);
        }
    }

END:
    if (swr_ctx)
    {
        swr_free(&swr_ctx);
        swr_ctx = nullptr;
    }

    if (mp3_avpacket)
    {
        av_packet_free(&mp3_avpacket);
        mp3_avpacket = nullptr;
    }

    if (codec_avframe)
    {
        av_frame_free(&codec_avframe);
        codec_avframe = nullptr;
    }

    if (pcm_avframe)
    {
        av_frame_free(&pcm_avframe);
        pcm_avframe = nullptr;
    }

    if (codec_ctx)
    {
        avcodec_free_context(&codec_ctx);
        codec_ctx = nullptr;
    }

    if (out_fp)
    {
        fclose(out_fp);
        out_fp = nullptr;
    }

    if (in_fp)
    {
        fclose(in_fp);
        in_fp = nullptr;
    }
}

performance

相关推荐
·薯条大王7 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
hansang_IR12 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘12 小时前
day7.c++继承
c++
沫璃染墨13 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌13 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
醉城夜风~13 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
烟花落o14 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门
liulilittle14 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光14 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
我不会插花弄玉16 小时前
15.map,set下:AVL树,红黑树和map,set的封装【由浅入深-C++】
c++·stl