[FFmpeg-devel] MKV Header: Writing duration early

Soft Works softworkz at hotmail.com
Thu Jul 14 06:04:03 EEST 2016

Nicolas George wrote:
>Le quintidi 25 messidor, an CCXXIV, Soft Works a écrit :
>> Such circumstances are not really "special" or even rare.
>> Especially in most trivial cases (like mkv to mkv) there is a known
>> duration from the source that could be used.
>> No doubt, that it's not available in all cases. But it could be written in
>> those cases where it's available.
>There are two sides to this issue: change the muxer to write the value if it
>is known at the beginning and change the command-line tool to compute the
>value for their output.
>I suspect the muxer change would be reasonably easy.
>The change on the command-line tool, on the other hand, you would have to
>determine if the input duration is reliable, detect if filters may change
>the duration, take mapping from various files into account, etc. This is
>very hard.
>> Probably the actual question is: If I would submit a patch to do this,
>> would it have a chance to be included?
>If the patch is clean and the feature/size ratio is reasonable, of course.

I had a look at the encoder for WebM files (webmdashenc.c). In this case
the duration is written during "write_header" (and not even overwritten
in "write_trailer").

The duration is determined in webmdashenc.c with this method:

static double get_duration(AVFormatContext *s)
    int i = 0;
    double max = 0.0;
    for (i = 0; i < s->nb_streams; i++) {
        AVDictionaryEntry *duration = av_dict_get(s->streams[i]->metadata,
                                                  DURATION, NULL, 0);
        if (!duration || atof(duration->value) < 0) continue;
        if (atof(duration->value) > max) max = atof(duration->value);
    return max / 1000;

Another example is from write_metadata in flvenc.c:

    // fill in the guessed duration, it'll be corrected later if incorrect
    put_amf_double(pb, s->duration / AV_TIME_BASE);

This is evidence that early writing a duration to result file headers
is nothing new and already implemented for other file formats.

So can I just do it in the same way for mkv in matroskaenc.c?
Or should it be guarded by a new command line option?

(I'd say no because this only affects situations where the output
file is being read while ffmpeg is still running and at the end of the
process the duration is overwritten anyway).

What do you think?


More information about the ffmpeg-devel mailing list