how to call avformat_alloc_output_context2 in ffmpeg 2.6.3?

7.2k Views Asked by At

I'm developing an c/c++ app that uses ffmpeg to play audio/video.Now i want to enhance the application to allow the users to extract audio from video and save it. I followed this https://ffmpeg.org/doxygen/trunk/muxing_8c-source.html for the saving part but now the problem is with avformat_alloc_output_context2(). I'm getting an error: "undefined reference to `avformat_alloc_output_context2' ". Does anyone know about the proper way to call 'avformat_alloc_output_context2' in ffmpeg version 2.6.3

2

There are 2 best solutions below

0
On

Remember to include

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

After that you can just call

 * @param *ctx is set to the created format context, or to NULL in
 * case of failure
 * @param oformat format to use for allocating the context, if NULL
 * format_name and filename are used instead
 * @param format_name the name of output format to use for allocating the
 * context, if NULL filename is used instead
 * @param filename the name of the filename to use for allocating the
 * context, may be NULL
 * @return >= 0 in case of success, a negative AVERROR code in case of
 * failure
 */
 int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename);

For example:

static const char* output_formats[] = { NULL, "mp3", "ogg", "wav" };

AVFormatContext* formatCtx = NULL;

QString outputFileName = "insert here your output file name";

avformat_alloc_output_context2(&formatCtx, NULL, output_formats[1], outputFileName.toStdString().c_str());
if(formatCtx == NULL)
{
    //allocation have failed
}
0
On

Also git grep avformat_alloc_output_context2 in the in-source examples under doc/examples. A few matches:

You can then see exactly how they are built with:

make V=1 examples

on the top-level. The build works on Ubuntu 15.10, so then you can compare it to your build and see what is wrong.