title: 使用nginx搭建一个rtmp直播服务器,来实现一天24小时播放音乐的电台
date: 2023-03-08 16:58:05.588
updated: 2023-03-11 17:38:50.116
url: https://hexo.start237.top/archives/使用nginx搭建一个rtmp直播服务器来实现一天24小时播放音乐的电台
categories:
- IT技术
tags:
搭建rtmp服务
https://github.com/TheQuib/dji-obs-stream 参考这个链接。
里面有现成的window下集成好的nginx程序。
使用ffmpeg推流
参考文章
https://xuan-ming.github.io/2020/06/04/ffmpeg/
ffmpeg -re -i ./m.wav -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/live/abc
nginx.conf关键配置项
nginx hls配置参考文章
https://blog.csdn.net/Harry_z666/article/details/114984077
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
hls on;
live on;
interleave on;
record off;
hls_path E:/dji-obs-stream-main/Windows/html/hls;
hls_fragment 5s;
hls_playlist_length 15s;
hls_continuous on;
hls_cleanup on;
hls_nested on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
}
alias E:/dji-obs-stream-main/Windows/html/hls;
expires -1;
add_header Cache-Control no-cache;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
写一个拉流页面
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 直播</title>
<head>
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.15.2/skins/default/aliplayer-min.css" />
<script charset="utf-8" type="text/javascript"
src="https://g.alicdn.com/de/prismplayer/2.15.2/aliplayer-min.js"></script> //(必须)引入js文件。
</head>
</head>
<body>
<body>
<div id="J_prismPlayer"></div>
</body>
</body>
</html>
<script>
var player = new Aliplayer({
id: 'J_prismPlayer',
format: "hls",
source: "http://localhost/hls/abc/index.m3u8",
autoplay: true,
isLive: true,
}, function (player) {
console.log('The player is created.')
})
// 也可以使用replayByMediaAuth函数切换不同的mediaAuth播放(前提是已经使用mediaAuth初始化播放器)。
// player.replayByMediaAuth('another mediaAuth');
</script>
搞定!
这是windows的环境,在linux环境中还要细化,另外还要写脚本来实现把音频循环播放的功能。