Using libav* (FFmpeg)

本文详细介绍了FFmpeg项目中各个组成部分的功能及应用场景,包括libavutil、libavcodec、libavformat等核心库,并提供了使用这些库进行多媒体处理的指导。


First some disambiguiation.  there is a project called "libav" which is basically a fork of FFmpeg.  There is also a library system that underlies FFmpeg itself, also called libav.  This page is about the library libav, which is a part of FFmpeg.


FFmpeg itself is composed of several libraries that can be used individually, and outside of FFmpeg, for instance in integrating parts of FFmpeg into your own program.  These are:
* libavutil contains various routines used to simplify programming, including random number generators, data structures, mathematics routines, core multimedia utilities, and much more. 
* libavcodec provides a decoding and encoding API, and all the supported codecs.
* libavformat provides a demuxing and muxing API, and all the supported muxers and de-muxers.
* libavdevice provides an interface for grabbing from input devices (e.g. webcames or line-in audio) and rendering to output devices, and all the supported input and output devices
* libswscale provides a scaling and (raw pixel) format conversions API, with high speed/assembly optimized versions of several scaling routines.
* libavfilter provides an audio and video filtering API, and all the supported filters.
* libpostproc provides video postprocessing routines
* libswresample provides an audio resampling, rematrixing and sample format conversion API, and many high-quality optimized routines.


These can be useful for instance if you don't have access to a command line to run ffmpeg.exe (for example, Android apps), or if you want to use just a small "part" of FFmpeg inside your own program, or if you want access to raw video frames in your program, etc.  Also note that if you just need access to raw video frames, you could also write an audio or video [FilteringGuide filter] and compile it along with FFmpeg and distribute that.  Another way to access raw video frames is to have ffmpeg output to "stdout", like {{{ffmpeg -i ... -}}}. Also note that if you just need to convert/transcode videos within your own application, you could make a system call out to the FFmpeg executable to do the heavy lifting for you.  You can parse the output for stdout for status information, or use the "-progress" option to make the output even more parseable.  


= Getting started =


