ffmpeg7.0 合并2个 aac 文件

ffmpeg7.0 将2个aac文件合并。

复制代码
#include <stdio.h>

// 之所以增加__cplusplus的宏定义,是为了同时兼容gcc编译器和g++编译器
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#ifdef __cplusplus
};
#endif

#pragma warning(disable:4996)

AVFormatContext* pFormatContext[2] = { NULL };
int g_nAudioIndex[2] = {-1};
AVStream* g_pStream[2] = {NULL};
AVCodecContext* g_pCodecContext[2] = {NULL};


bool Init(int nIndex, const char* pFileName)
{
    int nRet = avformat_open_input(&pFormatContext[nIndex], pFileName, NULL, NULL);
    if (nRet == 0) {
        nRet = avformat_find_stream_info(pFormatContext[nIndex], NULL);
        if (nRet >= 0) {
            g_nAudioIndex[nIndex] = av_find_best_stream(pFormatContext[nIndex], AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
            if (g_nAudioIndex[nIndex] >= 0) {
                g_pStream[nIndex] = pFormatContext[nIndex]->streams[g_nAudioIndex[nIndex]];

                AVCodecID nCodecID = g_pStream[nIndex]->codecpar->codec_id;

                const AVCodec* pCodec = avcodec_find_decoder(nCodecID);
                if (pCodec) {
                    g_pCodecContext[nIndex] = avcodec_alloc_context3(pCodec);

                    nRet = avcodec_parameters_to_context(g_pCodecContext[nIndex], g_pStream[nIndex]->codecpar);
                    if (nRet >= 0) {
                        nRet = avcodec_open2(g_pCodecContext[nIndex], pCodec, NULL);
                        if (nRet == 0) {
                            return true;
                        }
                    }
                }
            }
        } 
    }
    return false;
}

void Uninit(int nIndex)
{
    avcodec_close(g_pCodecContext[nIndex]);

    avcodec_free_context(&g_pCodecContext[nIndex]);

    avformat_close_input(&pFormatContext[nIndex]);
    avformat_free_context(pFormatContext[nIndex]);
}

void _WriteFile(int nIndex, FILE* pFile)
{
    AVPacket* pPacket = av_packet_alloc();
    if (pPacket) {
        while (true) {
            int nRet = av_read_frame(pFormatContext[nIndex], pPacket);
            if (nRet < 0) {
                break;
            }

            if (pPacket->stream_index == g_nAudioIndex[nIndex]) {
                fwrite(pPacket->data, 1, pPacket->size, pFile); // AVPacket已经是一个完整的包,包括aac 数据头和aac 数据块
                av_packet_unref(pPacket); // 释放buffer
            }
        }

        av_packet_free(&pPacket);
    }
}

int main(int argc, char** argv)
{
    Init(0, "D:\\123.aac");
    Init(1, "D:\\234.aac");

    FILE* pFile = fopen("d:\\1.aac", "wb");
    _WriteFile(0, pFile);
    _WriteFile(1, pFile);
    fclose(pFile);

    Uninit(0);
    Uninit(1);
    return 0;
}

合并后,用网易云音乐app打开,正常播放,时间轴正常。

相关推荐
我想进大厂33 分钟前
图论---Kruskal(稀疏图)
数据结构·c++·算法·图论
RenderNow1 小时前
深耕ffmpeg系列之AVFrame
ffmpeg
_w_z_j_1 小时前
C++----模拟实现string
开发语言·c++
特立独行的猫a1 小时前
redis客户端库redis++在嵌入式Linux下的交叉编译及使用
linux·数据库·c++·redis·redis客户端库
张槊哲1 小时前
const(C++)
开发语言·c++
oioihoii1 小时前
C++23 中 constexpr 的重要改动
c++·算法·c++23
pystraf2 小时前
UOJ 228 基础数据结构练习题 Solution
数据结构·c++·算法·线段树
牙痛不能吃糖,哭2 小时前
C++面试复习日记(8)2025.4.25,malloc,free和new,delete的区别
开发语言·c++
ChoSeitaku3 小时前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
软行4 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode