Clion-mac版引入FFmepg库

作者开发Android播放器应用后,发现实际工作多为音视频维护,尤其面对片源问题时现有工具作用不大,于是准备利用业余时间基于FFmpeg搭建工具工程。本文介绍了安装FFmpeg、新建Clion工程、引入库及用示例代码检查是否成功的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

碎碎念

利用FFmpeg库开发完了一个完整的Android播放器应用之后(后面抽空整理一下发出来),我用起来并不是很爽。因为,我发现光是能开发播放器,并不能很好的解决实际工作场景下的问题。

现在市场上已经有很多播放器了,不管什么平台的,我们很少会从零开发播放器的机会。而现在的音视频领域的开发者,大部分时间都在做什么呢?

答案是维护

我自己在工作中,做的最多的也是维护。说的直白一点,就是一线发现bug了,把日志丢给我们,我们定位后,能修复的修复,不能的给出解释,让客户规避。

常见的问题有:音视频同步、卡顿、噪点、黑屏、无声等。问题发生的原因,有时是解码能力、解码速度原因、有时可能网络原因,还有可能是片源问题。每当怀疑是片源问题时,总是感觉束手无策,没法直接证明,只能用其它手段曲线救国。

网上虽然有一些片源分析工具,但因为片源的问题往往千奇八怪,作用不大。

所以,准备用业务时间,基于FFmpeg自己撸一个工具类型的工程,专治片源的各种不服。这是开始的第一篇。

安装FFmpeg

直接按照官网的操作执行就成,哪里有你想要的操作系统版本和所有的FFmpeg历史版本。

传送门

mac系统的小伙伴直接用这个命令也行:

brew install ffmpeg

安装完成后,可以通过如下命令查看ffmpeg目录:

brew info ffmpeg

输出如下:

