ubuntu ffmpeg+sdl 开发平台搭建

    FFMPEG的官方解释是:A complete, cross-platform solution to record, convert and stream audio and video.

    SDL的官方解释是:Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

    简单地说,FFMPEG主要是负责音视频流的编解码,而SDL主要负责显示,他们是一对好基友。

    要让这对好基友好好工作,首先,得让他们在一起。

 

    我们就在 ubuntu 上搭建 ffmpeg+sdl 开发环境吧。

 

    操作系统:Ubuntu 16.04.1 LTS

    用户:root

    顺便提一下 ubuntu 下开root用户的方法,执行 sudo passwd root,按提示操作就行了。用root用户登录,好处就是执行某些命令,不需要在前面加sudo,比如apt-get install时。

 

    搭建开发环境其实就是安装各种软件包,有些用 apt-get install 直接安装,有些下载源码包,编译安装。

    用 apt-get install 直接安装,系统会从 ubuntu 默认的源下载安装,全自动化,简单方便,童叟不欺,但不保证版本是最新的。比如,编译 x264/x265/ffmpeg时使用到的nasm,就要求用2.14以上的,用apt-get install 安装的,就不是,所以,只能下载源码包,编译安装了。

    下载源码包编译安装,基本流程是:下载->解压->进入目录->配置->编译->安装。

        下载:有些用 git,有些用 wget,下载的源码,放在当前执行命令的目录下;

        解压:使用 tar 命令;

        进入目录:使用 cd命令;

        配置:一般用 ./configure,配置可带 --prefex=/aa/bb 参数,带这个参数时,make install 会把软件安装到/aa/bbb目录下,如果不带 --prefex 参数,则默认安装到 /usr/bin 下面;不一般的特殊情况,只要阅读文档,照做就行了。

        编译:一般使用 make 命令,不一般的情况,还是阅读文档;

        安装:一般使用 make install 命令,不一般的情况,还是阅读文档;

 

    基础讲完了,开始下载安装需要的软件包:

    1、SDL:基友1。注意:一定要先安SDL,再安装 ffmpeg,不然ffmpeg编译时不会生成 ffplay,因为ffplay需要用到sdl的库呀。

        依次执行如下命令,就OK了。

            apt-get install libsdl2-dev

            apt-get install libsdl2-image-dev

            apt-get install libsdl2-mixer-dev

            apt-get install libsdl2-ttf-dev

            apt-get install libsdl2-gfx-dev

        我们用 apt-get install 的,版本是 2.0.4,执行 sdl2-config --version 看版本号。

 

    2、nasm:编译x264、x265、ffmpeg时用到,要求 2.14以上,用apt-get inatall nasm安装的,版本比较低,不符合要求,还是下载最新的吧。按照上面所说的基本流程下载->解压->进入目录->配置->编译->安装。

        wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz

        tar -jxvf nasm-2.14.02.tar.gz

        cd nasm-2.14.02

        ./configure

        make

        make install

    我没有加 --prefex=//参数,所以nasm 安装在 /usr/bin文件夹下

    安装成功后,执行,nasm --version

    可以看到 NASM version 2.14.02 compiled on Aug 30 2019

 

    3、x264:ffmpeg实现x.264的解码,但无h.264编码器,所以需要第三方h.264编码器,同样下新的。依然按照上面所说的基本流程下载->解压->进入目录->配置->编译->安装。

        wget  http://download.videolan.org/x264/snapshots/last_stable_x264.tar.bz2

        tar -jxvf last_stable_x264.tar.bz2

        cd  x264-snapshot-20190827-2245-stable

        ./configure

        make

        make install

    x264 安装在 /usr/local/bin 文件夹下面

    安装成功后,执行,x264 --version

    可以看到

        x264 0.157.x

        built on Aug 30 2019, gcc: 5.4.0 20160609

        x264 configuration: --chroma-format=all

        libx264 configuration: --chroma-format=all

        x264 license: GPL version 2 or later

 

    4、x265:    

    ffmpeg实现h.265的解码,但无h.265编码器,所以需要第三方h.265编码器,同样下新的。依然按照上面所说的基本流程下载->解压->进入目录->配置->编译->安装。但它的编译安装方式要按照指南来,指南在:https://bitbucket.org/multicoreware/x265/wiki/Home,照做就行了。

    先下载:wget https://bitbucket.org/multicoreware/x265/downloads/x265_3.1.2.tar.gz

    指南告诉我们:

        # ubuntu packages:

        $ sudo apt-get install mercurial cmake cmake-curses-gui build-essential yasm

        # Note: if the packaged yasm is older than 1.2, you must download yasm (1.3 recommended) and build it# If you are compiling off the default             branch after release of v2.6, you must have nasm (2.13 or newer) installed and added to your path

 

        $ hg clone https://bitbucket.org/multicoreware/x265

        $ cd x265/build/linux

        $ ./make-Makefiles.bash

        $ make

 

    看Note,you must have nasm(2.13 or newer),我们下的x265是3.1.2的,要用 nasm 2.13 以上,在第一步已经装了喔。

    那就执行 apt-get install mercurial cmake cmake-curses-gui build-essential 这个就行了。

    执行完之后,

        tar -zxvf x265_3.1.2.tar.gz

        cd /x265_3.1.2/build/linux/

        ./make-Makefiles.bash

        make

    好了。

 

    5、libmp3lame:开源mp3编码器。

        在https://sourceforge.net/projects/lame/files/下载,然后拷贝到ubuntu主机上。 

        tar -zxvf lame-3.100.tar.gz

        cd lame-3.100/

        ./configure

        make

        make install

    好了

 

    6、librtmp:支持rtmp协议的库。

        git clone git://git.ffmpeg.org/rtmpdump

        cd rtmpdump

        这个需要先安装 openssl,执行: 

            apt-get install libssl-dev

            apt-get install openssl

        看一下REAMME,照做就行了。

        make SYS=posix

        make install

    搞拈。

            

    7、ffmpeg:基友2。

        git clone https://git.ffmpeg.org/ffmpeg.git

        cd ffmpeg

        ./configure --enable-shared --enable-static --enable-gpl --enable-pthreads --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-librtmp

        make

        make install

    搞拈,这个编译的时间会有点长喔,请耐心等待,喝杯咖啡,或者喝杯茶,等~

 

    至此,ffmpeg+sdl 环境已经搭好了,验证一下吧。

    ffplay test.mp4 播放一个文件吧,果然看到一个大长腿和狗的视频,如下图~呵呵哒~~