> 技术文档 > Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译


目录

      • 一、交叉编译环境准备
      • 二、下载FFmpeg源码和编译测试
        • 关键参数说明
        • C 语言测试代码
        • 交叉编译测试程序
        • 在 ARM 设备上运行
      • 三、配置编译选项(支持RTSP)
      • 四、编译并安装
      • 五、部署与运行
      • 常见问题解决

下载地址

https://ffmpeg.org/download.html

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

一、交叉编译环境准备

  1. 配置交叉编译工具链

    # Ubuntu/Debian系统 export PATH=$PATH:/home/user/Desktop/xxx/output/host/bin# 验证安装arm-linux-gcc --version

    Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

  2. 安装依赖库 – 可选

    sudo apt-get install make automake autoconf libtool pkg-config \\  libssl-dev libx264-dev libx265-dev

二、下载FFmpeg源码和编译测试

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译
版本是 7.1.1

cd ffmpeg-7.1.1

FFmpeg 中所有协议(protocol)和解封装器(demuxer)的状态可通过 ./configure --list-protocols./configure --list-demuxers 查看完整列表。
Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

# 配置编译参数(静态库版)./configure \\ --arch=aarch64 \\ --target-os=linux \\ --cross-prefix=aarch64-linux-gnu- \\ --enable-cross-compile \\ --prefix=/tmp/ffmpeg-arm64 \\ --disable-shared \\ --enable-static \\ --disable-doc \\ --disable-ffplay \\ --disable-ffprobe \\ --disable-avdevice \\ --disable-swresample \\ --disable-postproc \\ --disable-avfilter# 3. 编译并安装make -j$(nproc)make install
 ./configure --arch=arm --target-os=linux --cross-prefix=arm-linux- --enable-cross-compile --prefix=/home/user/Desktop/ffmpeg/ffmpeg-7.1.1/install --disable-shared --enable-static --disable-doc --disable-ffplay --disable-ffprobe --disable-avdevice --disable-swresample --disable-postproc --disable-avfilter

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

make 

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

make install

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

