【FFmpeg】音视频MP4封装格式转封装MOV

一.源码总览

cpp 复制代码
#include <iostream>
extern "C"
{
#include <libavformat/avformat.h>
}

int main()
{
    //1.打开输入文件
    const char* infile = "E:/videos/test.mp4";
    char outfile[] = "E:/videos/test.mov";
    AVFormatContext* ic = nullptr;
    int ret = avformat_open_input(&ic, infile, NULL, NULL);
    if (ret < 0)
    {
        char errbuf[1024];
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << "avformat_open_input failed : " << errbuf << std::endl;
        return -1;
    }

    // 扒开包裹看详细参数
    ret = avformat_find_stream_info(ic, nullptr);
    if (ret < 0)
    {
        std::cout << "获取流信息失败" << std::endl;
        avformat_close_input(&ic);
        return -1;
    }

    //2.创建输出上下文
    AVFormatContext* oc = nullptr;
    ret = avformat_alloc_output_context2(&oc,NULL,NULL,outfile);
    if (ret < 0)
    {
        char errbuf[1024];
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << "avformat_alloc_output_context2 failed : " << errbuf << std::endl;
        return -1;
    }

    //3.添加流
    AVStream* videoStream = avformat_new_stream(oc, NULL);
    AVStream* audioStream = avformat_new_stream(oc, NULL);

    //4.复制参数
    avcodec_parameters_copy(videoStream->codecpar, ic->streams[0]->codecpar);
    avcodec_parameters_copy(audioStream->codecpar, ic->streams[1]->codecpar);

    //5.标签清零
    videoStream->codecpar->codec_tag = 0;
    audioStream->codecpar->codec_tag = 0;
    av_dump_format(ic, 0, infile, 0);
    std::cout << "=====================================" << std::endl;
    av_dump_format(oc, 0, outfile, 1);

    //6.打开输出文件io,写入头部信息
    ret = avio_open(&oc->pb, outfile, AVIO_FLAG_WRITE);
    if (ret < 0)
    {
        std::cout << "avio open failed!" << std::endl;
        return -1;
    }

    //写入头
    ret = avformat_write_header(oc, NULL);
    if (ret < 0)
    {
        char errbuf[1024];
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << "avformat_write_header failed: " << errbuf << std::endl;
    }

    //写入帧数据
    AVPacket* pkt = av_packet_alloc();
    for (;;)
    {
        int re = av_read_frame(ic, pkt);        //读取帧
        if (re < 0)
            break;

        //根据time_base不同,进行转换
        pkt->pts = av_rescale_q_rnd(pkt->pts,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );
        pkt->dts = av_rescale_q_rnd(pkt->dts,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );
        pkt->pos = -1;
        pkt->duration = av_rescale_q_rnd(pkt->duration,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );

        av_interleaved_write_frame(oc, pkt);    //写入帧
        av_packet_unref(pkt);
    }
    av_write_trailer(oc);

    av_packet_free(&pkt);
    avio_close(oc->pb);
    avformat_close_input(&ic);
    avformat_free_context(oc);

    return 0;
}

二、流程拆解

第一步:打开输入文件,摸底源文件 (输入管家)

在内存中建立"输入大管家"。它像 X 光机一样扫描 MP4,摸清了里面有 H.264 视频轨和 AAC 音频轨,拿到了第一手资料。

对应代码块:

cpp 复制代码
    //1.打开输入文件
    const char* infile = "E:/videos/test.mp4";
    char outfile[] = "E:/videos/test.mov";
    AVFormatContext* ic = nullptr;
    int ret = avformat_open_input(&ic, infile, NULL, NULL);
    if (ret < 0)
    {
        char errbuf[1024];
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << "avformat_open_input failed : " << errbuf << std::endl;
        return -1;
    }

    // 扒开包裹看详细参数
    ret = avformat_find_stream_info(ic, nullptr);
    if (ret < 0)
    {
        std::cout << "获取流信息失败" << std::endl;
        avformat_close_input(&ic);
        return -1;
    }

第二步:创建输出文件上下文凭空捏造图纸 (输出空壳)

在内存中新建一个针对 MOV 格式的"输出空壳"。注意:此时这只是个内存图纸,硬盘上依然什么都没有发生。

对应代码块:

cpp 复制代码
    //2.创建输出上下文
    AVFormatContext* oc = nullptr;
    ret = avformat_alloc_output_context2(&oc,NULL,NULL,outfile);
    if (ret < 0)
    {
        char errbuf[1024];
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << "avformat_alloc_output_context2 failed : " << errbuf << std::endl;
        return -1;
    }