[Superli-2:github superli$ brew info ffmpeg
ffmpeg: stable 4.1.1 (bottled), HEAD
Play, record, convert, and stream audio and video
https://ffmpeg.org/
/usr/local/Cellar/ffmpeg/3.4.2 (250 files, 50.9MB) *
  Poured from bottle on 2018-03-18 at 09:54:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ffmpeg.rb
==> Dependencies
Build: nasm ✘, pkg-config ✘, texi2html ✘
Required: aom ✘, fontconfig ✘, freetype ✘, frei0r ✘, gnutls ✘, lame ✔, libass ✘, libbluray ✘, libsoxr ✘, libvorbis ✘, libvpx ✘, opencore-amr ✘, openjpeg ✘, opus ✘, rtmpdump ✘, rubberband ✘, sdl2 ✘, snappy ✘, speex ✘, tesseract ✘, theora ✘, x264 ✘, x265 ✘, xvid ✔, xz ✔
==> Options
--HEAD
	Install HEAD version

在输出中,有一个待会儿需要用到的信息——FFmpeg的安装路径:/usr/local/Cellar/ffmpeg/3.4.2

来看看里边都有些啥:

[Superli-2:3.4.2 superli$ ls -al
total 168
drwxr-xr-x  11 superli  admin    352  3 18  2018 .
drwxr-xr-x   3 superli  admin     96  3 18  2018 ..
drwxr-xr-x   3 superli  admin     96  2 12  2018 .brew
-rw-r--r--   1 superli  admin  65727  2 12  2018 Changelog
-rw-r--r--   1 superli  admin   1897  3 18  2018 INSTALL_RECEIPT.json
-rw-r--r--   1 superli  admin   4368  2 12  2018 LICENSE.md
-rw-r--r--   1 superli  admin   1893  2 12  2018 README.md
drwxr-xr-x   5 superli  admin    160  2 12  2018 bin
drwxr-xr-x  11 superli  admin    352  2 12  2018 include
drwxr-xr-x  39 superli  admin   1248  2 12  2018 lib
drwxr-xr-x   5 superli  admin    160  2 12  2018 share

其中的lib和include文件夹,待会儿我们需要用到。

新建一个Clion工程

虽然FFmpeg是用C语言编写的,但我还是选择了C++可执行的工程。谁让Java是我的入门语言呢。

引入库

引入和链接库的动作,当然是在Cmake文件中啦。Clion的Cmake文件是CmakeLists.txt。比如,我这个工程的CmakeLists.txt文件长这样:

cmake_minimum_required(VERSION 3.10)
project(PlayFFmpeg) # 指定的项目名称

set(FFMPEG_DIR /usr/local/Cellar/ffmpeg/3.4.2) # FFmpeg的安装目录,可以通过命令"brew info ffmpeg"获取
include_directories(${FFMPEG_DIR}/include/) # 头文件搜索路径
link_directories(${FFMPEG_DIR}/lib/) # 动态链接库或静态链接库的搜索路径
set(CMAKE_CXX_STANDARD 11)

add_executable(PlayFFmpeg main.cpp)

# 添加链接库
target_link_libraries(
        PlayFFmpeg
        avcodec
        avdevice
        avfilter
        avformat
)

额,不好意思,加注释的部分,除了project(PlayFFmpeg意外,都是我自己添加的,这是一个引入了FFMepg库,可以正常使用的Cmake文件。注释部分的内容说明了当前代码行的作用,就不另作说明了。

另外需要注意的是,别把add_executable()target_link_libraries()的顺序搞混了,如果你的Clion告诉你:

CMake Error at CMakeLists.txt:12 (target_link_libraries):
  Cannot specify link libraries for target "PlayFFmpeg" which is not built by
  this project.

记得检查一下add_executable()target_link_libraries()的顺序。

另外,代码中target_link_libraries()链接的库并不多,你可以按需使用:你需要的所有FFmpeg的库,都在FFmpeg安装路径的lib目录下。

不管是windows还是mac系统,使用时,记得掐头去尾,例如:libavcodec.a 需要写成avcodec

示例代码检查是否成功

#include <iostream>
extern "C" { 
#include <libavformat/avformat.h>
}
using namespace std;
int main() {
    cout << "Hello, FFmpeg!" << endl;
    cout << avformat_configuration() << endl; // 打印libavformat构建时配置信息。
    return 0;
}

FFmpeg是纯C语言开发的,使用它的库时,需要申明:extern "C"

运行一下:

Hello, FFmpeg!
--prefix=/usr/local/Cellar/ffmpeg/3.4.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --disable-jack --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma

没什么问题,over!

后面应该会基于这个工程,做一个片源分析的工作。争取最后整理出一个可以解决部分工作中问题的工具出来。

/root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/ninja/linux/x64/ninja -DVP_SDK_PLATFORM=VP_SDK_PLATFORM_LINUX -G Ninja -S /home/dong/vp_cloud -B /home/dong/vp_cloud -- Current build platform is [Linux] use host -- vp_context_1.0.0.12 Module Config: -- VP_SDK_OS_TYPE:VP_SDK_OS_VEEPAI -- VP_SDK_PLATFORM:VP_SDK_PLATFORM_LINUX -- VP_POWER_TYPE:VP_POWER_TYPE_LOW -- VP_NETWORK_MODE:VP_NETWORK_MODE_MOBILE -- VP_SOC_BRAND:VP_SOC_BRAND_JZ -- VP_SOC_MODEL:VP_SOC_MODEL_JZ_T41ZM -- VP_NIC_BRAND:VP_NIC_BRAND_HZ -- VP_NIC_MODEL:VP_NIC_MODEL_HZ_780E -- VP_DEVICE_TYPE:VP_DEVICE_TYPE_BW6 -- VP_MOBILE_DEVICE_TYPE:VP_MOBILE_DEVICE_TYPE_QG5 -- Current build Veepai OS -- Current build nic mode wifi:OFF mobile:ON wire:OFF -- vp_printf_1.0.0.4 Module Config: -- VP_PRINTF_ENABLE:0x01 -- VP_PRINTF_LEVEL:VP_PRINTF_LEVEL_ALL -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libavformat' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libavformat' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:16 (add_pkg_library) -- vp_platform not found libavformat -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libavcodec' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libavcodec' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:17 (add_pkg_library) -- vp_platform not found libavcodec -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libswresample' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libswresample' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:18 (add_pkg_library) -- vp_platform not found libswresample -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libavfilter' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libavfilter' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:19 (add_pkg_library) -- vp_platform not found libavfilter -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libpostproc' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libpostproc' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:20 (add_pkg_library) -- vp_platform not found libpostproc -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libswscale' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libswscale' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:21 (add_pkg_library) -- vp_platform not found libswscale -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) -- Checking for one of the modules 'libavutil' CMake Error at /root/.cache/JetBrains/RemoteDev/dist/4301d48e0f61a_CLion-252.23309.24/bin/cmake/linux/x64/share/cmake-4.0/Modules/FindPkgConfig.cmake:934 (message): None of the required 'libavutil' found Call Stack (most recent call first): .dependencies/vp_cmake_1.0.0.8/vp_cmake/src/CMakeLists.txt:256 (pkg_search_module) .dependencies/vp_platform_1.0.0.19/vp_platform/CMakeLists.txt:22 (add_pkg_library) -- vp_platform not found libavutil platform_dir:LINUX -- vp_crypto_1.1.0.0 Module Config: -- VP_SSL_MBEDTLS:0x01 -- VP_SSL_WOLFSSL:0x00 -- VP_SSL_LIBRARY:0x01 -- Configuring incomplete, errors occurred! [无法重新加载]
最新发布
06-22
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值