Search code examples
c++windowsaudioffmpeg

FFmpeg av_dump_format showing incorrect output, but ffprobe displays correct metadata


I use ffmpeg 7.1 source build on windows and compilation configuration as follow:

configuration:
  --toolchain=msvc
  --arch=x86_64
  --enable-x86asm
  --enable-asm
  --enable-shared
  --enable-w32threads
  --prefix=/D/build

When I use av_dump_format to print metadata of test.mp3, it display the incorrect information.

extern "C"
{
#include <libavformat/avformat.h>
}

int main()
{ 
    av_log_set_level(AV_LOG_DEBUG);

    auto url = "D:/music/test.mp3";

    AVFormatContext* fmt;
    auto ret = avformat_open_input(&fmt, url, nullptr, nullptr);
    if (ret < 0)
    {
        av_log(nullptr, AV_LOG_ERROR, "Cannot open %s format : %s", url, av_err2str(ret));
        exit(EXIT_FAILURE);
    }

    av_dump_format(fmt, 0, url, 0);
}

output

[AVFormatContext @ 000001768E65BC40] Opening 'D:/music/test.mp3' for reading
[file @ 000001768E65C180] Setting default whitelist 'file,crypto,data'
[mp3 @ 000001768E65BC40] Format mp3 probed with size=4096 and score=51
id3v2 ver:4 flags:00 len:35
[mp3 @ 000001768E65BC40] pad 576 576
[mp3 @ 000001768E65BC40] Skipping 0 bytes of junk at 462.
Input #0, mp3, from 'D:/music/test.mp3':
  Metadata:
    encoder         : Lavf58.76.100
  Duration: N/A, bitrate: N/A
  Stream #0:0, 0, 1/14112000: Audio: mp3, 0 channels
      Metadata:
        encoder         : Lavc58.13

You can see the Stream info is useless.

But I use ffprobe which is built with same configuration to print metadata of test.mp3.

It's display normal.

ffprobe version 7.0.1 Copyright (c) 2007-2024 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34123 for x64
  configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --prefix=/D/build
  WARNING: library configuration mismatch
  avutil      configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  avcodec     configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  avformat    configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  avdevice    configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  avfilter    configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  swscale     configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  swresample  configuration: --toolchain=msvc --arch=x86_64 --enable-x86asm --enable-asm --enable-shared --enable-w32threads --disable-programs --disable-doc --disable-static --prefix=/D/build
  libavutil      59.  8.100 / 59. 39.100
  libavcodec     61.  3.100 / 61. 19.100
  libavformat    61.  1.100 / 61.  7.100
  libavdevice    61.  1.100 / 61.  3.100
  libavfilter    10.  1.100 / 10.  4.100
  libswscale      8.  1.100 /  8.  3.100
  libswresample   5.  1.100 /  5.  3.100
Input #0, mp3, from 'D:\music\test.mp3':
  Metadata:
    encoder         : Lavf58.76.100
  Duration: 00:05:00.12, start: 0.025057, bitrate: 128 kb/s
  Stream #0:0: Audio: mp3 (mp3float), 44100 Hz, stereo, fltp, 128 kb/s
      Metadata:
        encoder         : Lavc58.13

Why is av_dump_format not correctly showing stream information? How can I fix it?


Solution

  • You need to run

    ret = avformat_find_stream_info(fmt, NULL);
    

    before dumping.

    Hopefully, you have decoders enabled.