ubuntu18.04.1LTS 编译安装ffmpeg详解
一、环境:win10 虚拟机安装的ubuntu系统
二、下载ffmpeg源码:ffmpeg官网
进入Download页面,向下拉,找到最新的源码下载:
三、ffmpeg的编译安装
1、将下载压缩包放置到home目录下(放到哪个目录自己定),对压缩包进行解压:
tar -xvjf ffmpeg-5.1.tar.gz
2、编译ffmpeg之前首先安装必要的库(这里我安装了全部库):
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev libx264-dev libx265-dev libfdk-aac-dev libmp3lame-dev libopus-dev libvpx-dev
3、设置编译项,首先在命令行中 cd ffmpeg-5.1(解压后的文件夹):prefix制定的参数表示安装完成后库文件和头文件的路径
(1)最基础的编译项:
./configure --enable-shared --prefix=/usr/local/ffmpeg
(2)增加H264支持(我是用的这个):
./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static --disable-doc --enable-gpl --enable-libx264
附上其它的编译项:
./configure --enable-shared \
--prefix=/home/hy/ffmpeg-build \
--enable-libx264 \
--enable-libsrt \
--enable-libfdk-aac \
--enable-gpl \
--enable-nonfree \
--enable-libmp3lame \
--enable-libopus \
--enable-libvpx \
--enable-libtheora \
--enable-libvorbis \
--enable-libass \
--enable-pthreads \
--enable-version3 \
--enable-shared \
--enable-librtmp
4、编译和安装:
make
sudo make install
5、建立软连接:
sudo ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/bin/ffmpeg
sudo ln -s /usr/local/ffmpeg/bin/ffprobe /usr/bin/ffprobe
6、配置ffmpeg的环境变量:
(1)、打开bashrc文件:
gedit ~/.bashrc
(2)、在环境的末尾添加环境变量配置项:注意冒号后面的路径,同上面ffmpeg的安装路径一致。
export PATH="$PATH:/usr/local/ffmpeg/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/ffmpeg/lib"
(3)、保存文件,然后输入如下命令就配置完成:
source ~/.bashrc
(4)、如果可以查询出版本号则安装成功:
ffmpeg -version
四、在ubuntu中的QT中编写代码调用ffmpeg的动态库:
1、ffmpeg的头文件和库文件相对于测试工程的存放路径如下:
2、打开pro文件,右键---Add Library---ExternalLibrary,添加Linux下的动态库:
添加完成后如下:
unix:!macx: LIBS += -L$$PWD/ffmpeg/lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswresample -lswscale
INCLUDEPATH += $$PWD/ffmpeg/include
DEPENDPATH += $$PWD/ffmpeg/include
3、添加头文件和测试代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
unsigned version = avcodec_version();
qDebug() << "version :" << version;
}
MainWindow::~MainWindow()
{
delete ui;
}
可以输出版本号则编译安装成功: