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打开,正常播放,时间轴正常。

相关推荐
R-G-B1 小时前
【07】OpenCV C++实战篇——鼠标在图片上绘制矩形,计算矩形区域内灰度值的累加值显示在图片上,支持连续多次框选,快速计算结果,快速刷新画面不卡顿
c++·opencv·鼠标在图片上绘制矩形·矩形区域内灰度值总和·计算矩形区域内灰度值·矩形区域灰度值累加和·鼠标在图片上连续多次框选矩形
王江奎6 小时前
FFmpeg 视频旋转信息处理:3.4 vs 7.0.2
ffmpeg·音视频
superxxd9 小时前
基于ffmpeg和rk3588的mpp编解码库多路融屏程序设计
ffmpeg
草莓熊Lotso15 小时前
【洛谷题单】--分支结构(二)
c语言·c++·经验分享·其他·刷题
snowfoootball15 小时前
2025 蓝桥杯C/C++国B 部分题解
c语言·c++·笔记·学习·贪心算法·蓝桥杯
爱吃芒果的蘑菇17 小时前
使用pybind11封装C++API
开发语言·c++·python
汉汉汉汉汉20 小时前
C++中的继承:从基础到复杂
c++
aqi0020 小时前
FFmpeg开发笔记(七十九)专注于视频弹幕功能的国产弹弹播放器
android·ffmpeg·音视频·直播·流媒体
科大饭桶21 小时前
Linux系统编程Day9 -- gdb (linux)和lldb(macOS)调试工具
linux·服务器·c语言·c++
黑色的山岗在沉睡21 小时前
【无标题】
数据结构·c++·算法·图论