ffmpeg libswresample.so.2 needed by libavcodec.so not found - ffmpeg

I have locally built ffmpeg library, but I am unable to link them in my program.
I have tried CMAKE_INSTALL_RPATH, but libswresample still cannot be found.
Strangely all the libraries libswresample, libavcodec etc etc are all in the same folder ( /local/git/PSG/libs/ffmpeg/../ffmpeg-install/lib/ ) . libavcodec is found, but not the libswresample.
So, I fail to understand why is gcc complaining?
I am using target_link_libraries( ) to link the ffmpeg libraries.
What am I doing wrong?
/usr/bin/ld: warning: libswresample.so.2, needed by /local/git/PSG/libs/ffmpeg/../ffmpeg-install/lib/libavcodec.so, not found (try using -rpath or -rpath-link)

The problem was that the libswresample.so.2 was taken from the system path. Unfortunately, all the libs generated by ffmpeg have to be explicitly linked in the target_link_libraries(). By explicitly giving the absolute path of libswresample for example by
find_library(libswresample_path NAMES libswresample PATH "the/path/to/your/libresample"),
the problem was solved

Related

How can I detect ffmpeg vs libav in CMake?

My project uses libavformat to connect to rtsp:// URLs. It's important that it set a socket timeout and reconnect on error. Unfortunately, the stimeout open option for this only exists in ffmpeg (and in particular, its libavformat versions >= 55.1.100), not the competing project libav (any version). And some systems I'd like to support (such as Raspbian Jessie) are still bundled with libav.
So, I think my best option is to detect whether I have a suitable version using cmake, and install ffmpeg in-tree if not. I think I should be able to do this via something like:
pkg_check_modules(FFMPEG libavutil libavcodec libavformat)
if(not FFMPEG_FOUND or FFMPEG_VERSION VERSION_LESS 55.1.101)
ExternalProject_Add(
FfmpegProject
URL "http://ffmpeg.org/releases/ffmpeg-2.8.3.tar.xz"
URL_HASH "SHA1=a6f39efe1bea9a9b271c903d3c1dcb940a510c87"
INSTALL_COMMAND "")
...set up flags and such to use this in-tree version...
endif()
except that I don't know how to detect libav vs ffmpeg. I don't see anything in the pkgconfig stuff or libavformat/version.h to distinguish them. The version numbers they use seem to overlap. It's not obvious to me at all how to tell the difference programmatically, much less do so with a not-weird cmake rule. Any ideas?
To specifically answer your question, use this code:
#include <stdio.h>
#include "libavutil/opt.h"
#include "libavformat/avformat.h"
int main(int argc, char *argv[]) {
av_register_all();
AVInputFormat *input = av_find_input_format("rtsp");
const AVClass *klass = input->priv_class;
const AVOption *opt = av_opt_find2(&klass, argv[1], NULL, 0, AV_OPT_SEARCH_FAKE_OBJ, NULL);
printf("%p\n", opt);
return 0;
}
This can do runtime detection, and here's how it works:
bash-3.2$ /tmp/test hi
0x0
bash-3.2$ /tmp/test stimeout
0x103420100
For your other question, detecting Libav vs. FFmpeg can be done by looking at the library micro version. For FFmpeg, they all start at 100 (e.g. libavformat 55.1.100), whereas for Libav, they start at 0. So if micro < 100, it's Libav, else it's FFmpeg. To get libavformat micro version at runtime, use avformat_version() & 0xff, or LIBAVFORMAT_VERSION_MICRO at compile time.

How to use libraries ".lib" and ".a" files in GCC

I have simple C code that uses some functions of libavcodec in FFmpeg. I try to compile the code with GCC (on Windows using MinGW) as follows:
gcc -o mycode mycode.c
But I get a lot of errors like "undefined reference to av_free". I know that these functions are defined in the libraries of FFmpeg. I do access files like avcodec.lib and libavcodec.dll.a, but I don't know how to use them with GCC so that I can compile and make my file. How can I solve this problem?
Try something like:
gcc -o mycode mycode.c -L C:\path\to\avcodec -lavcodec
I don't have Windows, so I cannot test it myself.
The point is that you need to tell gcc that some functions are in avcodec.lib / .dll. After the -L you tell where the .a / .dll is, then you must tell the linker to actually consider the avcodec.lib / .dll, that corresponds to -lavcodec.

Compilation error using ffmpeg library