关键参数说明
  • --cross-prefix:指定交叉编译工具前缀
  • --arch:目标平台架构(ARMv7 用 armv7l
  • --disable-shared --enable-static:生成静态库
  • --prefix:指定安装目录

C 语言测试代码

保存为 test.c

#include #include #include int main() { av_log_set_level(AV_LOG_INFO); // 初始化 FFmpeg avformat_network_init(); // 获取 FFmpeg 版本 printf(\"=== FFmpeg 库测试 ===\\n\"); printf(\"AVFormat version: %d\\n\", avformat_version()); // 测试编解码器 AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264); if (!codec) { printf(\"错误: H264 解码器未找到!\\n\"); return 1; } printf(\"找到解码器: %s\\n\", codec->name); // 测试协议支持 printf(\"支持的协议:\\n\"); void *opaque = NULL; const char *protocol = avio_enum_protocols(&opaque, 0); while (protocol) { printf(\" - %s\\n\", protocol); protocol = avio_enum_protocols(&opaque, 0); } avformat_network_deinit(); return 0;}

交叉编译测试程序
# 使用交叉编译器编译aarch64-linux-gnu-gcc test_ffmpeg.c \\ -I/tmp/ffmpeg-arm64/include \\ -L/tmp/ffmpeg-arm64/lib \\ -lavformat -lavcodec -lavutil -lswscale -lz -lm -lpthread \\ -o test_ffmpeg_arm# 查看文件类型(确认ARM架构)file test_ffmpeg# 应显示:ELF 64-bit LSB executable, ARM aarch64, ...
arm-linux-gcc test.c -I/home/user/Desktop/ffmpeg/ffmpeg-7.1.1/install/include -L/home/user/Desktop/ffmpeg/ffmpeg-7.1.1/install/lib -lavformat -lavcodec -lavutil -lswscale -lz -lm -lpthread -o test_ffmpeg

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译


在 ARM 设备上运行
  1. 将生成的 test_ffmpeg 复制到 ARM 开发板
  2. 执行测试:
./test_ffmpeg

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

三、配置编译选项(支持RTSP)

x264 的交叉编译可参考文章 开源的 H.264/AVC 视频编码器库-x264 的交叉编译 和 程序测试

https://mp.weixin.qq.com/s/3HC-QvBuRJknte-Vu_OJig

将生成的库文件拷贝到 Arm 开发板。
Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

设置 x264 路径

 # 设置交叉编译专用路径export ARM_PKG_CONFIG=\"/opt/x264_arm/lib/pkgconfig:/opt/fdkaac_arm/lib/pkgconfig\"export PKG_CONFIG_PATH=$ARM_PKG_CONFIG:$PKG_CONFIG_PATH# 设置 pkg-config 使用交叉编译工具链export PKG_CONFIG=\"pkg-config --define-variable=prefix=/opt/x264_arm\"
 export PKG_CONFIG_PATH=/home/user/Desktop/x264/x264/output/lib/pkgconfig:$PKG_CONFIG_PATH export PKG_CONFIG=\"pkg-config --define-variable=prefix=/home/user/Desktop/x264/x264/output\"
# 检查 x264 是否可被检测pkg-config --exists --print-errors x264# 获取 x264 信息pkg-config --modversion x264pkg-config --libs x264pkg-config --cflags x264

如果不设置,则报错,报错内容如下:x264 not found using pkg-config

ffmpeg 配置

# 配置交叉编译环境变量# 配置FFmpeg编译选项(重点支持RTSP/H.264)./configure \\ --prefix=$(pwd)/output \\ --arch=aarch64 \\ --target-os=linux \\ --cross-prefix=aarch64-linux-gnu- \\ --enable-cross-compile \\ --disable-shared \\ --enable-static \\ --disable-doc \\ --disable-ffplay \\ --disable-ffprobe \\ --enable-gpl \\ --enable-nonfree \\ --enable-libx264 \\ --enable-openssl \\ --enable-protocol=tcp \\ --enable-protocol=udp \\ --enable-demuxer=rtsp \\ --enable-decoder=h264 \\ --enable-network \\ --extra-cflags=-I/home/x264/include \\--extra-ldflags=-L/home/x264/lib \\--extra-libs=-ldl \\--pkg-config=\"pkg-config --static\" \\--extra-ldflags=\"-latomic\"

关键参数说明

  • --enable-openssl:支持RTSP over HTTPS
  • --enable-protocol=tcp/udp:启用传输协议
  • --enable-demuxer=rtsp:启用RTSP解复用器
  • --enable-decoder=h264:支持H.264解码(最常见的RTSP视频编码)

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

./configure --prefix=$(pwd)/output --arch=arm --target-os=linux --cross-prefix=arm-linux- --enable-cross-compile --disable-shared --enable-static --disable-doc --disable-ffplay --disable-ffprobe --enable-gpl --enable-nonfree --enable-openssl --enable-protocol=rtsp --enable-protocol=tcp --enable-protocol=udp --enable-demuxer=rtsp --enable-decoder=h264 --enable-network --enable-libx264 --extra-cflags=-I/home/user/Desktop/x264/x264/output/include --extra-ldflags=-L/home/user/Desktop/x264/x264/output/lib --extra-libs=-ldl --pkg-config=\"pkg-config --static\" --extra-ldflags=\"-latomic\"

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

四、编译并安装

# 执行配置脚本bash arm64_configure.sh# 编译(根据CPU核心数调整-j参数)make -j$(nproc)# 安装到指定目录make install
make

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

# 安装到指定目录make install

Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

五、部署与运行

  1. 将 FFmpeg库文件(output/lib目录下的.a文件)和 可执行文件(output/bin目录下的文件)分别复制到Arm 开发板的 /lib 和 /bin 文件下

  2. 在ARM设备上运行测试程序:

    # 通过命令行参数指定RTSP URL./test_rtsp rtsp://admin:password@192.168.1.100:554/stream1
    # 查看版本信息ffmpeg -version# 查看编译配置ffmpeg -buildconf# 检查支持的协议ffmpeg -protocols | grep tcpffmpeg -demuxers| grep rtsp# 检查编解码器支持ffmpeg -codecs | grep h264

    Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

    # 查看媒体文件信息ffmpeg -i input.mp4# 详细技术参数分析ffprobe -show_streams -show_format input.mp4# 导出帧信息(JSON格式)ffprobe -print_format json -show_frames input.mp4
    # 基本拉流测试(TCP模式)ffmpeg -rtsp_transport tcp -i rtsp://admin:password@192.168.1.100:554/stream1 -f null -# 保存RTSP流到本地文件ffmpeg -rtsp_transport tcp -i rtsp://... -c copy -map 0 output.mp4# 限制拉流时长(10秒)ffmpeg -rtsp_transport tcp -t 10 -i rtsp://... output.mp4ffmpeg -rtsp_transport tcp -t 5 -i rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream 1.wmvffmpeg -rtsp_transport tcp -t 5 -i rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream 1.flv

    Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译
    Arm Linux交叉编译FFmpeg 开源库_ffmpeg交叉编译

    # 本地文件推送到RTSP服务器ffmpeg -re -i input.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream# 实时屏幕捕获推流(X11)ffmpeg -f x11grab -s 1280x720 -i :0.0 -vcodec libx264 -f rtsp rtsp://...
    # H.264转码(CPU编码)ffmpeg -i input.mp4 -c:v libx264 -preset fast -crf 23 output.mp4# 硬件加速转码(NVIDIA)ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.mp4# 多路转码压力测试ffmpeg -i input.mp4 \\ -map 0:v -c:v libx264 -s 640x360 -b:v 800k output_360p.mp4 \\ -map 0:v -c:v libx264 -s 1280x720 -b:v 1500k output_720p.mp4
    # PSNR质量分析ffmpeg -i encoded.mp4 -i original.mp4 -lavfi psnr -f null -# SSIM质量分析ffmpeg -i encoded.mp4 -i original.mp4 -lavfi ssim -f null -# VMAF视频质量评估(需要安装libvmaf)ffmpeg -i encoded.mp4 -i original.mp4 -lavfi libvmaf -f null -
    # 解码性能测试ffmpeg -benchmark -i input.mp4 -f null -# 编码性能测试ffmpeg -benchmark -i input.mp4 -c:v libx264 -preset ultrafast output.mp4# 多线程效率分析ffmpeg -threads 4 -i input.mp4 -c:v libx264 output.mp4
    # 24小时连续拉流测试ffmpeg -rtsp_transport tcp -i rtsp://... -c copy -f segment \\ -strftime 1 -segment_time 3600 \"output_%Y-%m-%d_%H-%M-%S.mp4\"# 断线重连测试ffmpeg -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 \\ -i rtsp://... output.mp4
    # 视频缩放+水印ffmpeg -i input.mp4 -vf \"scale=640:-1, drawtext=text=\'TEST\':x=10:y=10\" output.mp4# 视频稳定处理ffmpeg -i shaky.mp4 -vf vidstabdetect=shakiness=10:accuracy=15 transform.trfffmpeg -i shaky.mp4 -vf vidstabtransform=smoothing=30:input=transform.trf stable.mp4
    # RTSP超低延迟配置(200ms)ffmpeg -rtsp_transport tcp \\ -fflags nobuffer -flags low_delay \\ -i rtsp://... -c copy -f mpegts udp://127.0.0.1:1234

RTSP 拉流并推流的命令

如果需要从 RTSP 源拉流,然后直接推送到另一个 RTSP 服务器或其他流媒体服务器(如 RTMP),可以使用以下 FFmpeg 命令:

ffmpeg -rtsp_transport tcp -i \"rtsp://源地址:端口/路径\" \\-c:v copy -c:a copy \\-f rtsp \"rtsp://目标地址:端口/路径\"
ffmpeg -rtsp_transport tcp -i \"rtsp://源地址:端口/路径\" -c:v copy -c:a copy -f rtsp \"rtsp://目标地址:端口/路径\"

或者推送到 RTMP 服务器(如 YouTube、Twitch 或自建服务器):

ffmpeg -rtsp_transport tcp -i \"rtsp://源地址:端口/路径\" \\-c:v copy -c:a copy \\-f flv \"rtmp://目标地址:端口/应用名/流名\"

参数说明:

  • -rtsp_transport tcp:使用 TCP 协议传输 RTSP 流,适合网络不稳定的环境
  • -i \"rtsp://源地址...\":输入 RTSP 流地址
  • -c:v copy -c:a copy:直接复制音视频流,不进行重新编码(节省 CPU 资源)
  • -f rtsp/rtmp:输出格式,根据目标服务器选择
  • 输出地址:目标 RTSP 或 RTMP 服务器地址

从 RTSP 拉流并推送到另一个 RTSP 服务器:

ffmpeg -rtsp_transport tcp -i \"rtsp://192.168.1.100:554/stream1\" \\-c:v copy -c:a copy \\-f rtsp \"rtsp://192.168.1.200:554/output\"

推送到 RTMP 服务器(如 YouTube 直播):

ffmpeg -rtsp_transport tcp -i \"rtsp://192.168.1.100:554/stream1\" \\-c:v copy -c:a copy \\-f flv \"rtmp://a.rtmp.youtube.com/live2/流密钥\"

实时转码(如需调整分辨率/比特率)

ffmpeg -rtsp_transport tcp -i \"rtsp://源地址\" \\-c:v libx264 -preset veryfast -tune zerolatency -b:v 2000k \\-c:a aac -b:a 128k \\-f flv \"rtmp://目标地址\"

这个命令会重新编码视频为 H.264,音频为 AAC,适合需要调整质量或兼容不同服务器的场景。

常见问题解决

  1. RTSP连接失败

    • 检查RTSP URL格式是否正确
    • 尝试修改rtsp_transportudphttp
    • 确认设备网络可达,RTSP服务器端口(默认554)开放
  2. H.264解码失败

    • 确保编译时启用了H.264解码器(--enable-decoder=h264
    • 检查RTSP流实际编码格式(可使用VLC等工具确认)
  3. 性能优化

    • 启用硬件加速(如ARM Mali GPU):添加--enable-omx --enable-omx-rpi
    • 减小缓冲区:调整buffer_sizestimeout参数
    • 禁用不必要的协议:如--disable-protocol=file