• ffmpeg概述


    相关网址


    编译

    ffmpeg编译可以有很多东西,你可以添加需要的扩展,笔者这里只按照笔者的需求来添加。
    笔者编译的版本,x264扩展,ffplay播放器。

    如果你的/usr/local/lib路径没有加入到动态库路径,请先执行下面的指令,增加到系统环境中去。

    export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
    
    • 1

    编译步骤

    • 安装x264
    • 安装SDL
    • 编译ffmpeg

    Ubuntu

    系统安装依赖

    sudo apt install ubuntu-restricted-extras
    sudo apt install libx11-dev
    sudo apt install xorg-dev
    sudo apt install nasm
    sudo apt install yasm
    
    • 1
    • 2
    • 3
    • 4
    • 5

    SDL

    // 安装SDL,这里推荐用命令安装SDL2的库,笔者试过,自行编译的SDL库会存在问题。
    apt-get install libsdl2-2.0			
    apt-get install libsdl2-dev
    
    • 1
    • 2
    • 3

    关于笔者编译SDL不能使用的问题,具体原因笔者也未知,但从ldd查看相关联的库来看,通过命令安装的SDL,会链接到许多相关的库上,而笔者编译出来的库则缺少了很多链接。
    ps:如果有人知道这个是什么问题,笔者可能是编译的一个条件少了,也可能是其它原因,如果你知道,那么希望可以给笔者解解惑。

    • apt安装的SDL依赖
    jie@jie-virtual-machine:/usr/lib/x86_64-linux-gnu$ ldd  libSDL2-2.0.so
    	linux-vdso.so.1 (0x00007fffac966000)
    	libasound.so.2 => /lib/x86_64-linux-gnu/libasound.so.2 (0x00007f006d6a3000)
    	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f006d5bc000)
    	libpulse.so.0 => /lib/x86_64-linux-gnu/libpulse.so.0 (0x00007f006d567000)
    	libX11.so.6 => /lib/x86_64-linux-gnu/libX11.so.6 (0x00007f006d427000)
    	libXext.so.6 => /lib/x86_64-linux-gnu/libXext.so.6 (0x00007f006d412000)
    	libXcursor.so.1 => /lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f006d404000)
    	libXinerama.so.1 => /lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f006d3ff000)
    	libXi.so.6 => /lib/x86_64-linux-gnu/libXi.so.6 (0x00007f006d3eb000)
    	libXfixes.so.3 => /lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f006d3e3000)
    	libXrandr.so.2 => /lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f006d3d6000)
    	libXss.so.1 => /lib/x86_64-linux-gnu/libXss.so.1 (0x00007f006d3d1000)
    	libXxf86vm.so.1 => /lib/x86_64-linux-gnu/libXxf86vm.so.1 (0x00007f006d3c8000)
    	libdrm.so.2 => /lib/x86_64-linux-gnu/libdrm.so.2 (0x00007f006d3b2000)
    	libgbm.so.1 => /lib/x86_64-linux-gnu/libgbm.so.1 (0x00007f006d3a1000)
    	libwayland-egl.so.1 => /lib/x86_64-linux-gnu/libwayland-egl.so.1 (0x00007f006d39c000)
    	libwayland-client.so.0 => /lib/x86_64-linux-gnu/libwayland-client.so.0 (0x00007f006d38b000)
    	libwayland-cursor.so.0 => /lib/x86_64-linux-gnu/libwayland-cursor.so.0 (0x00007f006d381000)
    	libxkbcommon.so.0 => /lib/x86_64-linux-gnu/libxkbcommon.so.0 (0x00007f006d338000)
    	libdecor-0.so.0 => /lib/x86_64-linux-gnu/libdecor-0.so.0 (0x00007f006d32e000)
    	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f006d000000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007f006d95a000)
    	libpulsecommon-15.99.so => /usr/lib/x86_64-linux-gnu/pulseaudio/libpulsecommon-15.99.so (0x00007f006d2a9000)
    	libdbus-1.so.3 => /lib/x86_64-linux-gnu/libdbus-1.so.3 (0x00007f006d25b000)
    	libxcb.so.1 => /lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f006d22f000)
    	libXrender.so.1 => /lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f006cff3000)
    	libwayland-server.so.0 => /lib/x86_64-linux-gnu/libwayland-server.so.0 (0x00007f006cfdd000)
    	libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f006cfac000)
    	libffi.so.8 => /lib/x86_64-linux-gnu/libffi.so.8 (0x00007f006cf9f000)
    	libsndfile.so.1 => /lib/x86_64-linux-gnu/libsndfile.so.1 (0x00007f006cf20000)
    	libX11-xcb.so.1 => /lib/x86_64-linux-gnu/libX11-xcb.so.1 (0x00007f006d228000)
    	libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f006ce59000)
    	libasyncns.so.0 => /lib/x86_64-linux-gnu/libasyncns.so.0 (0x00007f006ce51000)
    	libapparmor.so.1 => /lib/x86_64-linux-gnu/libapparmor.so.1 (0x00007f006ce3c000)
    	libXau.so.6 => /lib/x86_64-linux-gnu/libXau.so.6 (0x00007f006ce36000)
    	libXdmcp.so.6 => /lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f006ce2c000)
    	libFLAC.so.8 => /lib/x86_64-linux-gnu/libFLAC.so.8 (0x00007f006cdf0000)
    	libvorbis.so.0 => /lib/x86_64-linux-gnu/libvorbis.so.0 (0x00007f006cdc3000)
    	libvorbisenc.so.2 => /lib/x86_64-linux-gnu/libvorbisenc.so.2 (0x00007f006cd18000)
    	libopus.so.0 => /lib/x86_64-linux-gnu/libopus.so.0 (0x00007f006ccba000)
    	libogg.so.0 => /lib/x86_64-linux-gnu/libogg.so.0 (0x00007f006ccaf000)
    	liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f006cc82000)
    	libzstd.so.1 => /lib/x86_64-linux-gnu/libzstd.so.1 (0x00007f006cbb3000)
    	liblz4.so.1 => /lib/x86_64-linux-gnu/liblz4.so.1 (0x00007f006cb93000)
    	libcap.so.2 => /lib/x86_64-linux-gnu/libcap.so.2 (0x00007f006cb88000)
    	libgcrypt.so.20 => /lib/x86_64-linux-gnu/libgcrypt.so.20 (0x00007f006ca4a000)
    	libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f006ca30000)
    	libgpg-error.so.0 => /lib/x86_64-linux-gnu/libgpg-error.so.0 (0x00007f006ca0a000)
    	libmd.so.0 => /lib/x86_64-linux-gnu/libmd.so.0 (0x00007f006c9fd000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 笔者编译的SDL依赖,这差距一目了然。
    jie@jie-virtual-machine:~/user/lib_source/SDL/build/.libs$ ldd libSDL2-2.0.so.0
    	linux-vdso.so.1 (0x00007ffeb65db000)
    	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea9a919000)
    	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea9a600000)
    	/lib64/ld-linux-x86-64.so.2 (0x00007fea9ac7c000)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    所以这里不推荐自行编译SDL,如果你发现自行编译的用了出现一些问题,比如打不开播放,比如莫名其妙的崩溃之类的,请卸载自行编译的SDL,安装前面apt的SDL。
    ps:make的卸载指令, sudo make uninstall

    x264

    关于x264的安装比较简单,下载源码,切入到源码目录。

    x264目录,查看到configure执行脚本

    jie@jie-virtual-machine:~/user/lib_source/x264-master$ ls -l
    total 420
    -rw-rw-r-- 1 jie jie  1999  1月 29  2023 AUTHORS
    -rw-rw-r-- 1 jie jie 10641  1月 29  2023 autocomplete.c
    drwxrwxr-x 8 jie jie  4096 10月  2 10:53 common
    -rwxr-xr-x 1 jie jie 45147  1月 29  2023 config.guess
    -rw-rw-r-- 1 jie jie   983 10月  1 13:13 config.h
    -rw-rw-r-- 1 jie jie  9006 10月  1 13:13 config.log
    -rw-rw-r-- 1 jie jie  1196 10月  1 13:13 config.mak
    -rwxr-xr-x 1 jie jie 35777  1月 29  2023 config.sub
    -rwxr-xr-x 1 jie jie 54257  1月 29  2023 configure
    -rw-rw-r-- 1 jie jie 17992  1月 29  2023 COPYING
    drwxrwxr-x 2 jie jie  4096  1月 29  2023 doc
    drwxrwxr-x 2 jie jie  4096 10月  2 10:53 encoder
    -rw-rw-r-- 1 jie jie  4202  1月 29  2023 example.c
    drwxrwxr-x 2 jie jie  4096  1月 29  2023 extras
    drwxrwxr-x 3 jie jie  4096 10月  2 10:53 filters
    drwxrwxr-x 2 jie jie  4096 10月  2 10:53 input
    -rw-rw-r-- 1 jie jie 13355  1月 29  2023 Makefile
    drwxrwxr-x 2 jie jie  4096 10月  2 10:53 output
    drwxrwxr-x 3 jie jie  4096  1月 29  2023 tools
    -rwxr-xr-x 1 jie jie   854  1月 29  2023 version.sh
    -rw-rw-r-- 1 jie jie 92653  1月 29  2023 x264.c
    -rw-rw-r-- 1 jie jie  3182  1月 29  2023 x264cli.h
    -rw-rw-r-- 1 jie jie   172 10月  1 13:13 x264_config.h
    -rw-rw-r-- 1 jie jie  1910  1月 29  2023 x264dll.c
    -rw-rw-r-- 1 jie jie 48650  1月 29  2023 x264.h
    -rw-rw-r-- 1 jie jie   286 10月  1 13:13 x264.pc
    -rw-rw-r-- 1 jie jie   572  1月 29  2023 x264res.manifest
    -rw-rw-r-- 1 jie jie  2709  1月 29  2023 x264res.rc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    编译&安装

    ./configure --enable-shared			// 执行配置
    make -j 8							// 编译
    sudo make install					// 安装库系统环境
    
    • 1
    • 2
    • 3

    ffmpeg

    下载之后,切换到ffmpeg目录中去。

    ffmpeg目录,找到configure ps:我这里是编译好的目录,所以你没有编译的不同。

    jie@jie-virtual-machine:~/user/lib_source/ffmpeg$ ls -l
    total 3652
    -rw-rw-r--  1 jie jie   74753 10月  1 13:02 Changelog
    drwxrwxr-x 11 jie jie    4096 10月  1 13:02 compat
    -rw-rw-r--  1 jie jie   20076 10月  1 13:22 config.asm
    -rw-rw-r--  1 jie jie   68687 10月  1 13:22 config_components.h
    -rw-rw-r--  1 jie jie   20683 10月  1 13:22 config.h
    -rwxrwxr-x  1 jie jie  269339 10月  1 13:02 configure
    -rw-rw-r--  1 jie jie     418 10月  1 13:02 CONTRIBUTING.md
    -rw-rw-r--  1 jie jie   18092 10月  1 13:02 COPYING.GPLv2
    -rw-rw-r--  1 jie jie   35147 10月  1 13:02 COPYING.GPLv3
    -rw-rw-r--  1 jie jie   26526 10月  1 13:02 COPYING.LGPLv2.1
    -rw-rw-r--  1 jie jie    7651 10月  1 13:02 COPYING.LGPLv3
    -rw-rw-r--  1 jie jie     277 10月  1 13:02 CREDITS
    drwxrwxr-x  5 jie jie    4096 10月  1 13:25 doc
    drwxrwxr-x  2 jie jie    4096 10月  1 13:22 ffbuild
    -rwxrwxr-x  1 jie jie  289096 10月  1 13:25 ffmpeg
    -rwxrwxr-x  1 jie jie 1045968 10月  1 13:25 ffmpeg_g
    -rwxrwxr-x  1 jie jie  145720 10月  1 13:25 ffplay
    -rwxrwxr-x  1 jie jie  524072 10月  1 13:25 ffplay_g
    -rwxrwxr-x  1 jie jie  179048 10月  1 13:25 ffprobe
    -rwxrwxr-x  1 jie jie  655168 10月  1 13:25 ffprobe_g
    drwxrwxr-x  2 jie jie    4096 10月  1 13:25 fftools
    -rw-rw-r--  1 jie jie     596 10月  1 13:02 INSTALL.md
    drwxrwxr-x 15 jie jie  114688 10月  1 13:25 libavcodec
    drwxrwxr-x  3 jie jie    4096 10月  1 13:25 libavdevice
    drwxrwxr-x  9 jie jie   36864 10月  1 13:25 libavfilter
    drwxrwxr-x  3 jie jie   45056 10月  1 13:25 libavformat
    drwxrwxr-x 13 jie jie   16384 10月  1 13:25 libavutil
    drwxrwxr-x  2 jie jie    4096 10月  1 13:25 libpostproc
    drwxrwxr-x  6 jie jie    4096 10月  1 13:25 libswresample
    drwxrwxr-x  7 jie jie    4096 10月  1 13:25 libswscale
    -rw-rw-r--  1 jie jie    4376 10月  1 13:02 LICENSE.md
    -rw-rw-r--  1 jie jie   31223 10月  1 13:02 MAINTAINERS
    -rw-rw-r--  1 jie jie    6549 10月  1 13:02 Makefile
    drwxrwxr-x  2 jie jie    4096 10月  1 13:02 presets
    -rw-rw-r--  1 jie jie    1810 10月  1 13:02 README.md
    -rw-rw-r--  1 jie jie       6 10月  1 13:02 RELEASE
    -rw-rw-r--  1 jie jie    1076 10月  1 13:02 RELEASE_NOTES
    drwxrwxr-x  7 jie jie    4096 10月  1 13:02 tests
    drwxrwxr-x  3 jie jie    4096 10月  1 13:02 tools
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    编译&安装

    ./configure --enable-shared --enable-libx264 --enable-gpl --enable-sdl2 --enable-ffplay		// ffmpeg 编译参数参考,这里需要安装x264
    make -j 8			// 编译
    sudo make install	// 安装
    
    • 1
    • 2
    • 3

    Windows

    windows编译的话,要使用msys2,所以如果你没有装msys2,请先安装msys2。
    windows编译有mingw & msvc,mingw相对来说会比msvc简单一些,具体看个人需要,可以选择不同的编译方式。即便是mingw编译出来的库,msvc也是可以通用的,所以编译mingw版本即可,会相对简单许多。

    本次编译ffmpeg拉取的库为

    git clone -b release/5.1 https://github.com/FFmpeg/FFmpeg.git
    
    • 1

    ps: 拉取库不同可能会导致编译出错,所以如果你按照笔者的方式走下去,编译出错了先检查是不是源码的问题,换一份源码编可能就没问题了。

    修改msys2的配置

    • 换源 ps: 这一个不是必须,这里只是为了提速换成国内源
      找到安装msys2的根目录,然后切换到${msys2}\etc\pacman.d,目录中去。

    目录文件,找到 mirrorlist.msys & 2137 mirrorlist.mingw64 & 2071 mirrorlist.mingw32文件

    PS D:\it_sys\msys2\etc\pacman.d> ls
     目录: D:\it_sys\msys2\etc\pacman.d
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    d-----         2023/10/1     21:53                gnupg
    -a----        2022/10/16     22:05           2051 mirrorlist.clang32
    -a----        2022/10/16     22:05           2051 mirrorlist.clang64
    -a----        2022/10/16     22:05           1985 mirrorlist.mingw
    -a----         2023/10/1     22:48           2071 mirrorlist.mingw32
    -a----         2023/10/1     22:48           2137 mirrorlist.mingw64
    -a----         2023/10/1     22:48           2071 mirrorlist.msys
    -a----        2022/10/16     22:05           2018 mirrorlist.ucrt64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    国内源

    Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/
    Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
    
    • 1
    • 2

    在开头添加国内源到 mirrorlist.msys & 2137 mirrorlist.mingw64 & 2071 mirrorlist.mingw32 文件中去

    # See https://www.msys2.org/dev/mirrors
    
    ## Primary
    Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/
    Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
    
    Server = https://mirror.msys2.org/msys/$arch/
    Server = https://repo.msys2.org/msys/$arch/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 修改启动脚本msys2_shell.cmd
      在msys2的根目录中,可以找到msys2_shell.cmd脚本,该脚本可以用来启动msys2。用文本编辑器打开,将rem set MSYS2_PATH_TYPE=inherit修改为set MSYS2_PATH_TYPE=inherit,rem其实是注释,这个操作就是将这个注释打开。
      ps: 笔者这里在17行的位置,你不一定和笔者的位置一致。

    修改后的

    rem To export full current PATH from environment into MSYS2 use '-use-full-path' parameter
    rem or uncomment next line
    set MSYS2_PATH_TYPE=inherit
    
    • 1
    • 2
    • 3

    打开msys2

    使用命令行cmd或者powershell,或者msvc命令,你想继承谁的环境就用谁的命令行去打开。
    如果是mingw编译,那么用cmd或者powershell都可以。
    如果是msvc编译,那么用msvc的命令打开。

    .\msys2_shell.cmd -mingw64			// 打开mingw64
    .\msys2_shell.cmd -mingw32			// 打开mingw32
    .\msys2_shell.cmd -msys				// 打开msys
    
    • 1
    • 2
    • 3

    mingw64

    打开msys-mingw64之后,先更新pacman的包。

    更新包
    pacman -Sy 
    
    • 1

    安装基础依赖包

    pacman -S mingw-w64-x86_64-toolchain
    pacman -S git
    pacman -S make
    pacman -S automake 
    pacman -S autoconf
    pacman -S perl
    pacman -S libtool
    pacman -S mingw-w64-x86_64-cmake
    pacman -S pkg-config 
    pacman -S diffutils
    pacman -S yasm
    pacman -S nasm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    安装x264
    pacman -S mingw-w64-x86_64-x264
    
    • 1
    安装SDL

    安装SDL和x264不同,SDL是需要注意版本的,有些版本的ffmepg不支持版本过低的SDL有则是不支持过高的。不过基本上也只能安装pacman给出的版本,如果你想安装低版本的SDL例如2.2.0,可能需要自己编译。

    pacman -Ss SDL			// 查新相关包
    ......
    mingw32/mingw-w64-i686-smpeg2 2.0.0-6
        SDL2 MPEG Player Library (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL 1.2.15+r419+gef3a6c05-1
        A library for portable low-level access to a video framebuffer, audio output, mouse, and
        keyboard (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2 2.28.3-1 [installed]
        A library for portable low-level access to a video framebuffer, audio output, mouse, and
        keyboard (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2_gfx 1.0.4-2
        SDL graphics drawing primitives and other support functions (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2_image 2.6.3-2
        A simple library to load images of various formats as SDL surfaces (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2_mixer 2.6.3-1
        A simple multi-channel audio mixer (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2_net 2.2.0-1 [installed]
        A small sample cross-platform networking library (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL2_ttf 2.20.2-1
        A library that allows you to use TrueType fonts in your SDL applications (Version 2) (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL_gfx 2.0.26-1
        SDL Graphic Primitives (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL_image 1.2.12-8
        A simple library to load images of various formats as SDL surfaces (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL_mixer 1.2.12-9
        A simple multi-channel audio mixer (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL_net 1.2.8-2
        A small sample cross-platform networking library (mingw-w64)
    mingw64/mingw-w64-x86_64-SDL_ttf 2.0.11-6
        A library that allows you to use TrueType fonts in your SDL applications (mingw-w64)
    mingw64/mingw-w64-x86_64-sdl12-compat 1.2.68-1
        SDL 1.2 runtime compatibility library using SDL 2.0
    mingw64/mingw-w64-x86_64-smpeg 0.4.5-5
        SDL MPEG Player Library (mingw-w64)
    mingw64/mingw-w64-x86_64-smpeg2 2.0.0-6
        SDL2 MPEG Player Library (mingw-w64)
    ucrt64/mingw-w64-ucrt-x86_64-SDL 1.2.15+r419+gef3a6c05-1
        A library for portable low-level access to a video framebuffer, audio output, mouse, and
        keyboard (mingw-w64)
    ucrt64/mingw-w64-ucrt-x86_64-SDL2 2.28.3-1
        A library for portable low-level access to a video framebuffer, audio output, mouse, and
        keyboard (Version 2) (mingw-w64)
    ......
    
    pacman -S mingw-w64-x86_64-SDL2			// 安装SDL
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    ffmpeg

    进入ffmpeg目录,查看到configure执行文件

    jie@LAPTOP-TDJ5MUQ6 MINGW64 ~/ffmpeg
    $ ls -l
    total 5948
    -rw-r--r-- 1 jie None     418 Oct  1 23:27 CONTRIBUTING.md
    -rw-r--r-- 1 jie None   18092 Oct  1 23:27 COPYING.GPLv2
    -rw-r--r-- 1 jie None   35147 Oct  1 23:27 COPYING.GPLv3
    -rw-r--r-- 1 jie None   26526 Oct  1 23:27 COPYING.LGPLv2.1
    -rw-r--r-- 1 jie None    7651 Oct  1 23:27 COPYING.LGPLv3
    -rw-r--r-- 1 jie None     277 Oct  1 23:27 CREDITS
    -rw-r--r-- 1 jie None   74753 Oct  1 23:27 Changelog
    -rw-r--r-- 1 jie None     596 Oct  1 23:27 INSTALL.md
    -rw-r--r-- 1 jie None    4376 Oct  1 23:27 LICENSE.md
    -rw-r--r-- 1 jie None   31223 Oct  1 23:27 MAINTAINERS
    -rw-r--r-- 1 jie None    6549 Oct  1 23:27 Makefile
    -rw-r--r-- 1 jie None    1810 Oct  1 23:27 README.md
    -rw-r--r-- 1 jie None       6 Oct  1 23:27 RELEASE
    -rw-r--r-- 1 jie None    1076 Oct  1 23:27 RELEASE_NOTES
    drwxr-xr-x 1 jie None       0 Oct  1 23:27 compat
    -rw-r--r-- 1 jie None   20076 Oct  1 23:30 config.asm
    -rw-r--r-- 1 jie None   20690 Oct  1 23:30 config.h
    -rw-r--r-- 1 jie None   68687 Oct  1 23:30 config_components.h
    -rwxr-xr-x 1 jie None  269339 Oct  1 23:27 configure
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 doc
    drwxr-xr-x 1 jie None       0 Oct  1 23:30 ffbuild
    -rwxr-xr-x 1 jie None  325632 Oct  1 23:35 ffmpeg.exe
    -rwxr-xr-x 1 jie None 1613628 Oct  1 23:35 ffmpeg_g.exe
    -rwxr-xr-x 1 jie None  153600 Oct  1 23:35 ffplay.exe
    -rwxr-xr-x 1 jie None  843463 Oct  1 23:35 ffplay_g.exe
    -rwxr-xr-x 1 jie None  183808 Oct  1 23:35 ffprobe.exe
    -rwxr-xr-x 1 jie None  935909 Oct  1 23:35 ffprobe_g.exe
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 fftools
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libavcodec
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libavdevice
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libavfilter
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libavformat
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libavutil
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libpostproc
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libswresample
    drwxr-xr-x 1 jie None       0 Oct  1 23:35 libswscale
    drwxr-xr-x 1 jie None       0 Oct  1 23:27 presets
    drwxr-xr-x 1 jie None       0 Oct  1 23:27 tests
    drwxr-xr-x 1 jie None       0 Oct  1 23:27 tools
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    编译 & 安装

    ./configure --enable-shared --enable-libx264 --enable-gpl --enable-sdl2 --enable-ffplay
    make -j 8
    make install
    
    • 1
    • 2
    • 3

    ps: 这里安装到的msys-mingw64的环境里面去的,如果你想在外面执行,还需要自行找到相关的库和头文件进行手动打包。

    兼容性说明
    如果目标对象是以C语言编译出来的,那么在windows平台,mingw和msvc都是可以通用的。它们之间不会产生,符号链接上的歧义。
    所以,即便是mingw编译出来的ffmpeg也是可以由msvc使用的。不过msvc和mingw库的后缀命名有些不同,mingw以.a为后缀,而msvc则是以.lib作为后缀,但两者是一样的。
    不过为了方便使用,你最好还是复制链接文件然后重命名一下给msvc使用。
    比如以笔者这样子,作为参考示例,.lib的文件都是我复制.dll.a文件然后重命名一下而已。

    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         2023/10/1     23:35         112410 libavcodec.dll.a
    -a----         2023/10/1     23:35         112410 libavcodec.lib
    -a----         2023/10/1     23:35          13518 libavdevice.dll.a
    -a----         2023/10/1     23:35          13518 libavdevice.lib
    -a----         2023/10/1     23:35          42286 libavfilter.dll.a
    -a----         2023/10/1     23:35          42286 libavfilter.lib
    -a----         2023/10/1     23:35         103864 libavformat.dll.a
    -a----         2023/10/1     23:35         103864 libavformat.lib
    -a----         2023/10/1     23:35         384570 libavutil.dll.a
    -a----         2023/10/1     23:35         384570 libavutil.lib
    -a----         2023/10/1     23:35           7514 libpostproc.dll.a
    -a----         2023/10/1     23:35           7514 libpostproc.lib
    -a----         2023/10/1     23:35          17288 libswresample.dll.a
    -a----         2023/10/1     23:35          17288 libswresample.lib
    -a----         2023/10/1     23:35          22050 libswscale.dll.a
    -a----         2023/10/1     23:35          22050 libswscale.lib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    虽然C语言不区分,mingw和msvc编译出来的库,都可以给识别不会产生符号命名歧义。但是c++是严格区分的,mingw编译出来的库就是无法被msvc进行链接。根本原因是,mingw和msvc厂商之间,符号命名规则不同,因此无法兼容。
    啊啊啊,你问笔者,如果有一个对接甲方,只提供了msvc的sdk,但是公司使用的是mingw,那么该怎么办呢?
    方式一,让甲方提供一个mingw的版本。
    方式二,用msvc创建一个c动态库进行内部调用甲方的sdk,然后mingw调用c动态库,以一个c动态库作为中间适配。


    ffmpeg命令

    ffmpeg命令格式ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...
    ffmpeg是一个视频文件编辑的软件,它不是用来播放文件的,适合文件转换,封装,提取之类的操作。如果你需要播放文件,可以使用ffplay进行播放。

    查看帮助

    // 查看命令行帮助 > 是重定向,你也可以不重定向
    ffmpeg -h > ffmpeg_h.txt					// 打印一部分帮助
    ffmpeg -h long > ffmpeg_h_long.txt			// 打印更多帮助
    ffmpeg -h full > ffmpeg_h_full.txt			// 打印完整帮助
    
    // 他们之间的文件大小差距是这样的
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a----         2023/10/3      9:41          10548 ffmpeg_h.txt
    -a----         2023/10/3      9:44        1817436 ffmpeg_h_full.txt
    -a----         2023/10/3      9:44          23578 ffmpeg_h_long.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    常用操作

    // 通过重新编码媒体流,将输入媒体文件转换为不同的格式
    ffmpeg -i [input.avi] [output.mp4]
    
    // 强制设置帧数 -r
    ffmpeg -i input.avi -r [24] [output.mp4]
    
    // 设置码率 -b:v
    ffmpeg -i input.avi -b:v [64k] [output.mp4]
    
    // 设置分辨率 -s 
    ffmpeg -i [.\SampleVideo_720x480_30mb.mkv] -s [1920x1080] [jie_1920x1080.mp4]
    
    // 设置分辨率,-s 不重新编码 -codec copy,copy就是拷贝
    ffmpeg -i [.\SampleVideo_720x480_30mb.mkv] -s [1920x1080] -codec [copy] [jie_1920x1080_copy.mp4]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ps: 有时间笔者会慢慢补充命令的。

    示例代码

    获取音频与视频流

    #include 
    
    extern "C" {
    
    #include 
    #include 
    
    }
    
    
    int main()
    {
    	// 打开封装文件
        AVFormatContext *fmt = nullptr;
        int ret = avformat_open_input(&fmt, "jie.mp4", nullptr, nullptr);
        if (ret < 0) {
            std::cerr << "ret: " << ret << std::endl;
            return 0;
        }
    
        std::cout << "open fmt success" << std::endl;
    	// 获取文件播放秒数
        std::cout << "duration: " << fmt->duration / AV_TIME_BASE << std::endl;
    	// 获取封装文件一共有多个流
        std::cout << "streams: " << fmt->nb_streams << std::endl;
    	
    	// 查找到视频流索引
        int videoIndex = av_find_best_stream(fmt, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
        if (videoIndex < 0) {
            std::cerr << "video index failed" << std::endl;
            goto quit;
        }
        std::cout << "video index: " << videoIndex << std::endl;
    	// 查找到音频流索引
        int audioIndex = av_find_best_stream(fmt, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
        if (audioIndex < 0) {
            std::cerr << "audio index failed" << std::endl;
            goto quit;
        }
        std::cout << "audio index: " << videoIndex << std::endl;
    	
    	// 获取音频与视频流,打印它们的解码器id
        AVStream *videoStream = fmt->streams[videoIndex];
        std::cout << "video codec id: " << videoStream->codecpar->codec_id << std::endl;
        AVStream *audionStream = fmt->streams[audioIndex];
        std::cout << "audio codec id: " << audionStream->codecpar->codec_id << std::endl;
    
    quit:
    	// 关闭封装
        avformat_close_input(&fmt);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    ps:未完,待续...

  • 相关阅读:
    超级实用网站+公众号合集
    喜讯!云起无垠入选《2023年中国AIGC创新企业榜》
    为RabbitMQ配置SSL
    Unity记录5.5-地图-随机生成连续洞穴块
    关于Vue的一些高频面试题总结
    Spring Bean 的生命周期了解么?
    SwiftUI 动态岛开发教程之 05 Dynamic Island 和 Live Activity 无需太多代码即可为用户提供大量信息
    操作系统——复习笔记
    [python]十九、进程、线程和协程
    电脑怎么设置定时关机?
  • 原文地址:https://blog.csdn.net/BCMY0110/article/details/133485723