Logo
Security & Network Streams

Compile FFmpeg 7 with OpenSSL

To process HTTPS, RTMPS, or TLS streams, FFmpeg must be linked against OpenSSL. Here is the definitive cross-platform guide to compiling them from source.

Required Tools & Source

Ensure you have the latest stable releases

License Warning: Linking OpenSSL requires `--enable-nonfree` in FFmpeg due to GPL incompatibilities in older OpenSSL versions, though OpenSSL 3 uses Apache 2.0.

Linux (Ubuntu/Debian) 编译步骤

1. 安装依赖
sudo apt updatesudo apt install build-essential pkg-config nasm yasm curl
2. 编译并安装 OpenSSL
# 解压并进入源码目录tar -zxf openssl-3.*.tar.gz && cd openssl-3.*/# 配置安装路径到 /usr/local/openssl./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlibmake -j$(nproc)sudo make install
3. 编译 FFmpeg 7
tar -xvf ffmpeg-7.*.tar.bz2 && cd ffmpeg-7.*/# 导出 PKG_CONFIG 环境变量,让 FFmpeg 找到刚才编译的 OpenSSLexport PKG_CONFIG_PATH=/usr/local/openssl/lib64/pkgconfig:$PKG_CONFIG_PATH./configure \--prefix=/usr/local/ffmpeg \--enable-shared \--enable-openssl \--enable-nonfree \--extra-cflags="-I/usr/local/openssl/include" \--extra-ldflags="-L/usr/local/openssl/lib64"make -j$(nproc)sudo make install

macOS (Apple Silicon / Intel) 编译步骤

1. 使用 Homebrew 准备环境
# macOS 下自带的 OpenSSL 被替换成了 LibreSSL,所以推荐用 brew 装个标准的,或者从源码编译brew install pkg-config nasm openssl@3
2. 编译 FFmpeg 7
# 获取 Homebrew 安装的 OpenSSL 路径 (Apple Silicon 默认在 /opt/homebrew)export OPENSSL_DIR=$(brew --prefix openssl@3)export PKG_CONFIG_PATH=$OPENSSL_DIR/lib/pkgconfig./configure \--prefix=/usr/local/ffmpeg \--enable-shared \--enable-openssl \--enable-nonfree \--extra-cflags="-I$OPENSSL_DIR/include" \--extra-ldflags="-L$OPENSSL_DIR/lib"make -j8sudo make install

Windows (MSYS2 + MinGW) 编译步骤

1. 配置 MSYS2 (UCRT64)
# 安装好 MSYS2 后,打开 "MSYS2 UCRT64" 终端执行:pacman -Syupacman -S mingw-w64-ucrt-x86_64-toolchain base-devel nasm pkg-config
2. 编译并安装 OpenSSL
# 在 MSYS2 中解压 openssl 源码并进入./Configure mingw64 --prefix=/ucrt64 sharedmake -j16make install
3. 编译 FFmpeg 7
# 因为 OpenSSL 装在了 MSYS2 的系统路径下,所以 pkg-config 会自动找到它./configure \--arch=x86_64 \--target-os=mingw32 \--enable-shared \--enable-openssl \--enable-nonfreemake -j16# 编译完成后,会在当前目录生成 ffmpeg.exe,以及需要的 avcodec-61.dll 等动态库

Verification & Usage

Testing your newly compiled FFmpeg build

命令行验证
./ffmpeg -protocols | grep https
Input:
...
https
Output:
...
https

如果能查看到 `https` 协议,说明 OpenSSL 已经成功链接进入 FFmpeg。

C++ API 调用验证
extern "C" {
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
}

int main() {
    avformat_network_init(); // 必须调用此函数初始化网络组件

    AVFormatContext *fmt_ctx = nullptr;
    const char *url = "https://example.com/stream.m3u8";

    // 如果没有编译 OpenSSL,这里将返回 Protocol not found 错误
    int ret = avformat_open_input(&fmt_ctx, url, nullptr, nullptr);
    if (ret < 0) {
        printf("Failed to open HTTPS stream: %d\n", ret);
        return -1;
    }

    printf("HTTPS Stream opened successfully!\n");
    avformat_close_input(&fmt_ctx);
    avformat_network_deinit();
    return 0;
}