从零开始编译属于你的FFmpeg

it2024-12-19  5

一、前提:

编译FFmpeg可以是初学者,尤其是对C语言项目,Linux编译不熟悉的的初学者的一道门槛。 我曾经找过很多博客,文章,有些能编译成功,有些则不能。编译通过,能够运行也是云里雾里的。其实最好最权威的编译教程应该是官方提供的。 在这里:http://trac.ffmpeg.org/wiki/CompilationGuide强烈建议参考官网步骤一步步编译,本文只是作者的操作记录而已。 注意:如果你只需要FFmpeg的安装包或者可运行的二进制文件,你不需要自己编译源代码,下面的地址有提供:

http://ffmpeg.org/download.html#LinuxBuilds

1.1 下面是我的编译环境:

Ubuntu 14.04ffmpeg-3.3.1 稳定版,下载地址:http://ffmpeg.org/releases/ffmpeg-3.3.1.tar.bz2务必确定自己的 apt 的源是国内的镜像地址,比如阿里云的源,否则apt安装依赖库卡的要死~

二、准备步骤:

2.1 检查自己的gcc版本

输入命令: gcc -v 检查结果:gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

原因

官方提示:

Linux Attention: If you are using gcc-4.2 please read ticket 1464 first, and try to update your compiler. A message to that effect is printed on running configure off the master branch. Compiling FFmpeg on Ubuntu / Debian / Mint Compiling FFmpeg on CentOS / RHEL / Fedora

小结:gcc-4.2 好像有bug,所以避免使用这个版本的gcc编译

2.2 获取安装编译需要的依赖(Get the Dependencies)

参考官方指南:

Copy and paste the whole code box for each step. First install the dependencies:(复制粘贴下面的命令,安装依赖)

sudo apt-get update 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 //Note: Server users can omit the ffplay and x11grab dependencies: libsdl2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev.

小结:这里只需要复制粘贴命令到bash 里面执行即可

2.3 创建FFmpeg源码目录:

Now make a directory for the source files that will be downloaded later in this guide: (在home/your_user_name 创建ffmpeg_sources这个目录用于下载FFmpeg的源码)

mkdir ~/ffmpeg_sources

三、编译与安装

3.1 官方教程:

You can compile ffmpeg to your liking. If you do not require certain encoders you may skip the relevant section and then remove the appropriate ./configure option in FFmpeg. For example, if libopus is not needed, then skip that section and then remove --enable-libopus from the Install FFmpeg section. This guide is designed to be non-intrusive and will create several directories in your home directory:

ffmpeg_sources – Where the source files will be downloaded. This can be deleted if desired when finished with the guide. ffmpeg_build – Where the files will be built and libraries installed. This can be deleted if desired when finished with the guide. bin – Where the resulting binaries (ffmpeg, ffplay, ffserver, x264, and yasm) will be installed. You can easily undo any of this as shown in Reverting Changes Made by This Guide.

中文翻译:

您可以根据自己的喜好编译ffmpeg。如果您不需要某些编码器,可以跳过下面指南中关于这个编码器的部分,然后在FFmpeg 中删除相应的./configure选项。例如,如果不需要libopus,请跳过该部分,然后从Install FFmpeg部分删除--enable-libopus。 本指南希望做到非侵入性,并只会在您的主目录(/home/your_user_name)中创建几个目录 ffmpeg_sources - 源文件将在哪里下载。如果需要,可以在导板完成后删除。 ffmpeg_build - 文件将在哪里构建和库安装。如果需要,可以在导板完成后删除。 bin - 将安装结果二进制文件(ffmpeg,ffplay,ffserver,x264和yasm)。 您可以轻松地撤消任何这一点,如“本指南”中的“ 恢复更改”所示。

3.2 安装yasm 用于FFmpeg官方教程:

Yasm

直接安装yasm An assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow. If your repository provides yasm version ≥ 1.2.0 then you can install that instead of compiling: sudo apt-get install yasm // 不直接安装yasm,你可以选择编译yasm源码,安装(建议直接安装即可,跳过这里) //Otherwise you can compile: cd ~/ffmpeg_sources wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz tar xzvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" make make install

