Ubuntu24 源码编译安装nginx及rtmp、http-flv,并测试部分功能
ubuntu24 编译nginx及rtmp、flv模块
root@business01:~/work/nginx-1.25.4# uname -a
Linux business01 5.15.0-141-generic #151-Ubuntu SMP Sun May 18 21:35:19 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev wget unzip git
# 下载 Nginx 最新源码(以 1.25.4 为例)
wget https://nginx.org/download/nginx-1.25.4.tar.gz
tar -zxvf nginx-1.25.4.tar.gz
# 下载 nginx-http-flv-module(支持 RTMP 和 HTTP-FLV)
git clone https://github.com/winshining/nginx-http-flv-module.git
# 下载 nginx-rtmp-module(RTMP 直播模块)
git clone https://github.com/arut/nginx-rtmp-module.git
# 首先安装nginx和rtmp模块
cd nginx-1.25.4
# 配置编译选项(启用 RTMP)
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--add-module=../nginx-rtmp-module
make -j$(nproc) && sudo make install
# 修改/usr/local/nginx/conf/nginx.conf,添加rtmp模块
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on; # 启用直播
meta off; # 禁用元数据(减少延迟)
}
}
}
# 验证模块安装与配置正确
/usr/local/nginx/sbin/nginx -T
# 然后安装http-flv模块
make clean
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--add-module=../nginx-http-flv-module
make -j$(nproc) && sudo make install
# 修改/usr/local/nginx/conf/nginx.conf, 在http下层添加flv模块
# HTTP-FLV 直播流
location /live {
flv_live on;
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
# 验证模块安装与配置正确
/usr/local/nginx/sbin/nginx -T
# 验证flv、rtmp模块都无误后,启动nginx
/usr/local/nginx/sbin/nginx
windows使用ffmpeg推流给nginx-rtmp服务器
PS C:\Users\LiuYang> ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version 7.0.2-essentials_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 13.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 59. 8.100 / 59. 8.100
libavcodec 61. 3.100 / 61. 3.100
libavformat 61. 1.100 / 61. 1.100
libavdevice 61. 1.100 / 61. 1.100
libavfilter 10. 1.100 / 10. 1.100
libswscale 8. 1.100 / 8. 1.100
libswresample 5. 1.100 / 5. 1.100
libpostproc 58. 1.100 / 58. 1.100
[dshow @ 000001cc9476f240] "USB Video Device" (video)
[dshow @ 000001cc9476f240] Alternative name "@device_pnp_\\?\usb#vid_1bcf&pid_28c4&mi_00#7&1da11eb4&0&0000#{xxxxxxxxxx}\global"
[dshow @ 000001cc9476f240] "OBS Virtual Camera" (none)
[dshow @ 000001cc9476f240] Alternative name "@device_sw_{860xxxxxxxxxxxxxxxxx11CE86}\{A3FCE0xxxxxxxxxxxxxxxxxxEC20B}"
[dshow @ 000001cc9476f240] "麦克风 (SPCA2650 AV Camera)" (audio)
[dshow @ 000001cc9476f240] Alternative name "@device_cm_{33D9A762-90C8xxxxxxxxxxxxxxxxxx1CE86}\wave_{197Bxxxxxxxxxxxxxx2A633}"
[dshow @ 000001cc9476f240] "麦克风 (Realtek(R) Audio)" (audio)
[dshow @ 000001cc9476f240] Alternative name "@device_cm_{33D9A76xxxxxxxxxxxxxCE86}\wave_{EF9D3928-Cxxxxxxxxxxxxxxx1A961E7}"
[in#0 @ 000001cc9476ee40] Error opening input: Immediate exit requested
Error opening input file dummy.
PS C:\Users\LiuYang> ffmpeg -f dshow -i video="USB Video Device" -c:v libx264 -preset ultrafast -tune zerolatency -f flv rtmp://192.168.1.103:1935/live/msi5600
ffmpeg version 7.0.2-essentials_build-www.gyan.dev Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 13.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 59. 8.100 / 59. 8.100
libavcodec 61. 3.100 / 61. 3.100
libavformat 61. 1.100 / 61. 1.100
libavdevice 61. 1.100 / 61. 1.100
libavfilter 10. 1.100 / 10. 1.100
libswscale 8. 1.100 / 8. 1.100
libswresample 5. 1.100 / 5. 1.100
libpostproc 58. 1.100 / 58. 1.100
Input #0, dshow, from 'video=USB Video Device':
Duration: N/A, start: 94248.028347, bitrate: N/A
Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/bt709/unknown), 1920x1080, 60 fps, 60 tbr, 10000k tbn
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 00000155786d0a80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 00000155786d0a80] profile High 4:2:2, level 4.2, 4:2:2, 8-bit
[libx264 @ 00000155786d0a80] 264 - core 164 r3191 4613ac3 - H.264/MPEG-4 AVC codec - Copyleft 2003-2024 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=12 sliced_threads=1 slices=12 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, flv, to 'rtmp://192.168.1.103:1935/live/msi5600':
Metadata:
encoder : Lavf61.1.100
Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuvj422p(pc, bt470bg/bt709/unknown, progressive), 1920x1080, q=2-31, 60 fps, 1k tbn
Metadata:
encoder : Lavc61.3.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
frame=63907 fps= 59 q=27.0 size= 1473531KiB time=00:17:45.42 bitrate=11329.9kbits/s speed=0.982x
使用vlc加载两个rtmp和flv两个流媒体数据
rtmp://192.168.1.103:1935/live/msi5600
使用html播放http-flv流媒体数据
<!DOCTYPE html>
<html>
<body>
<video id="videoElement" controls muted></video>
<button id="manualPlay" style="display:none;">点击播放</button>
<script src="https://cdn.jsdelivr.net/npm/flv.js@1.6.2/dist/flv.min.js"></script>
<script>
const videoElement = document.getElementById('videoElement');
const manualPlayBtn = document.getElementById('manualPlay');
let flvPlayer;
// 初始化播放器
function init() {
if (flvjs.isSupported()) {
flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'http://example.com/live.flv',
isLive: true
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
// 自动播放尝试(静音模式)
videoElement.muted = true;
const playPromise = videoElement.play();
if (playPromise !== undefined) {
playPromise.catch(e => {
manualPlayBtn.style.display = 'block';
});
}
}
}
// 用户手动触发播放
manualPlayBtn.addEventListener('click', () => {
videoElement.play();
manualPlayBtn.style.display = 'none';
});
// 页面加载后初始化
window.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>



浙公网安备 33010602011771号