I have downloaded and installed the ffmpeg library. I want to use it for reading the separate frames of different videos and manipulate them. For that I tried to follow some tutorial from here: http://dranger.com/ffmpeg/tutorial01.html
But I can't compile my cpp file since I get the following compilation:
Undefined symbols for architecture x86_64:
"av_register_all()", referenced from:
_main in cc9zyUBe.o
_main in ccRz35d4.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
When I was installing ffmpeg library, I used arch=x86_64 option in ./configure step.
I use OS X Mountain Lion 10.8.2 and gcc 4.2 compiler.
Does somebody have any clue what can be the reason of this error?
Thanks in advance.
UPDATE:
I've already tried many different install options, with static libraries, shared libraries, with/without --arch=x86_64 option. Also installed it with homebrew, result remains the same. Library isn't recognized. But ffmpeg binary works pretty well, when I use it as a command-line tool.
Finally I have managed to compile my program which uses ffmpeg library.
For some reason I still couldn't compile it using gcc compiler, but I could do it with g++ compiler.
When the static libraries are installed, all the dependencies must be specified explicitly, and the order of linking of these libraries is also important. So here is the compilation code which finally compiled my program:
g++ readVideo.cc -o readVideo $(pkg-config --libs --cflags libavformat)
pkg-config here is a utility which prints all the flags and libraries that are needed to properly link the specified libavformat library.
Also it's worth of mentioning, that the source file was renamed from readVideo.C to readVideo.cc, and that #include statements have been encompassed using extern "C" as follows:
extern "C" {
#include <libavformat/avformat.h>
}
It is needed because ffmpeg is a C project and program will not compile with C++ compiler if you don't explicitly state that it is C library.
And if you don't want to bother with pkg-config to include all dependencies for ffmpeg libraries, you can install ffmpeg with shared libraries instead of static ones. Then it will compile by simpler call:
g++ readVideo.cc -o readVideo -lavformat
To install shared libraries, you need to add these 2 options to ./configure program when installing ffmpeg:
--disable-static --enable-shared
Hope it helps somebody some time ...
The error means that you aren't linking to the ffmpeg libraries. Simply including the header files is not enough. You need to also link to the actual library files. On Windows they have .lib extension, on Linux usually .a extension, not sure about Mac.

undefined reference to `x264_encoder_open_125'

While installing ffmpeg on Ubuntu 12.04
I am getting following error
libavcodec/libavcodec.a(libx264.o): In function `X264_init':
/root/ffmpeg/libavcodec/libx264.c:492: undefined reference to `x264_encoder_open_125'
collect2: ld returned 1 exit status
make: *** [ffmpeg_g] Error 1
I am following the instructions given at
http://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide
Do anyone have idea about this error?
This is a typical problem for people who already have x264 installed through the package management system. You can solve this in at least 2 ways:
Uninstall the already existing x264 from your system, through the package management system:
# apt-get remove x264
and compile your new x264 from source
Don't uninstall the x264 package, but compile your new x264 and then compile your ffmpeg, telling it to use that newly compiled x264 library, by specifying that directory where your compiled x264 library is, using the mentioned LD_LIBRARY_PATH environment variable:
LD_LIBRARY_PATH=/path/to/my/compiled/x264/library ./configure --enable-libx264 ...
More info can be found on these links:
problem with --enable-libx264 option in ffmpeg
Compiling FFmpeg
add the header and lib path
gcc x264_test1.c -o x264_encoder -I/usr/local/include -L/usr/local/lib -lpthread -lm -lx264
Generally the error means that the library binary libx264.so picked up by the linker does not match the version in the header file x264.h. See the following lines of code in this header file:
/* Force a link error in the case of linking against an incompatible API version.
* Glue #defines exist to force correct macro expansion; the final output of the macro
* is x264_encoder_open_##X264_BUILD (for purposes of dlopen). */
#define x264_encoder_glue1(x,y) x##y
#define x264_encoder_glue2(x,y) x264_encoder_glue1(x,y)
#define x264_encoder_open x264_encoder_glue2(x264_encoder_open_,X264_BUILD)
The solution usually does not require building libx264 yourself,
just make sure that you installed libx264-dev properly without interference with other versions, which may also be in /usr/local/lib or the like.
I had the same issue with version 155:
undefined reference to 'x264_encoder_open_155'.
In my case this was because I had in /usr/lib/x86_64-linux-gnu and unsuitable copy of libx264.so (which I had produced myself and uncleanly copied there).
So all I had to do was sudo apt-get install --reinstall libx264-dev.

Compiling against modified library in linux

I've compiled mplayer2 against the prebuilt libavcodec library in ubuntu. It crashes under specific conditions.
Judging my the error message spat out, the problem lies with libavcodec. So what I would like to is do experiment with some of the libavcodec code. i.e. Compile my own version of libavcodec and then compile mplayer2 against that.
pkg-config will point towards the installed libavcodec libraries from ubuntu's apt-get. How do I point it to my own version of libavcodec? Or tell the configure script to use my version?

Resources