Here is the [https://www.ffmpeg.org/documentation.html official documentation] for using these libraries (the "Libraries Documentation" section).


Also check [https://github.com/FFmpeg/FFmpeg/tree/master/doc/examples doc/examples], the doxygen documentation is fairly complete and should work as reference (example: the [http://ffmpeg.org/doxygen/trunk/files.html example codes as doxygen]).


In general, you must have the appropriate library compiled/available on your machine (for instance, if using packages, something like libswscale-dev must be installed, or configure, build, and install FFmpeg yourself using the --enable-shared configure option), then you include the appropriate header file in your C code, then link against that library's linker file, like "gcc input.c -lswscale" or the like during the linker phase.


= Tutorials =


The web has a few tutorials, some of which are out of date.  The doc/examples files usually use the latest ABI, however, should be more trustworthy.


[http://dranger.com/ffmpeg/ An FFmpeg and SDL Tutorial by Stephen Dranger], explains how to write a video player based on FFmpeg (see also [https://github.com/phamquy/FFmpeg-tutorial-samples here]).


= Hints =


== libavformat ==


Check its [http://ffmpeg.org/doxygen/trunk/group__libavf.html Doxygen documentation].  See most of the tutorials, as well.


== libavcodec ==


Check its [http://ffmpeg.org/doxygen/trunk/group__libavc.html Doxygen documentation].  See most of the tutorials, as well.


Determining the right values to pass to AVCodecContext:


One user [https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-June/007410.html shared] this advice for determining all the correct values:


 [An] approach to figuring this out is:


 1. come up with the ffmpeg app command line which does what you want


 2. use the gdb debugger to execute the ffmpeg_g app, put a breakpoint on avcodec_encode_audio2() (or whichever method you need), and see what values the ffmpeg app uses for AVPacket and for the (audio or otherwise) related fields in AVCodecContext.


== libswresample ==


[https://github.com/FFmpeg/FFmpeg/blob/master/doc/swresample.txt This file] is also given as documentation.


== libswscale ==


[https://github.com/FFmpeg/FFmpeg/blob/master/doc/swscale.txt This file] is also given as documentation.


== libavdevice ==


Check its [http://ffmpeg.org/doxygen/trunk/group__lavd.html doxygen documentation].


here is an example: http://ffmpeg.zeranoe.com/forum/viewtopic.php?f=15&t=274


== libavfilter ==


See http://wiki.multimedia.cx/index.php?title=Libavfilter and also the examples, and already existing filters. we have more doxy, some examples and a libavfilter-design document in doc.  Also see the ffmpeg documentation for the various filters.




= Contact =


If you have problems, one place to get help is to ask the [https://lists.ffmpeg.org/mailman/listinfo/libav-user/ libav-user] mailing list, its description: "This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter."  IRC might work also.


== see also ==


http://wiki.multimedia.cx/index.php?title=Category:FFmpeg_Tutorials










<think> 我们遇到了两个问题: 1. 运行ffmpeg时出现共享库错误:libpostproc.so.56: cannot open shared object file 2. 在配置(configure)某个软件时出现错误:Missing libav or FFmpeg. Pass --disable-avcodec to ignore this error. 从问题描述来看,第二个问题(configure error)可能是因为FFmpeg开发包(头文件库)没有正确安装或者版本不匹配。而第一个问题(共享库缺失)则是运行时的问题。 我们可以分步骤解决这两个问题,首先解决FFmpeg的安装配置问题,确保FFmpeg正确安装并且版本兼容。 步骤概览: 1. 确保FFmpeg完整安装(包括开发包),解决共享库问题。 2. 解决configure错误,确保配置时能够找到FFmpeg。 详细步骤: ### 一、解决FFmpeg共享库问题(libpostproc.so.56缺失) 根据之前的解决方案,我们可以尝试: #### 1. 安装缺失的库(推荐) 对于Ubuntu/Debian系统: ```bash sudo apt update sudo apt install libpostproc-dev libpostproc-extra-56 ``` 如果安装后仍然遇到问题,可能是因为安装的FFmpeg版本较高,库的版本号已经升级(如libpostproc.so.57)。此时可以创建符号链接: #### 2. 创建符号链接(如果库版本不匹配) 查找已安装的libpostproc库: ```bash find /usr -name "libpostproc*" 2>/dev/null ``` 假设找到的是`/usr/lib/x86_64-linux-gnu/libpostproc.so.57`,则创建符号链接: ```bash sudo ln -s /usr/lib/x86_64-linux-gnu/libpostproc.so.57 /usr/lib/x86_64-linux-gnu/libpostproc.so.56 sudo ldconfig ``` #### 3. 添加库路径到环境变量(如果库不在标准路径) 如果库安装在其他路径(如`/usr/local/lib`),则需要将该路径加入动态链接库的搜索路径: ```bash echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/ffmpeg.conf sudo ldconfig ``` 验证: ```bash ldconfig -p | grep libpostproc ``` 应该能看到`libpostproc.so.56`已经链接到实际存在的库文件。 ### 二、解决configure错误:Missing libav or FFmpeg 这个错误通常发生在配置需要FFmpeg库的软件(如mplayer、handbrake等)时,配置脚本找不到FFmpeg的开发文件(头文件库)。 我们需要确保FFmpeg的开发包已经安装,并且配置脚本能够找到它。 #### 1. 安装FFmpeg开发包 对于Ubuntu/Debian系统: ```bash sudo apt install ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libpostproc-dev ``` 对于CentOS/RHEL系统: ```bash sudo yum install ffmpeg ffmpeg-devel ``` #### 2. 设置PKG_CONFIG_PATH 如果FFmpeg安装在非标准路径(比如通过源码安装到`/usr/local`),需要设置`PKG_CONFIG_PATH`环境变量指向FFmpeg的pkgconfig目录。 查找pkgconfig目录: ```bash find /usr -name "libavcodec.pc" 2>/dev/null ``` 假设找到的目录是`/usr/local/lib/pkgconfig`,则: ```bash export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH ``` 将此命令添加到`~/.bashrc`或`/etc/profile`中永久生效。 #### 3. 重新运行configure 确保已经安装了所有依赖的开发包,然后重新运行配置脚本: ```bash ./configure ``` 如果仍然失败,可以尝试明确指定FFmpeg的路径(如果配置脚本支持): ```bash ./configure --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib ``` 其中`/usr/local/include``/usr/local/lib`是FFmpeg开发文件的安装路径(根据实际情况调整)。 #### 4. 如果不需要FFmpeg支持 如果软件允许,可以禁用FFmpeg支持(不推荐,除非确定不需要): ```bash ./configure --disable-avcodec ``` ### 三、验证FFmpeg安装 运行以下命令检查FFmpeg是否安装正确: ```bash ffmpeg -version ``` 输出应包含libpostproc等库,且没有报错。 同时,检查开发文件是否存在: ```bash # 检查头文件 ls /usr/include/libavcodec/avcodec.h # 检查库文件 ls /usr/lib/libavcodec.so ``` ### 注意事项 1. 如果通过源码安装FFmpeg,请确保在编译时启用共享库(`--enable-shared`)[^1][^2]。 2. 如果系统中存在多个FFmpeg版本(如系统自带手动编译),可能会导致冲突。建议统一使用包管理器安装或源码安装。 ### 完整示例(源码安装FFmpeg) 如果包管理器安装的版本太旧,可以考虑源码安装: 1. 下载FFmpeg源码(https://ffmpeg.org/download.html) 2. 编译安装: ```bash tar xf ffmpeg-*.tar.xz cd ffmpeg-* ./configure --enable-shared --prefix=/usr/local make -j$(nproc) sudo make install ``` 3. 配置动态链接器: ```bash echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/ffmpeg.conf sudo ldconfig ``` 4. 设置PKG_CONFIG_PATH: ```bash export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH ``` 然后重新运行需要FFmpeg的configure脚本。 §§相关问题§§ 1. 如何在不同Linux发行版中编译安装FFmpeg? 2. `ldconfig`命令的作用是什么?如何正确配置动态链接库路径? 3. FFmpeg的版本兼容性问题如何解决?[^5] </think> ### 解决 FFmpeg 相关错误的完整指南 #### 问题分析与解决路径 您的两个问题密切相关: 1. **运行时错误**:`ffmpeg: error while loading shared libraries: libpostproc.so.56` - 核心问题:动态链接库缺失或路径配置错误 2. **编译配置错误**:`configure error: Missing libav or FFmpeg` - 核心问题:开发文件(头文件库)未正确安装或路径未识别 以下是系统化的解决方案: --- ### 一、解决共享库错误 (`libpostproc.so.56`) #### 方法1:安装兼容库(推荐) ```bash # Ubuntu/Debian sudo apt update sudo apt install libpostproc-dev libpostproc-extra-56 # CentOS/RHEL sudo yum install ffmpeg-libs ``` #### 方法2:创建符号链接(解决版本冲突) ```bash # 查找实际安装的库版本(例如 libpostproc.so.57) find /usr -name "libpostproc*" 2>/dev/null # 创建符号链接(替换实际路径) sudo ln -s /usr/lib/x86_64-linux-gnu/libpostproc.so.57 /usr/lib/libpostproc.so.56 # 更新链接缓存 sudo ldconfig ``` #### 方法3:配置动态库路径 ```bash # 临时生效 export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH # 永久生效 echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' | sudo tee -a /etc/profile source /etc/profile ``` --- ### 二、解决编译配置错误 (`Missing libav or FFmpeg`) #### 步骤1:安装完整开发包 ```bash # Ubuntu/Debian sudo apt install ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev # CentOS/RHEL sudo yum install ffmpeg-devel ``` #### 步骤2:配置 pkg-config 路径 ```bash # 查找开发文件位置 find /usr -name "libavcodec.pc" 2>/dev/null # 设置环境变量(假设路径为 /usr/local/lib/pkgconfig) export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH ``` #### 步骤3:重新配置项目 ```bash # 明确指定 FFmpeg 路径 ./configure --extra-cflags=-I/usr/local/include --extra-ldflags=-L/usr/local/lib # 或强制启用 avcodec(不推荐) ./configure --enable-avcodec --disable-warnings ``` --- ### 三、版本兼容性解决方案 FFmpeg API 变更频繁(如 `av_find_stream_info()` → `avformat_find_stream_info()`[^1]),需确保一致性: 1. **检查版本匹配**: ```bash ffmpeg -version | grep libpostproc # 运行库版本 pkg-config --modversion libavcodec # 开发库版本 ``` 2. **版本对齐策略**: - 方案A:升级项目代码适配新版 FFmpeg API - 方案B:安装旧版 FFmpeg 匹配项目需求 ```bash # 安装指定版本(Ubuntu 示例) sudo apt install ffmpeg=7:4.4.2-0ubuntu0.22.04.1 ``` --- ### 四、验证修复 ```bash # 验证运行时库 ldconfig -p | grep libpostproc # 应显示 libpostproc.so.56 # 验证开发文件 pkg-config --cflags libavcodec # 应无错误输出 # 测试编译配置 ./configure --enable-avcodec | grep "FFmpeg" # 应显示 "enabled" ``` > **关键提示**: > - 共享库路径:运行时通过 `LD_LIBRARY_PATH` 或 `/etc/ld.so.conf` 控制 > - 开发文件路径:编译时通过 `PKG_CONFIG_PATH` 编译器标志控制 > - 版本管理:使用 `apt-mark hold ffmpeg` 防止自动升级导致兼容性问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值