1、拉取代码
#国内用户推荐从同步镜像网站gitee下载
git clone --depth 1 https://gitee.com/xia-chu/ZLMediaKit
cd ZLMediaKit
#千万不要忘记执行这句命令
git submodule update --init
2、安装编译器
sudo yum -y install gcc
3、安装cmake
sudo yum -y install cmake
4、安装依赖(可选,已安装了可以不用安装)
#除了openssl,其他其实都可以不安装
sudo yum -y install libssl-dev
sudo yum -y install libsdl-dev
sudo yum -y install libavcodec-dev
sudo yum -y install libavutil-dev
sudo yum -y install ffmpeg
5、构建和编译项目
cd ZLMediaKit
mkdir build
cd build
cmake ..
make -j4
6、运行项目
运行项目我写了一个脚本,直接把脚本放到 ZLMediaKit/release/linux/Debug 这个目录下。然后停止启动都使用脚本即可。
脚本内容:
#!/bin/bash
# 定义 ZLMediaKit 可执行文件路径和日志文件路径
MEDIA_SERVER_PATH="./MediaServer"
# 检查 ZLMediaKit 是否在运行
check_status() {
PID=$(ps -ef | grep MediaServer | grep -v grep | awk '{print $2}')
if [ -z "$PID" ]; then
echo "MediaServer is not running."
return 1
else
echo "MediaServer is running with PID: $PID"
return 0
fi
}
# 启动 ZLMediaKit
start_server() {
check_status
if [ $? -eq 0 ]; then
echo "MediaServer is already running."
else
nohup $MEDIA_SERVER_PATH -d >/dev/null 2>&1 &
echo "MediaServer started."
fi
}
# 停止 ZLMediaKit
stop_server() {
check_status
if [ $? -eq 0 ]; then
kill $PID
echo "Stopping MediaServer..."
sleep 1 # 等待进程停止
check_status
if [ $? -eq 1 ]; then
echo "MediaServer stopped successfully."
else
echo "Failed to stop MediaServer. Trying to force stop."
kill -9 $PID
if [ $? -eq 0 ]; then
echo "MediaServer forcefully stopped."
else
echo "Failed to force stop MediaServer."
fi
fi
else
echo "MediaServer is not running."
fi
}
# 重启 ZLMediaKit
restart_server() {
stop_server
start_server
}
# 查看日志
view_log() {
LATEST_LOG=$(ls -t ./log/*.log | head -n 1)
if [ -f "$LATEST_LOG" ]; then
echo "Viewing latest log file: $LATEST_LOG"
tail -f "$LATEST_LOG"
else
echo "No log files found."
fi
}
# 脚本的入口,根据参数选择操作
case $1 in
start)
start_server
;;
stop)
stop_server
;;
restart)
restart_server
;;
status)
check_status
;;
log)
view_log
;;
*)
echo "Usage: $0 {start|stop|restart|status|log}"
exit 1
;;
esac
修改脚本可执行权限
chmod 777 ./run.sh
脚本的使用
./run.sh start(启动)/ restart(重启)/ status(状态) / log(日志) / stop(停止)
ZLM配置文件是在 ZLMediaKit/release/linux/Debug 这个目录下。文件名是 config.ini 下面这个文件是我的配置。
; auto-generated by mINI class {
[api]
apiDebug=1
defaultSnap=./www/logo.png
downloadRoot=./www
secret=m0ZnheLlYJisLdTmPeoosFMPa46tAYSU
snapRoot=./www/snap/
[cluster]
origin_url=
retry_count=3
timeout_sec=15
[ffmpeg]
bin=/usr/bin/ffmpeg
cmd=%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s
log=./ffmpeg/ffmpeg.log
restart_sec=0
snap=%s -rtsp_transport tcp -i %s -y -f mjpeg -frames:v 1 %s
[general]
broadcast_player_count_changed=0
check_nvidia_dev=1
enableVhost=0
enable_ffmpeg_log=0
flowThreshold=1024
listen_ip=::
maxStreamWaitMS=15000
mediaServerId=suweijie_zlm
mergeWriteMS=0
resetWhenRePlay=1
streamNoneReaderDelayMS=20000
unready_frame_cache=100
wait_add_track_ms=3000
wait_track_ready_ms=10000
[hls]
broadcastRecordTs=0
deleteDelaySec=10
fastRegister=0
fileBufSize=65536
segDelay=0
segDur=2
segKeep=0
segNum=3
segRetain=5
[hook]
alive_interval=10.0
enable=1
on_flow_report=
on_http_access=
on_play=http://192.168.200.1:18080/index/hook/on_play
on_publish=http://192.168.200.1:18080/index/hook/on_publish
on_record_mp4=http://192.168.200.1:18080/index/hook/on_record_mp4
on_record_ts=
on_rtp_server_timeout=http://192.168.200.1:18080/index/hook/on_rtp_server_timeout
on_rtsp_auth=
on_rtsp_realm=
on_send_rtp_stopped=http://192.168.200.1:18080/index/hook/on_send_rtp_stopped
on_server_exited=
on_server_keepalive=http://192.168.200.1:18080/index/hook/on_server_keepalive
on_server_started=http://192.168.200.1:18080/index/hook/on_server_started
on_shell_login=
on_stream_changed=http://192.168.200.1:18080/index/hook/on_stream_changed
on_stream_none_reader=http://192.168.200.1:18080/index/hook/on_stream_none_reader
on_stream_not_found=http://192.168.200.1:18080/index/hook/on_stream_not_found
retry=1
retry_delay=3.0
stream_changed_schemas=rtsp/rtmp/fmp4/ts/hls/hls.fmp4
timeoutSec=30
[http]
allow_cross_domains=1
allow_ip_range=::1,127.0.0.1,172.16.0.0-172.31.255.255,192.168.0.0-192.168.255.255,10.0.0.0-10.255.255.255
charSet=utf-8
dirMenu=1
forbidCacheSuffix=
forwarded_ip_header=
keepAliveSecond=30
maxReqSize=40960
notFound=<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>您访问的资源不存在!</h1></center><hr><center>ZLMediaKit(git hash:6889afb/2024-08-14T20:11:24+08:00,branch:master,build time:2024-08-17T20:27:02)</center></body></html>
port=18081
rootPath=./www
sendBufSize=65536
sslport=443
virtualPath=
[multicast]
addrMax=239.255.255.255
addrMin=239.0.0.0
udpTTL=64
[protocol]
add_mute_audio=1
auto_close=0
continue_push_ms=3000
enable_audio=1
enable_fmp4=1
enable_hls=1
enable_hls_fmp4=0
enable_mp4=0
enable_rtmp=1
enable_rtsp=1
enable_ts=1
fmp4_demand=0
hls_demand=0
hls_save_path=./www
modify_stamp=2
mp4_as_player=0
mp4_max_second=3600
mp4_save_path=./www
paced_sender_ms=0
rtmp_demand=0
rtsp_demand=0
ts_demand=0
[record]
appName=record
enableFmp4=0
fastStart=0
fileBufSize=65536
fileRepeat=0
sampleMS=500
[rtc]
externIP=
maxRtpCacheMS=5000
maxRtpCacheSize=2048
max_bitrate=0
min_bitrate=0
nackIntervalRatio=1.0
nackMaxCount=15
nackMaxMS=3000
nackMaxSize=2048
nackRtpSize=8
port=8000
preferredCodecA=PCMA,PCMU,opus,mpeg4-generic
preferredCodecV=H264,H265,AV1,VP9,VP8
rembBitRate=0
start_bitrate=0
tcpPort=8000
timeoutSec=15
[rtmp]
directProxy=1
enhanced=0
handshakeSecond=15
keepAliveSecond=15
port=1935
sslport=0
[rtp]
audioMtuSize=600
h264_stap_a=1
lowLatency=0
rtpMaxSize=10
videoMtuSize=1400
[rtp_proxy]
dumpDir=
gop_cache=1
h264_pt=98
h265_pt=99
opus_pt=100
port=10000
port_range=30000-30500
ps_pt=96
rtp_g711_dur_ms=100
timeoutSec=15
udp_recv_socket_buffer=4194304
[rtsp]
authBasic=0
directProxy=1
handshakeSecond=15
keepAliveSecond=15
lowLatency=0
port=554
rtpTransportType=-1
sslport=0
[shell]
maxReqSize=1024
port=0
[srt]
latencyMul=4
pktBufSize=8192
port=9000
timeoutSec=5
; } ---
这些配置在 ZLMediaKit/conf/config.ini 有详细的说明
有几个比较重要的配置。
配置 | 说明 |
---|---|
secret | 密钥(WVP中配置需跟config.ini一致) |
mediaServerId | 服务名(WVP中配置需跟config.ini一致) |
port | ZLM启动监听的http端口(WVP中配置需跟config.ini一致) |
port_range | rtp的监听端口范围(WVP中配置需跟config.ini一致) |
跟WVP搭配使用需要修改的就这个4个参数
以上就是搭建ZLMediaKit的完整步骤和配置说明。希望这篇文章能帮助到正在使用或计划使用ZLMediaKit的朋友们。如果有任何错误或遗漏,欢迎指正和补充。