[Libav-user] movflags/faststart flag for avcodec_open2

Jodon Karlik jodonk at gmail.com
Thu May 3 03:45:48 EEST 2018

> Hello,
> I am trying to ensure that I have the moov metadata for H.264 encoded
> video at the beginning of the file. I have seen examples online that
> indicate I need to give ffmpeg the "-movflags +faststart"  option, but I am
> coding against libav*. How do these options translate to the options given
> to the AVDictionary parameter of avcodec_open2? Or rather, am I barking up
> the wrong tree entirely?
> I assumed that it was trivially a dictionary entry with key "movflags" and
> value "faststart" after I inspected movenc.c in libavformat, but this is
> not currently working for me and I need to determine what I have wrong (the
> "how" or the "what"). I appreciate any help.
> --michael
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
You can search in dict.h and use: int av_dict_set(AVDictionary **pm, const
char *key, const char *value, int flags).


AVDictionary* myOptions;
av_dict_set( &myOptions, "movflags", "+faststart", 0 );

av_dict_set will allocate the dictionary for you.

To understand how this all works:  there is a bunch of code in ffmpeg that
deals with AVOptions (and these dictionaries) and converts human readable
data into the right flags.  In the ffmpeg source code, in
libavformat/movenc.c you can find:

static const AVOption options[] = {
    { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags),
    { "faststart", "Run a second pass to put the index (moov atom) at the
beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 =
"movflags" },

The first one defines the human-readable "movflags" to access the "flags"
member variable, and the one further down that ends with "movflags" is a
way of saying the "faststart" string refers to a flag that applies to the
variable movflags.  In opt.c you'll find special code for AV_OPT_TYPE_FLAGS
that deals with prefixing with "+" or "-" which will add or remove flags
from the variable specified.  Once you know all of this, the code (and
settings) become quite readable which is nice.

Everything that is an AVClass can be accessed using these types of options
through the AVOptions interfaces (libavutil/opt.h), or passing in an
AVDictionary with the options when creating it, as you've found.  I've
found it helpful when starting out to use the AVOptions interface rather
than AVDictionary.  The reason being, there's more immediately feedback
through the return code of av_opt_set, and you can grab the configuration
of an object through av_opt_serialize.  This will help you catch errors
like setting movflags on the stream rather than the muxer.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20180502/5e0db68b/attachment.html>

More information about the Libav-user mailing list