第三步:克隆轨道数据

根据旧管家探明的参数,在新管家这里铺设对应的视频/音频轨道,并 1:1 复印参数说明书。清除旧标签,准备生成新暗号。

对应代码块:

cpp 复制代码
    //3.添加流
    AVStream* videoStream = avformat_new_stream(oc, NULL);
    AVStream* audioStream = avformat_new_stream(oc, NULL);

    //4.复制参数
    avcodec_parameters_copy(videoStream->codecpar, ic->streams[0]->codecpar);
    avcodec_parameters_copy(audioStream->codecpar, ic->streams[1]->codecpar);

    //5.标签清零
    videoStream->codecpar->codec_tag = 0;
    audioStream->codecpar->codec_tag = 0;

第四步:点亮硬盘,写入文件头

关键时刻!打通内存到硬盘的 I/O 管道,在硬盘真正创建 test.mov 文件,并把图纸上的"轨道参数说明书"写在文件的最头部。

对应代码块:

cpp 复制代码
   //6.打开输出文件io,写入头部信息
   ret = avio_open(&oc->pb, outfile, AVIO_FLAG_WRITE);
   if (ret < 0)
   {
       std::cout << "avio open failed!" << std::endl;
       return -1;
   }

   //写入头
   ret = avformat_write_header(oc, NULL);
   if (ret < 0)
   {
       char errbuf[1024];
       av_strerror(ret, errbuf, sizeof(errbuf));
       std::cout << "avformat_write_header failed: " << errbuf << std::endl;
   }

第五步:死循环搬运与时间换算

买一个"结实的空信封(Packet)"。不断从旧管家摸出数据,经过"时间比例尺"的精准换算,变成 MOV 的时间,再交织写入新文件,最后倒空信封复用。

这里由于MP4与MOV的时间基数(time_base)不同,需要换算一下pts,bts,duration等。(具体自行查询)

对应代码块:

cpp 复制代码
    //写入帧数据
    AVPacket* pkt = av_packet_alloc();
    for (;;)
    {
        int re = av_read_frame(ic, pkt);        //读取帧
        if (re < 0)
            break;

        //根据time_base不同,进行转换
        pkt->pts = av_rescale_q_rnd(pkt->pts,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );
        pkt->dts = av_rescale_q_rnd(pkt->dts,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );
        pkt->pos = -1;
        pkt->duration = av_rescale_q_rnd(pkt->duration,
            ic->streams[pkt->stream_index]->time_base,
            oc->streams[pkt->stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
        );

        av_interleaved_write_frame(oc, pkt);    //写入帧
        av_packet_unref(pkt);
    }

第六步:写入文件尾,全剧终

极其致命的收尾!把最重要的轨道索引表写入文件尾端(救活文件)。然后销毁信封、拔掉管道、拆除所有内存管家,完美退场。

代码:

cpp 复制代码
av_write_trailer(oc);
相关推荐
hu55667985 小时前
FFmpeg 如何合并字幕
ffmpeg
VOOHU-沃虎5 小时前
沃虎电子:音频变压器在信号隔离与音频接口中的选型与应用解析
算法·音视频
Likeadust5 小时前
智能会议管理系统EasyDSS构建企业视频全场景解决方案
人工智能·音视频
屋檐上的大修勾5 小时前
使用ffmpeg本地发布rtmp/rtsp直播流
ffmpeg
紫金修道5 小时前
【编解码】基于CPU的高性能 RTSP 多路摄像头抓帧插件:设计与实现详解
ffmpeg
雄哥0075 小时前
Windows系统下FFmpeg的安装与环境配置指南
windows·ffmpeg
ALONE_WORK5 小时前
ffmpeg-rk3568-mpp 硬件加速版本
ffmpeg·视频编解码·mpp·视频推流
墨染倾城殇5 小时前
FSC-BW5028MV适配车载多场景方案:WiFi7+蓝牙5.4 让音频与数据并发稳定输出
网络·音视频·wifi 7·蓝牙5.4·车载蓝牙模块
紫金修道1 天前
【编解码】RK3588 平台基于 FFmpeg RKMPP 硬解的多路 RTSP 抓帧插件实战
ffmpeg·rkmpp
电子科技圈1 天前
赋能高端音频功能促进多样化设备创新——XMOS USB Audio平台实现四大功能升级
人工智能·mcu·音视频·智能家居·边缘计算·语音识别·智能硬件