中文翻译:

Yasm 一个在x86平台的优化的汇编程序,用于 x264库和FFmpeg。强烈推荐使用,否则你生成编码的速度可能非常慢 如果你的系统提供版本号>1.02.0的yasm,你可以直接安装,否则你需要自己编译新版本的yasm

3.1.1 关于yasm的补充说明

yasm是汇编编译器,因为ffmpeg中为了提高效率用到了汇编指令,比如MMX和SSE Ubuntu下直接使用:sudo apt-get install yasm 安装

小结:

这里需要安装yasm,我采取直接安装的方法,省去了自己编译源码使用的麻烦。实测Ubuntu 14.04 apt-get install yasm的版本是1.2.0的

3.3 下载FFmpeg各种编码器(可选,我自己跳过这一步,切记如果你需要下面的编码器,务必自己下载)

libx264

H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with --enable-gpl --enable-libx264.

If your repository provides libx264-dev version ≥ 118 then you can install that instead of compiling:

sudo apt-get install libx264-dev //Otherwise you can compile: cd ~/ffmpeg_sources wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2 tar xjvf last_x264.tar.bz2 cd x264-snapshot* PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static --disable-opencl PATH="$HOME/bin:$PATH" make make install

libx265

H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.

If your repository provides libx265-dev version ≥ 68 then you can install that instead of compiling:

sudo apt-get install libx265-dev //Otherwise you can compile: sudo apt-get install cmake mercurial cd ~/ffmpeg_sources hg clone https://bitbucket.org/multicoreware/x265 cd ~/ffmpeg_sources/x265/build/linux PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source make make install

libfdk-aac

AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with --enable-libfdk-aac (and --enable-nonfree if you also included --enable-gpl).

If your repository provides libfdk-aac-dev then you can install that instead of compiling:

sudo apt-get install libfdk-aac-dev //Otherwise you can compile: cd ~/ffmpeg_sources wget -O fdk-aac.tar.gz https://github.com/mstorsjo/fdk-aac/tarball/master tar xzvf fdk-aac.tar.gz cd mstorsjo-fdk-aac* autoreconf -fiv ./configure --prefix="$HOME/ffmpeg_build" --disable-shared make make install

libmp3lame

MP3 audio encoder.

Requires ffmpeg to be configured with --enable-libmp3lame.

If your repository provides libmp3lame-dev version ≥ 3.98.3 then you can install that instead of compiling:

sudo apt-get install libmp3lame-dev //Otherwise you can compile: sudo apt-get install nasm cd ~/ffmpeg_sources wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz tar xzvf lame-3.99.5.tar.gz cd lame-3.99.5 ./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared make make install

libopus

Opus audio decoder and encoder.

Requires ffmpeg to be configured with --enable-libopus.

If your repository provides libopus-dev version ≥ 1.1 then you can install that instead of compiling:

sudo apt-get install libopus-dev //Otherwise you can compile: cd ~/ffmpeg_sources wget http://downloads.xiph.org/releases/opus/opus-1.1.4.tar.gz tar xzvf opus-1.1.4.tar.gz cd opus-1.1.4 ./configure --prefix="$HOME/ffmpeg_build" --disable-shared make make install

libvpx

VP8/VP9 video encoder and decoder. See the VP8 Video Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with --enable-libvpx.

If your repository provides libvpx-dev version ≥ 0.9.7 then you can install that instead of compiling:

sudo apt-get install libvpx-dev //Otherwise you can compile: cd ~/ffmpeg_sources wget http://storage.googleapis.com/downloads.webmproject.org/releases/webm/libvpx-1.6.1.tar.bz2 tar xjvf libvpx-1.6.1.tar.bz2 cd libvpx-1.6.1 PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests PATH="$HOME/bin:$PATH" make make install

小结:

FFmpeg 默认支持部分编码器,但是有一些并不属于FFmpeg的编码器,需要我们自己按照官方的指南下载源码编译,FFMPEG才能支持。由于暂时不学习编码器,~这一部分可耻跳过。注意这里跳过以后 下面的编译参数需要同步修改,去掉不支持的编码器

3.4 下载并编译FFmpeg的源码:

