bjoernz
bjoernz

Reputation: 3852

How to use libavcodec in Qt4?

How do I use libavcodec in Qt4 to access individual video frames?

After verifying that the video stream can be decoded by libavcodec by compiling this example, I moved the source code to my C++ program. Now av_open_input_file() is suddenly unable to open my video file (returning an errorcode: -2).

The call looks like this right now:

...
// Register all formats and codecs
avcodec_register_all();

// Open video file
QString videoFileName("/absolute/path/to/video.avi"); // from somewhere else in the application
const char* fileName = videoFileName.toStdString().c_str();
int err = 0;
if((err = av_open_input_file(&pFormatCtx, fileName, NULL, 0, NULL)) != 0)
{
    doErrorHandling(err, fileName); // err = -2
}

When I look at const char* fileName inside the debugger it looks correct. Am I making some basic mistake in mixing C and C++ code (for a first attempt I just dumped the code from the example into the constructor of a class)?

Note: In order to get the application to compile I include the headers like this:

extern "C"
{
#define __STDC_CONSTANT_MACROS // for UINT64_C
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}

I have also tried to hard code the path to the video file into the application without any success:

av_open_input_file(&pFormatCtx, "/home/bjoernz/video.avi", NULL, 0, NULL);

I was able to compile and execute the example (avcodec_sample.0.5.0.c) using g++.

Upvotes: 3

Views: 3696

Answers (2)

bjoernz
bjoernz

Reputation: 3852

Well, this is embarrassing:

When I transfered the source code from the example to the c++ application I made a stupid mistake when I got linker errors, that told me that av_register_all(); was unavailable... and I renamed it to avcodec_register_all(), a little while later I fixed the linker problem and forgot about it...

Solution: avcodec_register_all() needs to be changed to av_register_all().

Upvotes: 2

yonilevy
yonilevy

Reputation: 5428

Error -2 means No such file or directory. I'm pretty sure the file you're attempting to open isn't located in your "current working directory" when running the application.

Upvotes: 2

Related Questions