目录
直接开始
实现功能是通过SRS推拉流服务器的DVR功能,录制视频,录制后,将文件(这里是mp4,可以配置)自动上传到Minio中,Minio中的存住路径可以通过.sh脚本进行调整。好了,大家时间都挺紧,废话不多说,直接开始。
SRS配置
listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}
http_api {
enabled on;
listen 1985;
https {
# Whether enable HTTPS API.
# default: off
enabled on;
# The listen endpoint for HTTPS API.
# default: 1990
listen 1990;
# The SSL private key file, generated by:
# openssl genrsa -out server.key 2048
# default: ./conf/server.key
key ./conf/server.key;
# The SSL public cert file, generated by:
# openssl req -new -x509 -key server.key -out server.crt -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=Me/OU=Me/CN=ossrs.net"
# default: ./conf/server.crt
cert ./conf/server.crt;
}
}
stats {
network 0;
}
rtc_server {
enabled on;
listen 8000; # UDP port
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate *;
}
vhost __defaultVhost__ {
exec {
enabled on;
# 需要执行的脚本,功能是监听文件夹变化并上传到minio
publish /bin/bash /root/srs/trunk/auto_upload_minio_dvr.sh [app] [stream];
}
rtc {
enabled on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp on;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
# 设置录制功能
dvr {
enabled on;
# 只录制指定的地址,如果录制全部,改为all
dvr_apply live/zxd_rtsp_1 live/123456;
dvr_path ./objs/nginx/html/[app]/[stream]/[2006]-[01]-[02]/[stream].[15].[04].[05].[999].mp4;
# 按时间段录制
dvr_plan segment;
# 30秒
dvr_duration 30;
# 开启关键帧
dvr_wait_keyframe on;
}
play {
time_jitter full;
mix_correct off;
}
}
同步脚本
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: \$0 <app> <stream> "
exit 1
fi
APP=$1
STREAM=$2
# 这里清理对应的进程,方便当前sh编辑后,直接替换运行就行,不用管进程的如何
ps -ef | grep "$APP/$STREAM" | grep -v grep | awk '{print $2}' | xargs -r sudo kill -9
ps -ef | grep "auto_upload_minio_dvr.sh $APP $STREAM" | grep -v grep | awk '{print $2}' | xargs -r sudo kill -9
# 需要按照mc也就是Minio的客户端
MC_COMMAND="mc"
# 这里是Minio中设置的授权信息
MINIO_SERVER="https://your.com/"
MINIO_ACCESS_KEY="LLdeh..............."
MINIO_SECRET_KEY="FfJcygaBjDLcglRFd1td...................."
CONFIG_NAME="myminio"
# Check if the configuration '$CONFIG_NAME' exists
if ! $MC_COMMAND config host ls | grep -q "$CONFIG_NAME"; then
$MC_COMMAND config host add $CONFIG_NAME $MINIO_SERVER $MINIO_ACCESS_KEY $MINIO_SECRET_KEY
fi
# Minio中的桶名称
BUCKET_NAME="video"
LOCAL_DIR="/root/srs/trunk/objs/nginx/html/$APP/$STREAM"
# 为了方便启动和更新配置,这里判断目录是否存在,也就是dvr是否启动了,有的流可能不允许录制
if [ -d "$LOCAL_DIR" ]; then
inotifywait -mr $LOCAL_DIR -e create,modify,move |
while read -r directory event file; do
filename=$(basename "$file")
if [[ "$filename" != *.tmp ]]; then
$MC_COMMAND cp $directory/$file $CONFIG_NAME/$BUCKET_NAME/srs/$APP/$STREAM/$(date +"%Y-%m-%d")/
fi
done
else
echo "Directory $LOCAL_DIR does not exist. Exiting."
fi
存在的问题
会生成2个sh进程,导致上传2次文件,Minio相同文件会覆盖,这个到不影响正常使用,改进后,我会补充