ubuntu 编译安装ffmpeg

[前言]    

    使用apt-get install直接安装的ffmpeg无法提供相应的头文件,

在编译一些使用ffmpeg接口的程序时会提示找不到某某头文件,

于是准备自己编译安装一下ffmpeg.

    安装分为普通安装和修改配置文件后安装。普通安装不能调用x264、x265相关接口。

[安装FFmpeg前先安装SDL]

1. 将github上的FFmpeg代码复制到码云上,之后在linux中执行:

    (1). git clone 你的下载地址;

    (2). 安装SDL2(我用的最新的ffmpeg,如果版本较老,可能需要SDL1.2库)库,否则编译安装完成后没有ffplay。

          安装过程:

          sudo apt-get install libsdl2-2.0 

          sudo apt-get install libsdl2-dev

          测试:

#include <iostream>
#include <SDL2/SDL.h>
 
int main()
{
         SDL_Init(SDL_INIT_EVERYTHING);
         SDL_Quit();
         printf("Runing Successful!
");
         return 0;
}

    g++ filename.cpp -o filename -lSDL2

    执行生成的可执行文件,出现"Running Successful!"表示安装SDL2库成功。

[普通安装]

普通安装是不带x264、x265等库的,如果需要调用相关编解码器的接口,参考下方【设置配置文件后安装】

  1. 打开FFmpeg文件夹

      ./configure

      在执行此配置过程中,出现报错"nasm/yasm not found or too old"

      为解决此问题,我直接使用了

      apt-get install nasm

      apt-get install yasm

      之后,再执行

      ./configure

      成功

  (注意,FFmpeg源码包最好是现git clone到当前系统的,如果从Windows系统下或其它路径下拷贝过来,

  很可能./configure报错deps_deps_deps.._deps_checking:inprogress:File name too long)

  2. 编译

      make all

  3. 安装

      make install

  [参考链接]

  SDL2安装:https://blog.csdn.net/zhouzhenhe2008/article/details/54428761

  SDL2测试代码参考:https://blog.csdn.net/lazzy_dog/article/details/77914203

  ffplay没安装上:http://blog.chinaunix.net/uid-11344913-id-3936227.html

  FFmpeg编译安装:https://blog.csdn.net/qqqq245425070/article/details/83961218

【设置配置文件后安装】

  建议:如果使用虚拟机用了分享文件夹,编译安装各种包时,不要在分享文件夹里进行。

   需要将安装包拷入系统内部文件路径或者直接将安装包下载到系统内部路径。

  安装cmake: sudo apt-get install cmake

  1. 更新nasm

    x264库需要的nasm版本比ubuntu16 通过sudo apt-get install nasm安装的版本高,因此需要自己下载新版本的nasm编译安装;

    (1). 卸载原有nasm: sudo apt remove nasm;

    (2). 下载新版本的nasm安装包:

      $ wget "https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz"

      (具体版本也可参考https://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=A)

    (3). 解压 tar -xvf nasm-2.15.05.tar.gz

    (4). cd nasm-2.15.05

      ./configure

      make

      sudo make install

    (5). 查看nasm版本:

      nasm -v

      (NASM version 2.15.05 compiled on Nov 10 2020)

  2. 安装x264库

    (1). 下载x264:

      git clone https://gitee.com/mirrors/x264.git

    (2).  编译安装x264

      ./configure --enable-shared --enable-static

      make

      sudo make install

    (3). 查看x264库安装情况:

      $ updatedb

      $ locate x264

      我是安装到了默认路径,查询显示 /usr/local/lib中安装了x264相关的库。

      (为保险起见,可以在FFmpeg文件夹里先执行 ./configure --enable-shared  --enable-libx264  --enable-gpl   --enable-libx265

        如果没有提示找不到x264库,则说明安装库成功)

  3. 安装x265库

    (1). 下载x265:

      wget "http://ftp.videolan.org/pub/videolan/x265/x265_3.2.tar.gz"

      (其他版本:http://ftp.videolan.org/pub/videolan/x265/;github:https://github.com/videolan/x265)

    (2). 解压 tar -xvf  x265_3.2.tar.gz

    (3). cd x265_3.2/bulid/linux

        ./make-Makefiles.bash

      (这一步可能会提示找不到ccmake,需要 sudo apt-get install cmake-curses-gui)

      make

      sudo make install

    (4). 查看x265库安装情况

      $ updatedb

      $ locate x265

      查询到在 /usr/local/lib安装了x265相关的库

  4. 编译安装FFmpeg

    转到FFmpeg路径下:

    $ ./configure --enable-shared  --enable-libx264  --enable-gpl   --enable-libx265

    $ make

    $ make install

  5. 运行ffmpeg,报错:不能打开共享库 libavdevice.so.58

    使用 locate libavdevice.so.58,发现其在/usr/local/lib路径下。

    参考CSDN博客https://blog.csdn.net/weixin_43166958/article/details/89335679,问题得到解决:

    vi /etc/ld.so.conf

    添加 /usr/local/lib/

    保存后执行 sudo ldconfig

    运行 ffmpeg,正常。

  [参考链接]

  ccmake找不到:https://blog.csdn.net/u012939880/article/details/83752500

  ffmpeg交叉编译:https://blog.csdn.net/simonforfuture/article/details/79062329

  不能打开共享库 libavdevice.so.58:https://blog.csdn.net/weixin_43166958/article/details/89335679

  [推荐——大神雷霄骅的系列教程]

       https://blog.csdn.net/leixiaohua1020/article/details/15811977

      

原文地址:https://www.cnblogs.com/brian-sun/p/13936624.html