6

I can't use the ffmpeg in QT.

My Steps :

-1. compile the ffmpeg as normal

-2. add lib/include path in QT .pro

LIBS += -L/home/kim/ffmpeg/lib -lavcodec -lavformat
INCLUDEPATH += /home/kim/ffmpeg/include

-3. in code

extern "C" { 
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}

and just call a function named av_register_all();

then I got the following bulid issues< many functions undefined reference >

in function `rl2_read_packet`: undefined reference to `av_free_packet`
in function `av_register_all`: ....

I search a solution of the problem but not work for me.

Any other solution? Thanks.

Mat
  • 202,337
  • 40
  • 393
  • 406
user775668
  • 61
  • 1
  • 3

2 Answers2

11

This is an old post and it is almost solved. By the way, I faced to the problem and I had lots of time struggling with

undefined reference to av_register_all

I am using Qt Creator 5.3.2 and this is my MYPROJECT.pro file

MYPROJECT.pro

QT       += core
QT       -= gui
TARGET = MYPROJECT
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
QMAKE_CXXFLAGS += -D__STDC_CONSTANT_MACROS
LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil -lswscale
INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include
  • Setting up project.pro

It is recommended to build ffmpeg from scratch, but if you are on windows and if you are not willing to compile this masterpiece, then congrats! you are like me! Go for pre-built in zereanoe and download both the latest shared and dev win-32 versions. Extract the latter two packages in a place where there is no spaces in the path, i.e. mine is in: C:\ffmpeg-20151219-git-2dba040-win32-dev I recommend that don't rename the main folder because it is highly informative. Set the include folder of the [PATH TO ffmpg-YYYYMMDD-git-win32-dev]/include to your project's search space by the following line of code:

INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include

You should also use libraries in [PATH TO ffmpeg-YYYYMMDD-git-win32-dev]/lib to your project as below:

LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
    LIBS += -lavcodec -lavformat -lavutil

You can call libraries other than lavcodec, lavformat and lavutil. In your cpp file you should enclose headers corresponding to ffmpeg in extern "C" block.

  • DLLs should be added

The final step is to add all the dll files in the shared version of ffmpeg that you have recently downloaded. Copy *.dll files within

[PATH TO ffmpeg-YYYYMMDD-git-2dba040-win32-shared]/bin

and paste them wherever your executable exists. Fo instance, my project is located on C:/QtProjects/MYPROJECT, then my binaries are located on:

C:/QtProjects/build-MYPROJECT-Desktop_Qt_5_3_MinGW_32bit-Debug

Now, copy all the dlls to your project's debug or release folder.

  • My main.cpp This is my main.cpp :

main.cpp

#include <iostream>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>

}
using namespace std;
int main(int argc, char *argv[])
{
    cout<<"I have included FFMPEG in my project"<<endl;
    av_register_all();
    cout<<"If everything goes well you will see ME"<<endl;
    return 0;
}
Davood Falahati
  • 1,474
  • 16
  • 34
  • Even after doing all of these steps I am still unable to get ffmpeg to work in QT. My program complies fine and then unexpectedly finishes even though the dll's are in the same directory as the executable. http://stackoverflow.com/questions/35278460/including-ffmpeg-in-qt-project-on-windows-causes-the-program-to-unexpectedly-fin – Chris Mar 29 '16 at 23:06
  • 1
    I read your question and I think something went wrong on adding the external dlls to your .pro. Instead targeting the files's physical address( LIBS+=MY_PATH/libavformat.dll) use this format LIBS +=MY_PATH -lavcodec -lavformat -lavutil. Moreover, have you tried mingw compiler instead msvc ? – Davood Falahati Mar 30 '16 at 05:42
  • I am adding the lib not the dll files in my pro (I have tried the other way out of frustration). The dlls have been put in the same directory as the executable. My understanding of dlls is that as they are linked at run time they need to be in a find-able location (eg where the executable is or system32) and therefore do not need to be included in the pro. (If my understanding is incorrect it could be my problem as the type of error I am getting is usually associated with a dll not being found). Switching compilers is a undesirable solution as it might cause problems elsewhere in my code base. – Chris Mar 30 '16 at 18:26
  • @Burn-Man, your statement about DLLs are true, there is no need to include them while compiling, however, there are STATIC libraries that are needed while compiling. LIBS+= PATH -lavformat for example, adds static library, say dll.a files. – Davood Falahati Mar 30 '16 at 19:43
  • Well you were right about it working with MinGW. Now I have to see about either building ffmpeg with MSVC or seeing how bad switching to MinGW will be for our code. – Chris Apr 01 '16 at 14:45
  • Good to hear that! – Davood Falahati Apr 01 '16 at 16:01
5

I had the same problems, it worked using the following trick.

In code cpp source:

#define __STDC_CONSTANT_MACROS

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

And in your .pro file:

LIBS += -LC:/Users/tom/bla/ffmpeg/lib/ -lavcodec -lavformat -lavutil

I think in your case, you need the -lavutil lib parameter.

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
seattledirk
  • 123
  • 1
  • 1
  • 6