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

2

5

I'm encoding some h264 video into a mp4 container using ffmpeg in c++. But the result videos place the moov atom(or metadata?) at the end of the video file, it's bad for internet streaming. So how can I set the moov atom position to the front?

Grice answered 27/2, 2015 at 8:40 Comment(0)
A
2

You need to use faststart flag of ffmpeg to place the moov atom in the beginning of the MP4 file, Here is the explanation of the flag. Programatically you need to set the flag in output context, here is the sample code and its working for me,

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;
Askari answered 27/2, 2015 at 9:27 Comment(4)
Hi pragnesh Thanks for your help, but I can't find MOVMuxContext data structure and FF_MOV_FLAG_FASTSTART flag in my ffmpeg library, they are marked "undefined". Which version are you using?Grice
Mine was ffmpeg-1.0 and those things are defined in "libavformat/movenc.h" file. It should be there in your version as well may be you need to enable all the codecs in configure.Askari
I don't have the header file. I will try to recompile my ffmpeg library. Thanks!Grice
Yes, try to keep everything enabled in configureAskari
S
5

MOVMuxContext is an internal header and should not be accessed directly. Its implementation is not part of the API and it can change. The official way to do it is setting options via an AVDictionary :

AVDictionary* options = nullptr;
av_dict_set( &options, "movflags", "faststart", 0 );
avio_open2(..., &options);
Spancel answered 18/12, 2017 at 23:52 Comment(0)
A
2

You need to use faststart flag of ffmpeg to place the moov atom in the beginning of the MP4 file, Here is the explanation of the flag. Programatically you need to set the flag in output context, here is the sample code and its working for me,

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;
Askari answered 27/2, 2015 at 9:27 Comment(4)
Hi pragnesh Thanks for your help, but I can't find MOVMuxContext data structure and FF_MOV_FLAG_FASTSTART flag in my ffmpeg library, they are marked "undefined". Which version are you using?Grice
Mine was ffmpeg-1.0 and those things are defined in "libavformat/movenc.h" file. It should be there in your version as well may be you need to enable all the codecs in configure.Askari
I don't have the header file. I will try to recompile my ffmpeg library. Thanks!Grice
Yes, try to keep everything enabled in configureAskari

© 2022 - 2024 — McMap. All rights reserved.