ffmpeg把pcm编码为aac

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::EncodePCMToAAC(const char *pcm_file, const char *aac_file)
{
    AVFormatContext *fmt_ctx = nullptr;
    const AVOutputFormat *out_fmt = nullptr;
    int ret = -1;
    AVStream *avstream = nullptr;
    const AVCodec *codec = nullptr;
    AVCodecContext *codec_ctx = nullptr;
    AVFrame *avframe = nullptr;       // pcm file对应的avframe
    AVFrame *codec_avframe = nullptr; // 编码器接收的avframe
    AVPacket *avpacket = nullptr;
    int frame_data_bytes = 0;
    FILE *in_fp = nullptr;
    size_t n = 0;
    int64_t pts = 0;
    struct SwrContext *swr_ctx = nullptr; // 音频帧转换器
    AVChannelLayout src_channel_layout;
    AVChannelLayout dest_channel_layout;

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

    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->sample_rate = 44100;
    codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
    codec_ctx->channels = av_get_channel_layout_nb_channels(codec_ctx->channel_layout);
    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;
    }
    printf("codec_ctx->frame_size:%d\n", codec_ctx->frame_size);

    codec_avframe = av_frame_alloc();
    if (!codec_avframe)
    {
        printf("av_frame_alloc error\n");
        goto END;
    }
    codec_avframe->format = codec_ctx->sample_fmt; // pcm的采样格式
    codec_avframe->sample_rate = codec_c
### 如何在 Qt 中使用 FFmpegPCM 音频文件换为 AAC 格式 要实现将 PCM 文件换为 AAC 的功能,可以利用 FFmpeg 提供的强大音视频处理能力。以下是详细的说明: #### 使用 FFmpeg 进行音频编码 FFmpeg 是一个多用途工具库,支持多种多媒体格式之间的换。通过调用其命令行接口或者直接集成到应用程序中,能够完成复杂的媒体操作。 对于将 PCM 换为 AAC 的过程,主要涉及以下几个方面: - 初始化输入流并读取原始 PCM 数据。 - 设置输出参数以指定目标编解码器(此处为 AAC 编解码器)以及比特率等配置项。 - 执行实际的数据压缩与写入磁盘的操作[^1]。 #### 实现步骤概述 下面是一个基于 C++ 和 Qt 平台下嵌套 FFmpeg 库来执行上述任务的例子代码片段: ```cpp #include <QCoreApplication> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); AVCodec* codec; AVCodecContext* c= NULL; int i, out_size, size, x, y; av_register_all(); /* find the MP2 encoder */ codec = avcodec_find_encoder(AV_CODEC_ID_AAC); if (!codec) { fprintf(stderr, "codec not found\n"); exit(1); } c= avcodec_alloc_context3(codec); c->bit_rate = 64000; // Set bit rate. c->sample_fmt = AV_SAMPLE_FMT_FLTP; // Specify sample format (floating point planar). c->sample_rate = 44100; // Define sampling frequency as per requirement. c->channels = 2; // Mono or stereo channel count. /* open it */ if (avcodec_open2(c, codec, nullptr) < 0){ fprintf(stderr,"could not open codec\n"); return -1 ; } uint8_t* buffer; int bufferSize = av_samples_get_buffer_size(NULL,c->channels, c->frame_size,c->sample_fmt,1); buffer=(uint8_t*)malloc(bufferSize); while(get_input_data(&buffer,&size)){ send_packet_to_codec(context,packet); receive_encoded_frames(output_file); } free(buffer); avcodec_close(c); av_free(c); } ``` 此段程序展示了如何初始化一个简单的 AAC 编码环境,并准备好了用于存储已编码样本帧的缓冲区。注意这里省略了一些细节函数定义比如 `get_input_data`, 它们应该依据具体应用场景定制开发[^2]. #### 关键点解释 - **AVSampleFormat**: 表示采样数据的形式,在本例中选择了浮点型平面布局(`AV_SAMPLE_FMT_FLTP`),这是许多现代软件合成器使用的标准形式之一。 - **Frame Size & Buffer Management**: 计算所需内存大小以便容纳单个音频帧的所有样品值;这一步骤非常重要因为不当分配可能导致越界访问错误甚至崩溃应用进程。 #### 注意事项 当把这样的逻辑融入 GUI 框架如 Qt 当中的时候,请务必考虑线程安全问题,尤其是如果打算让长时间运行的任务不影响界面响应的话,则需采用多线程机制分离耗时计算部分同 UI 更新控制之外独立运作[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值