nginx rtmp 服务
1、下载Nginx
wget https://nginx.org/download/nginx-1.21.6.tar.gz
2、将压缩包移到需要的安装目录下
mv nginx-1.21.6.tar.gz /usr/local
3、下载Nginx-Rtmp-Module
git clone https://github.com/arut/nginx-rtmp-module.git
4、将文件移到需要安装目录下
mv nginx-rtmp-module /usr/local
5、进入目录
cd /usr/local
6、解压Nginx压缩包
tar -zxvf nginx-1.21.6.tar.gz
7、进入Nginx目录
cd nginx-1.21.6
8、配置
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
9、安装
make make install
10、配置 nginx.conf 文件(/usr/local/nginx/conf下)
#user nobody;
# multiple workers works !
worker_processes 2;
#pid logs/nginx.pid;
events {
worker_connections 8192;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
record all;
record_path /tmp/av;
record_max_size 1K;
record_unique on;
allow publish all;
deny publish all;
allow play all;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
server_names_hash_bucket_size 128;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
#gzip on;
server {
listen 8080;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
11、启动Nginx
cd /usr/local/nginx/sbin ./nginx -t ./nginx -s reload
启动时可能会包错:nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
解决:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
12、测试(注意打开服务器8080和1935端口安全组)
访问服务器外网 IP:8080
备注:
使用工具 Nginx、Nginx-Rtmp-Module 下载地址 Nginx:https://nginx.org/en/download.html Nginx-Rtmp-Module:https://github.com/arut/nginx-rtmp-module sudo apt-get update sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install libssl-dev sudo apt install zlib1g-dev 可以先运行ffmpeg -list_devices true -f dshow -i dummy来列出所有设备 推摄像头RTMP流 ffmpeg -rtbufsize 100M -f dshow -video_size 1280x720 -framerate 30 -i video="HD Camera" -c:v libx264 -f flv rtmp://192.168.56.130:1935/live/stream 推本地视频文件RTMP流 ffmpeg -re -i C:\Users\xuxia\Downloads\xuziyan.mp4 -c:v copy -c:a copy -f flv rtmp://192.168.56.130:1935/live/stream ffmpeg -stream_loop -1 -re -i C:\Users\xuxia\Downloads\xuziyan.mp4 -c copy -f flv rtmp://192.168.56.130:1935/live/stream 推RTSP流(前提服务器支持接收RTSP推流): ffmpeg -rtbufsize 100M -f dshow -video_size 1280x720 -framerate 30 -i video="HD Camera" -c:v libx264 -f rtsp rtsp://192.168.56.130:8554/live/stream 你推的是RTMP流,客户端用: ffplay rtmp://192.168.56.130:1935/live/stream 你推的是RTSP流,客户端用: ffplay rtsp://192.168.56.130:8554/live/stream
python代码:
from flask import Flask, render_template, Response
from subprocess import Popen, PIPE
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/live/<string:stream_name>")
def live(stream_name):
return Response(generate_stream(stream_name), mimetype="application/x-mpegURL")
def generate_stream(stream_name):
command = [
"ffmpeg",
"-re",
"-i",
"C:\\Users\\xuxia\\Downloads\\xuziyan.mp4",
"-c:v",
"copy",
"-c:a",
"copy",
"-f",
"flv",
f"rtmp://192.168.56.130:1935/live/{stream_name}"
]
process = Popen(command, stdout=PIPE, stderr=PIPE)
while True:
yield process.stdout.read(1024)
@app.route("/play/<string:stream_name>")
def play(stream_name):
stream_url = f"rtmp://192.168.56.130:1935/live/{stream_name}"
return render_template("play.html", stream_url=stream_url)
# 静态资源下载
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory('static', filename)
if __name__ == "__main__":
app.run(debug=True)
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HLS Stream Player</title>
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
<script src="https://unpkg.com/video.js/dist/video.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</head>
<body>
<video id="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="800" height="560" data-setup='{}'>
<source src="http://192.168.56.130/hls/stream.m3u8" type="application/x-mpegURL">
Your browser does not support the video tag.
</video>
<script>
var myVideo = videojs('videoPlayer',{
bigPlayButton : true,
textTrackDisplay : false,
posterImage: false,
errorDisplay : false,
})
myVideo.play() // 视频播放
//myVideo.pause() // 视频暂停
</script>
</body>
</html>
效果:

本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18946064

浙公网安备 33010602011771号