环境:ubuntu 18.04 LTS、qt5.13.0
下载 ffmpeg 源码
http://www.ffmpeg.org/download.html
1、下载好了之后,切换到文件所在目录,我是下载的 4.2 版本的
cd Downloads/
2、然后使用 tar 解压到当前目录 或 指定目录
tar jxvf ffmpeg-4.2.tar.bz2
tar jxvf ffmpeg-4.2.tar.bz2 -C /home/../
3、切换到目录
cd ffmpeg-4.2/
配置 ffmpeg
./configure
报错
nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
按照提示执行 ./configure --disable-x86asm 即可
./configure --disable-x86asm
编译安装
make
sudo make install
如果报错再重新执行一下 配置-> 编译-> 安装 操作即可
使用
接下来就可以在你的代码里面使用了
1、新建一个比如 Qt Quick Application 在头文件添加比如:
// 源代码是由 C 编写的
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
}
然后编译,但是报错
undefined reference to `avcodec_register_all'
undefined reference to `avformat_alloc_context'
undefined reference to `avformat_open_input'
undefined reference to `avformat_find_stream_info'
undefined reference to `avcodec_alloc_context3'
undefined reference to `avcodec_find_decoder'
undefined reference to `avcodec_open2'
undefined reference to `av_frame_alloc'
undefined reference to `av_packet_alloc'
undefined reference to `swr_alloc'
undefined reference to `av_opt_set_int'
undefined reference to `av_opt_set_int'
undefined reference to `av_opt_set_int'
undefined reference to `av_opt_set_int'
undefined reference to `av_opt_set_sample_fmt'
undefined reference to `av_opt_set_sample_fmt'
undefined reference to `swr_init'
undefined reference to `av_read_frame'
undefined reference to `av_samples_get_buffer_size'
undefined reference to `avcodec_decode_audio4'
undefined reference to `swr_convert'
undefined reference to `av_frame_free'
undefined reference to `avformat_close_input'
undefined reference to `av_packet_free'
collect2: error: ld returned 1 exit status
哦~忘记在程序的 .pro 文件里添加 Lib 路径了
LIBS += -L/usr/local/lib -lavcodec -lavformat -lavutil -lswresample
运行…我觉得是在搞我,还是报错
undefined reference to `av_bitstream_filter_filter'
undefined reference to `avpriv_codec2_mode_bit_rate'
undefined reference to `avpriv_codec2_mode_frame_size'
undefined reference to `avpriv_codec2_mode_block_align'
undefined reference to `avpriv_codec2_mode_bit_rate'
undefined reference to `avpriv_codec2_mode_frame_size'
undefined reference to `avpriv_codec2_mode_block_align'
undefined reference to `av_adts_header_parse'
undefined reference to `av_adts_header_parse'
undefined reference to `av_adts_header_parse'
collect2: error: ld returned 1 exit status
依赖库的顺序有问题。。。前面的依赖库会用到后面库的方法
将
LIBS += -L/usr/local/lib -lavcodec -lavformat -lavutil -lswresample
改为
LIBS += -L/usr/local/lib -lavformat -lavcodec -lavutil -lswresample
好了,编译通过
Note:不过会提示:
QML debugging is enabled. Only use this in a safe environment.
在 Debug 版本会提示这个,而在 Release 版不会,好像没啥影响。。。奇怪