12

By default, libavformat writes error messages to stderr, Like:

Estimating duration from bitrate, this may be inaccurate

How can I turn it off? or better yet, pipe it to my own neat logging function?

Edit: Redirecting stderr to somewhere else is not acceptable since I need it for other logging purposes, I just want libavformat to not write to it.

amrhassan
  • 2,285
  • 3
  • 16
  • 12
  • See either http://stackoverflow.com/questions/5095839/redirect-from-stderr-to-another-file-descriptor or http://stackoverflow.com/questions/573724/how-can-i-redirect-stdout-to-some-visible-display-in-a-windows-application – Martin Beckett Oct 16 '11 at 19:51
  • I don't want to redirect the whole output of stderr of the program, I have my logging functions writing to it in a neat formatted manner. I'd just like libavformat to not write to it. – amrhassan Oct 16 '11 at 19:56
  • What exactly writes to stderr? This is the 1st time that I hear libavcodec library sends errors to stderr (my experience with that library is not big). – BЈовић Oct 16 '11 at 20:12
  • I'm not really sure. It throws error messages haphazardly like the one I mentioned above. It might even be libavcodec and not libavformat. It's hard to track down since no further information is given. – amrhassan Oct 16 '11 at 20:24

4 Answers4

13

Looking through the code, it appears you can change the behavior by writing your own callback function for the av_log function.

From the description of this function in libavutil/log.h:

Send the specified message to the log if the level is less than or equal to the current av_log_level. By default, all logging messages are sent to stderr. This behavior can be altered by setting a different av_vlog callback function.

The API provides a function that will allow you to define your own callback:

void av_log_set_callback(void (*)(void*, int, const char*, va_list));

In your case, you could write a simple callback function that discards the messages altogether (or redirects them to a dedicated log, etc.) without tainting your stderr stream.

Ben
  • 1,200
  • 7
  • 13
8

Give av_log_set_level(level) a try!

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
cpl
  • 669
  • 6
  • 23
5
  1. include this header file

    #include <libavutil/log.h>
    
  2. add this code will disable the log

    av_log_set_level(AV_LOG_QUIET);
    
bingyi
  • 51
  • 1
  • 1
1

You can redirect them to a custom file, it will redirect all cerr entry:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  ofstream file("file.txt");

  streambuf *old_cerr = cerr.rdbuf();

  cerr.rdbuf (file.rdbuf());

  cerr << "test test test" << endl; // writes to file.txt

  // ...

  cerr.rdbuf (old_cerr); // restore orginal cerr

  return 0;
}

Edit: After editing the question, i warn about above code that it will redirect all cerr entry stream to file.txt

I'm not familiar with libavformat, but if its code is unchangeable, you can temporary redirect cerr to a file before calling library's api and redirect it to original cerr again.(However this is ugly way)