原文连接: https://blog.csdn.net/qq1124034012/article/details/94555466
官网指导编译文档http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
本文依据官方文档,根据自身需求定制编译选项,由于目前只需要和视频编码有关的第三方库,所以和音频有关的第三库就不编译。又因为项目涉及到需要调用FFmpeg库生成的可执行文件需是静态链接,而目前暂时无法解决硬解的静态链接,故在编译FFmpeg源码中去掉和硬解相关的所有选项,在文末中可参考我的调用ffmpge库的cmake文件可,已通过测试,生成的可执行文件是静态链接文件
一、编译前准备
1.编译环境
查看系统信息uname -a or lsb_release -a
Linux as2 4.4.0-142-generic #168-Ubuntu SMP Wed Jan 16 21:00:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
2.安装依赖包
sudo apt-get update -qq && sudo apt-get -y install \
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
备注:服务器用户可以省略ffplay和x11grab依赖项:libsdl2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev
3.新建编译目录
官网文档是新建/bin存放二进制文件,我自己是/ffmpeg_bin,以下都是~/ffmpeg_bin
mkdir -p ~/ffmpeg_sources ~/ffmpeg_bin ~/ffmpeg_bin
二、第三方库编译安装
本指南假设您希望安装一些最常见的第三方库。每个部分都为您提供安装该库所需的命令。
对于每个部分,将整个代码块复制粘贴到shell中。
如果不需要某些特性,则可以跳过相关部分(如果不需要),然后删除适当的./配置在FFmpeg中的选项。例如,如果不需要libvpx,请跳过该部分,然后删除。-启用-libvpx从安装FFmpeg部分。
提示:若要显著加快具有多个核的系统上的编译过程,可以使用-j每个选项制作,使命令,例如Make-J4.
1.NASM
汇编编译器
cd ~/ffmpeg_sources &&
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.bz2 &&
tar xjvf nasm-2.14.02.tar.bz2 &&
cd nasm-2.14.02 &&
./autogen.sh &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” ./configure --prefix=“H O M E / f f m p e g b u i l d " − − b i n d i r = " HOME/ffmpeg_build" --bindir="HOME/ffmpegbuild"−−bindir="HOME/ffmpeg_bin” &&
make -j4 &&
make install
2.Ysam
汇编编译器
cd ~/ffmpeg_sources &&
wget -O yasm-1.3.0.tar.gz https://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=“H O M E / f f m p e g b u i l d " − − b i n d i r = " HOME/ffmpeg_build" --bindir="HOME/ffmpegbuild"−−bindir="HOME/ffmpeg_bin” &&
make -j4 &&
make install
3.libx264
cd ~/ffmpeg_sources &&
git -C x264 pull 2> /dev/null || git clone --depth 1 https://code.videolan.org/videolan/x264.git &&
cd x264 &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” PKG_CONFIG_PATH=“H O M E / f f m p e g b u i l d / l i b / p k g c o n f i g " . / c o n f i g u r e − − p r e f i x = " HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="HOME/ffmpegbuild/lib/pkgconfig"./configure−−prefix="HOME/ffmpeg_build” --bindir=“KaTeX parse error: Expected 'EOF', got '&' at position 47: …c --enable-pic &̲& \ PATH="HOME/ffmpeg_bin:$PATH” make -j4 &&
make install
4.libx265
sudo apt-get install mercurial libnuma-dev &&
cd ~/ffmpeg_sources &&
if cd x265 2> /dev/null; then hg pull && hg update && cd …; else hg clone https://bitbucket.org/multicoreware/x265; fi &&
cd x265/build/linux &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” cmake -G “Unix Makefiles” -DCMAKE_INSTALL_PREFIX=“KaTeX parse error: Expected 'EOF', got '&' at position 53: …f ../../source &̲& \ PATH="HOME/ffmpeg_bin:$PATH” make -j4 &&
make install
5.libvpx(暂时不需要)
cd ~/ffmpeg_sources &&
git -C libvpx pull 2> /dev/null || git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git &&
cd libvpx &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” ./configure --prefix=“KaTeX parse error: Expected 'EOF', got '&' at position 96: …epth --as=yasm &̲& \ PATH="HOME/ffmpeg_bin:$PATH” make -j4 &&
make install
6.libfdk-aac(暂时不需要)
cd ~/ffmpeg_sources &&
git -C fdk-aac pull 2> /dev/null || git clone --depth 1 https://github.com/mstorsjo/fdk-aac &&
cd fdk-aac &&
autoreconf -fiv &&
./configure --prefix="$HOME/ffmpeg_build" --disable-shared &&
make -j4 &&
make install
7.libmp3lame(暂时不需要)
cd ~/ffmpeg_sources &&
wget -O lame-3.100.tar.gz https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz &&
tar xzvf lame-3.100.tar.gz &&
cd lame-3.100 &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” ./configure --prefix=“H O M E / f f m p e g b u i l d " − − b i n d i r = " HOME/ffmpeg_build" --bindir="HOME/ffmpegbuild"−−bindir="HOME/ffmpeg_bin” --disable-shared --enable-nasm &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” make -j4 &&
make install
8.libopus(暂时不需要)
cd ~/ffmpeg_sources &&
git -C opus pull 2> /dev/null || git clone --depth 1 https://github.com/xiph/opus.git &&
cd opus &&
./autogen.sh &&
./configure --prefix="$HOME/ffmpeg_build" --disable-shared &&
make -j4 &&
make install
9.libaom(暂时不需要)
cd ~/ffmpeg_sources &&
git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom &&
mkdir -p aom_build &&
cd aom_build &&
PATH=“H O M E / f f m p e g b i n : HOME/ffmpeg_bin:HOME/ffmpegbin:PATH” cmake -G “Unix Makefiles” -DCMAKE_INSTALL_PREFIX=“KaTeX parse error: Expected 'EOF', got '&' at position 64: …NASM=on ../aom &̲& \ PATH="HOME/ffmpeg_bin:$PATH” make &&
make install
三、FFmpeg编译安装
查看./configure --help,根据自身需求选择编译选项,目前我无法解决如果开启硬解功能,无法链接到和硬解相关的静态库,故关掉和硬解相关的所有选项。
如果没有特殊需求,直接抄官方编译文档就行了。
cd ~/ffmpeg_sources && \ wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 && \ tar xjvf ffmpeg-snapshot.tar.bz2 && \ cd ffmpeg && \ PATH="$HOME/ffmpeg_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" \ --extra-libs="-lpthread -lm" \ --bindir="$HOME/ffmpeg_bin" \ --enable-gpl \ --enable-libx264 \ --enable-libx265 \ --disable-amf \ --disable-audiotoolbox \ --disable-cuvid \ --disable-d3d11va \ --disable-dxva2 \ --disable-ffnvcodec \ --disable-nvdec \ --disable-nvenc \ --disable-v4l2-m2m \ --disable-vaapi \ --disable-vdpau \ --disable-videotoolbox \ --enable-libfreetype \ --enable-nonfree && \ PATH="$HOME/ffmpeg_binn:$PATH" make -j4 && \ make install && \ hash -r
四、使用FFmpeg库
在**/ffmpeg_build**文件中可找到头文件和lib库,重点关注**ffmpeg_build/lib/pkgconfig**文件夹中的一大堆pc文件,如果要写cmake文件或者makefile文件都要参考这些pc文件,pc文件保存了ffmpeg库所需的lib库,无论是静态链接还是动态链接编写,都要参考。这是我的cmake静态链接文件,目前写的比较粗糙,下一版本打算把和FFmpeg相关的cmake文件封装成独立的cmake 包,如果是编写Makefile文件,参考官方自带的~ffmpeg_build/share/ffmpeg/examples/Makefile文件。
set(PROJECT_NAME rtsptool)
project(${PROJECT_NAME} CXX)
set(SRC_LIST main.cpp)
# C++11
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-static ${CMAKE_CXX_FLAGS}")
include_directories("/home/lh/git/rtsptool/src/third")
include_directories("/home/lh/git/rtsptool/src/third/ffmpeg")
# 设定执行文件生成路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "~/git/rtsptool/bin")
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libavformat.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libavfilter.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libavcodec.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libswresample.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libavutil.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libswscale.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libx264.a)
target_link_libraries(${PROJECT_NAME} /home/lh/ffmpeg_build/lib/libx265.a)
#libavformat
target_link_libraries(${PROJECT_NAME} "z" "m" )
#ibavfilter
target_link_libraries(${PROJECT_NAME} "pthread" "m" "va" "freetype" "z" "png12" "z" "m" )
#libavcodec
target_link_libraries(${PROJECT_NAME} "pthread" "m" "z" "pthread" "m" "dl" "stdc++" "m" "rt" "dl" "numa" )
#libswresample
target_link_libraries(${PROJECT_NAME} "m")
#libavutil
target_link_libraries(${PROJECT_NAME} "pthread" "m" "Xv" "X11" "Xext" )
#libswscale
target_link_libraries(${PROJECT_NAME} "m" )
#libx264
target_link_libraries(${PROJECT_NAME} "pthread" "m" "dl" )
#libx265
target_link_libraries(${PROJECT_NAME} "stdc++" "m" "rt" "dl" "numa" )