[FFmpeg-devel] [PATCH 02/24] ffmpeg: simplify getting output file size

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Thu Dec 16 13:54:23 EET 2021


Anton Khirnov:
> Stop calling avio_size()/avio_tell(), which are potentially heavy
> operations and may interfere with muxer operation. Use the recently
> introduced bytes_written field instead.
> ---
>  fftools/ffmpeg.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index bdeff9a12e..b77531cb7f 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -1677,8 +1677,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
>  {
>      AVBPrint buf, buf_script;
>      OutputStream *ost;
> -    AVFormatContext *oc;
> -    int64_t total_size;
> +    int64_t total_size = -1;
>      AVCodecContext *enc;
>      int frame_number, vid, i;
>      double bitrate;
> @@ -1708,11 +1707,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
>      t = (cur_time-timer_start) / 1000000.0;
>  
>  
> -    oc = output_files[0]->ctx;
> -
> -    total_size = avio_size(oc->pb);
> -    if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
> -        total_size = avio_tell(oc->pb);
> +    if (output_files[0]->ctx->pb)
> +        total_size = output_files[0]->ctx->pb->bytes_written;
>  
>      vid = 0;
>      av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
> 

1. bytes_written is only updated when the data is actually written, i.e.
the stuff still residing in the AVIOContext's internal buffer is not
accounted yet. This might lead to the bitrate being significantly
underestimated at the beginning of output.
2. In case of seekable output when data already written is overwritten
the new stats will be totally off. With -movflags +faststart the final
size is off by a factor of two.

- Andreas


More information about the ffmpeg-devel mailing list