在 C++ 中使用 ffmpeg 编码视频时如何设置 moov 原子位置

How to set the moov atom position when encoding video using ffmpeg in c++

我正在使用 C++ 中的 ffmpeg 将一些 h264 视频编码到 mp4 容器中。但是结果视频将 moov 原子(或元数据?)放在视频文件的末尾,这不利于互联网流式传输。 那么如何设置moov atom位置到前面呢?

你需要使用ffmpeg的faststart标志将moov atom放在MP4文件的开头,Here是标志的解释。以编程方式,您需要在输出上下文中设置标志,这是示例代码及其对我的工作,

AVFormatContext *outFormatCtx;

// Write MOOV atom at the begining of the MP4 file
MOVMuxContext *mov = NULL;

mov = (MOVMuxContext *)outFormatCtx->priv_data;
mov->flags |= FF_MOV_FLAG_FASTSTART;

MOVMuxContext 是一个内部 header,不应直接访问。它的实现不是 API 的一部分,它可以改变。 官方的做法是通过 AVDictionary 设置选项:

AVDictionary* options = nullptr;
av_dict_set( &options, "movflags", "faststart", 0 );
avio_open2(..., &options);