How can I turn libavformat error messages off

感情迁移 提交于 2019-12-18 22:14:17

问题


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.


回答1:


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.




回答2:


Give av_log_set_level(level) a try!




回答3:


  1. include this header file

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

    av_log_set_level(AV_LOG_QUIET);
    



回答4:


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)



来源:https://stackoverflow.com/questions/7786802/how-can-i-turn-libavformat-error-messages-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!