Ubuntu 20.04LTS FFmpeg 环境配置

Ubuntu 20.04LTS FFmpeg 环境配置

FFmpeg 是音视频开发工作中不可缺少的工作技能,本文主要讲解如何在 Ubuntu 系统之上编译、搭建 FFmpeg 开发环境。

Chapter 1 安装前的环境配置

Part 1 安装环境依赖

1
  sudo apt-get update
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
sudo apt-get install -y \
  autoconf \
  automake \
  build-essential \
  cmake \
  git-core \
  libass-dev \
  libfreetype6-dev \
  libsdl2-dev \
  libtool \
  libva-dev \
  libvdpau-dev \
  libvorbis-dev \
  libxcb1-dev \
  libxcb-shm0-dev \
  libxcb-xfixes0-dev \
  pkg-config \
  texinfo \
  wget \
  zlib1g-dev
1
sudo apt install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev

Part 2 安装相关库

1
2
3
4
5
6
7
8
9
sudo apt-get install -y \
	nasm \
	yasm \
	libx264-dev \
	libx265-dev \
	libnuma-dev \
	libvpx-dev \
	libfdk-aac-dev \
	libopus-dev \

Chapter 2 安装 FFmpeg

Part 1 下载 FFmpeg

使用 4.3.1 版本

1
2
3
4
mkdir ffmpeg && cd ffmpeg/
wget https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.bz2
tar xvf ffmpeg-4.3.1.tar.bz2
cd ffmpeg-4.3.1/

Part 2 配置编译项

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
./configure --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
  --enable-vdpau
  --enable-vaapi

Part 3 编译

1
  sudo make -j8 && sudo make install

Part 4 测试

1
  ffmpeg -version
编写测试程序

1、创建C代码文件名叫hello.c,填入下面的代码内容:

1
2
3
4
5
  #include <libavutil/avutil.h>

  int main(int argc, char* argv[]) {
	  av_log(NULL, AV_LOG_INFO, "hello world\n");
  }

2、保存并退出该文件,执行以下命令编译hello.c:

1
  gcc hello.c -o hello -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib -lavformat -lavdevice -lavfilter -lavcodec -lavutil -lswscale -lswresample -lpostproc -lm

3、运行编译好的hello程序,也就是执行以下命令:

1
  ./hello

发现控制台回显日志信息“hello world”,表示测试程序运行正常,说明FFmpeg开发环境已经成功搭建。

4、刚才的测试程序hello.c采用C语言编写,并且使用gcc编译。若要采用C++编程的话,则需改成下面的hello.cpp代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  #include <iostream>

  // 因为FFmpeg源码使用C语言编写,所以在C++代码中调用FFmpeg的话,要使用标记“extern "C"{……}”把FFmpeg的头文件包含进来
  extern "C"
  {
  #include <libavutil/avutil.h>
  }

  int main(int argc, char* argv[]) {
	  av_log(NULL, AV_LOG_INFO, "hello world\n");
  }

鉴于C++代码采用g++编译,于是hello.cpp的编译命令变成下面这样:

1
  g++ hello.cpp -o hello -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/bin -lavformat -lavdevice -lavfilter -lavcodec -lavutil -lswscale -lswresample -lpostproc -lm

编译完毕,同样生成名叫hello的可执行程序,如此就实现了C++代码集成FFmpeg函数的目标了。

一键安装脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  sudo apt-get update
  sudo apt-get install -y autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev
  sudo apt install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev
  sudo apt-get install -y nasm yasm libx264-dev libx265-dev libnuma-dev libvpx-dev libfdk-aac-dev libopus-dev
  mkdir ffmpeg && cd ffmpeg/
  wget https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.bz2
  tar xvf ffmpeg-4.3.1.tar.bz2
  cd ffmpeg-4.3.1/
  ./configure --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-vdpau --enable-vaapi
  sudo make -j8 && make examples && sudo make install
  # git clone https://github.com/lazybing/ffmpeg-study-recording
Licensed under CC BY-NC-SA 4.0