I'm writing a program that uses libffmpeg to do some transcoding internally, but I find it has a tendency to print a lot of things to the process's stderr. I want to redirect those messages somewhere more intelligent.
To be clear, I am NOT talking about spawning the ffmpeg
executable as a subprocess and redirecting its stdio! That is trivial in any programming language. I'm using ffmpeg as a C library (well, Rust bindings around a C library in my case) and I'm wondering if libffmpeg provides a hook for writing to stderr that I can override.
If no such hook exists, would my best bet be to fork a child process with stdio redirection and only use libffmpeg from that subprocess? That feels really hacky to me, especially since it would be really nice to not have to send the results libffmpeg spits out across a process boundary.
#include <libavutil/log.h>
#include <stdarg.h>
#include <stdio.h>
void custom_log_callback(void* ptr, int level, const char* fmt, va_list vargs) {
FILE* log_file = fopen("ffmpeg_log.txt", "a");
if (log_file) {
vfprintf(log_file, fmt, vargs);
fclose(log_file);
}
}
int main() {
// Set the custom logging function
av_log_set_callback(custom_log_callback);
// Example: Log a message using FFmpeg's logging system
av_log(NULL, AV_LOG_INFO, "This is a log message.\n");
return 0;
}