文章目录
- 参考:https://segmentfault.com/a/1190000041737848
- 参考:https://juejin.cn/post/7011803866941358117
一、安装编译器
#安装gcc
apt install build-essential
#安装cmake
apt install cmake
如果无法安装 Cmake 更新一下源:
apt update
二、获取代码
代码从git获取,如果没安装git,需要执行
sudo apt-get install git
cd /opt
#拉取项目代码
git clone https://github.com/ZLMediaKit/ZLMediaKit.git
#国内用户推荐从同步镜像网站gitee下载
git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit
cd ZLMediaKit
#不要忘了这句命令
git submodule update --init
三、安装编译依赖库
1.openssl安装编译
#如果之前安装了可以先卸载:apt -y remove openssl
cd /opt
#从git下载
git clone https://github.com/openssl/openssl.git
#如果git下载太慢或者连接有问题(比如我),可以到gitee下载
git clone https://gitee.com/mirrors/openssl.git
#下面的依次执行
mv openssl openssl-src && cd openssl-src
./config --prefix=/opt/openssl
make -j4
sudo make install
cd /opt/openssl
cp -rf lib64 lib
2.libsrtp安装编译
cd /opt
git clone https://gitee.com/mirrors/cisco-libsrtp.git
cd cisco-libsrtp
./configure --enable-openssl --with-openssl-dir=/opt/openssl
make -j4
sudo make install
四、构建和编译ZLMediaKit
cd /opt/ZLMediaKit
mkdir build
cd build
cmake .. -DENABLE_WEBRTC=true -DOPENSSL_ROOT_DIR=/opt/openssl -DOPENSSL_LIBRARIES=/opt/openssl/lib
cmake --build . --target MediaServer
上一步操作执行后,运行服务成功但没有demo页面,发现对应的www文件夹以及ssl证书并未放入指定目录,需要进行补充操作
把www文件夹复制到编译后的目录
cd /opt/ZLMediaKit
sudo cp -r www release/linux/Debug/
#把自带的ssl证书放到编译后的目录
sudo cp -r tests/default.pem release/linux/Debug/
五、启动ZLMediaKit服务
cd /opt/ZLMediaKit/release/linux/Debug
#通过-h可以了解启动参数
./MediaServer -h
#以守护进程模式启动
./MediaServer -d &
#如果需要优雅的关闭
killall -2 MediaServer
六、推流测试
H264推流
ffmpeg -re -i boss.mp4 -vcodec h264 -acodec aac -f rtsp -rtsp_transport tcp rtsp://120.76.46.85/live/test
H265推流
ffmpeg -re -i test.mp4 -vcodec h265 -acodec aac -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test
HLS 推流
ffmpeg -i test.mp4 -c:v libx264 -c:a aac -b:v 1M -b:a 128k -hls_time 4 -hls_list_size 0 -hls_segment_filename "video_%03d.ts" -hls_flags single_file stream.m3u8
拉流测试
VLC播放器打开一个网络串流,输入如下地址就可以播放了。
rtsp://192.168.1.140/live/test
rtmp://192.168.1.140/live/test
http://192.168.1.140/live/test/hls.m3u8
其中 192.168.1.140 是开启视频服务的机器 IP 地址
七、服务器日志
root@iZwz99vom0kq9wknx6n0tlZ:/opt/ZLMediaKit/release/linux/Debug# sudo ./MediaServer -d
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: H264
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: H265
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: JPEG
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: mpeg4-generic
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: opus
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: PCMA
2025-08-27 23:43:49.244 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: PCMU
2025-08-27 23:43:49.245 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: L16
2025-08-27 23:43:49.245 I [MediaServer] [42036-MediaServer] Factory.cpp:35 registerPlugin | Load codec: MP3
2025-08-27 23:43:49.246 D [MediaServer] [42036-MediaServer] System.cpp:157 startDaemon | 启动子进程:42039
2025-08-27 23:43:49.247 I [MediaServer] [42039-MediaServer] System.cpp:203 systemSetup | core文件大小设置为:18446744073709551615
2025-08-27 23:43:49.248 I [MediaServer] [42039-MediaServer] System.cpp:212 systemSetup | 文件最大描述符个数设置为:65535
2025-08-27 23:43:49.248 I [MediaServer] [42039-MediaServer] main.cpp:270 start_main | ZLMediaKit(git hash:f524a0e/2025-08-23T10:41:06+08:00,branch:master,build time:2025-08-27T23:20:14)
2025-08-27 23:43:49.256 D [MediaServer] [42039-MediaServer] SSLBox.cpp:181 setContext | Add certificate of: default.zlmediakit.com
2025-08-27 23:43:49.257 D [MediaServer] [42039-stamp thread] util.cpp:437 operator() | Stamp thread started
2025-08-27 23:43:49.259 I [MediaServer] [42039-MediaServer] EventPoller.cpp:626 EventPollerPool | EventPoller created size: 2
2025-08-27 23:43:49.260 I [MediaServer] [42039-MediaServer] main.cpp:395 start_main | 已启动http api 接口
2025-08-27 23:43:49.260 I [MediaServer] [42039-MediaServer] main.cpp:397 start_main | 已启动http hook 接口
2025-08-27 23:43:49.261 I [MediaServer] [42039-MediaServer] TcpServer.cpp:260 start_l | TCP server listening on [::]: 554
2025-08-27 23:43:49.262 I [MediaServer] [42039-MediaServer] TcpServer.cpp:260 start_l | TCP server listening on [::]: 1935
2025-08-27 23:43:49.262 I [MediaServer] [42039-MediaServer] TcpServer.cpp:260 start_l | TCP server listening on [::]: 80
2025-08-27 23:43:49.262 I [MediaServer] [42039-MediaServer] TcpServer.cpp:260 start_l | TCP server listening on [::]: 443
2025-08-27 23:43:49.264 I [MediaServer] [42039-MediaServer] UdpServer.cpp:128 start_l | UDP server bind to [::]: 10000
2025-08-27 23:43:49.264 I [MediaServer] [42039-MediaServer] TcpServer.cpp:260 start_l | TCP server listening on [::]: 10000
2025-08-27 23:43:49.265 I [MediaServer] [42039-MediaServer] UdpServer.cpp:128 start_l | UDP server bind to [::]: 9000
2025-08-27 23:45:05.278 W [MediaServer] [42039-event poller 1] RtspSession.cpp:62 onError | 2-14(183.134.104.172:38564) RTSP播放器(//)断开:illegal connection,耗时(s):15
2025-08-27 23:49:41.332 W [MediaServer] [42039-event poller 0] RtpSession.cpp:71 onError | 3-25(89.248.164.165:39718) __defaultVhost__/rtp/ 2(illegal connection)
2025-08-27 23:50:19.009 W [MediaServer] [42039-event poller 1] RtspSession.cpp:62 onError | 4-25(115.238.44.234:43070) RTSP播放器(//)断开:Assertion failed: (!url.empty(), empty url), function splitUrl, file /opt/ZLMediaKit/src/Common/Parser.cpp, line 314.,耗时(s):0
2025-08-27 23:50:32.762 W [MediaServer] [42039-event poller 0] MediaSink.cpp:128 checkTrackIfReady | Audio track index 1 codec mpeg4-generic receive no data for long 1841ms. Ignore it!
2025-08-27 23:50:32.762 D [MediaServer] [42039-event poller 0] MediaSink.cpp:196 emitAllTrackReady | All track ready use 1841ms
2025-08-27 23:50:32.763 I [MediaServer] [42039-event poller 0] MediaSource.cpp:505 emitEvent | 媒体注册:fmp4://__defaultVhost__/live/test
2025-08-27 23:50:32.763 I [MediaServer] [42039-event poller 0] MultiMediaSourceMuxer.cpp:707 onAllTrackReady | stream: rtsp://120.76.46.85:554/live/test , codec info: mpeg4-generic[8000/1/16] H264[852/480/30]
2025-08-27 23:50:32.763 I [MediaServer] [42039-event poller 0] MediaSource.cpp:505 emitEvent | 媒体注册:rtmp://__defaultVhost__/live/test
2025-08-27 23:50:32.764 I [MediaServer] [42039-event poller 0] MediaSource.cpp:505 emitEvent | 媒体注册:ts://__defaultVhost__/live/test
2025-08-27 23:50:32.764 I [MediaServer] [42039-event poller 0] MediaSource.cpp:505 emitEvent | 媒体注册:rtsp://__defaultVhost__/live/test
2025-08-27 23:50:41.162 I [MediaServer] [42039-event poller 0] MediaSource.cpp:505 emitEvent | 媒体注册:hls://__defaultVhost__/live/test
2025-08-27 23:53:36.907 I [MediaServer] [42039-event poller 1] Rtsp.cpp:471 getPortPair | got port from pool:33884-33885
2025-08-27 23:53:36.921 I [MediaServer] [42039-event poller 1] Rtsp.cpp:471 getPortPair | got port from pool:34674-34675
2025-08-27 23:53:36.933 I [MediaServer] [42039-event poller 1] RtspSession.cpp:823 handleReq_Play | 12-27(119.136.135.77:50993) rtsp seekTo(ms):0
2025-08-27 23:55:53.613 I [MediaServer] [42039-event poller 0] Rtsp.cpp:471 getPortPair | got port from pool:33180-33181
2025-08-27 23:55:53.640 I [MediaServer] [42039-event poller 0] Rtsp.cpp:471 getPortPair | got port from pool:33396-33397
2025-08-27 23:55:53.662 I [MediaServer] [42039-event poller 0] RtspSession.cpp:823 handleReq_Play | 19-32(119.136.135.77:52041) rtsp seekTo(ms):0