官方指南:

ffmpeg cd ~/ffmpeg_sources

wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 tar xjvf ffmpeg-snapshot.tar.bz2 cd ffmpeg PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \ --prefix="$HOME/ffmpeg_build" \ --pkg-config-flags="--static" \ --extra-cflags="-I$HOME/ffmpeg_build/include" \ --extra-ldflags="-L$HOME/ffmpeg_build/lib" \ --bindir="$HOME/bin" \ --enable-gpl \ --enable-libass \ --enable-libfdk-aac \ --enable-libfreetype \ --enable-libmp3lame \ --enable-libopus \ --enable-libtheora \ --enable-libvorbis \ --enable-libvpx \ --enable-libx264 \ --enable-libx265 \ --enable-nonfree PATH="$HOME/bin:$PATH" make make install hash -r

小结:官方提供的配置 --enable-xxxxx 这种选项,如果你没有需要这一个编码器,或者压根就没下载安装,把这个参数去掉即可

更新 FFmpeg (Updating FFmpeg )

Development of FFmpeg is active and an occasional update can give you new features and bug fixes. First you need to delete (or move) the old files:

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265} Now just follow the guide from the beginning.

撤销所有的上面的指南对系统的所有改动(Reverting Changes Made by This Guide)

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265,nasm} sudo apt-get autoremove autoconf automake build-essential cmake libass-dev libfreetype6-dev \ libmp3lame-dev libopus-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev \ libvorbis-dev libvpx-dev libx264-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev mercurial texinfo zlib1g-dev sed -i '/ffmpeg_build/c\' ~/.manpath hash -r

个人实践补充:

第一步下载并解压源码:

wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 tar xjvf ffmpeg-snapshot.tar.bz2

下载源码的方式很多,官方指南中的 使用wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2t 去官方网址下载发型版本的快照版本 恩,直接到官网 http://ffmpeg.org/download.html#LinuxBuilds 下载源码压缩包,在复制粘贴到ffmpeg_sources也是一样的。 如果是正式产品使用可以考虑使用发行版本中的稳定版,比如这个:http://ffmpeg.org/releases/ffmpeg-3.3.1.tar.bz2 截止2017.5.31 我是在windows下使用百度云离线下载了ffmpeg-3.3.1.tar.bz2 ,再copy到linux下的,比较快~

开始编译:

由于上面我没有下载部分编码器,所以在官方的./configure 后面的参数做了修改,去掉了不支持编码器

cd ffmpeg PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \ --prefix="$HOME/ffmpeg_build" \ --pkg-config-flags="--static" \ --extra-cflags="-I$HOME/ffmpeg_build/include" \ --extra-ldflags="-L$HOME/ffmpeg_build/lib" \ --bindir="$HOME/bin" \ --enable-gpl \ --enable-libass \ --enable-libfreetype \ --enable-libtheora \ --enable-libvorbis \ --enable-nonfree PATH="$HOME/bin:$PATH" make make install hash -r

安装了h264 +aac编码库后,配置的参数:

/configure \ --prefix="$HOME/ffmpeg_build" \ --pkg-config-flags="--static" \ --extra-cflags="-I$HOME/ffmpeg_build/include" \ --extra-ldflags="-L$HOME/ffmpeg_build/lib" \ --bindir="$HOME/bin" \ --enable-gpl \ --enable-libass \ --enable-libfdk-aac \ --enable-libfreetype \ --enable-libtheora \ --enable-libvorbis \ --enable-libx264 \ --enable-nonfree

编译成功,在当前用户的 /home/用户名下,

可以找到 bin 文件夹 ffmpeg_build 文件夹

bin文件夹

ffmpeg的几个二进制可执行文件(程序),每一个都可以直接运行

ffmpeg_build 文件夹

FFmpeg编译中产生的链接库,头文件,还有一些其他文档,如果你需要对FFmpeg二次开发,需要使用到这里的东西

四、总结:

不要在浪费时间去百度各种FFmpeg编译教程了,直接官网走下来,行云流水,如果你遇到问题,可以看看我的文章~。

转载于:https://www.cnblogs.com/bylijian/p/6971225.html

最新回复(0)