[多媒体服务器] 通过nginx搭建 rtmp/hls/dash 媒体服务器,支持点播和直播

参考:

How To Set Up a Video Streaming Server using Nginx-RTMP on Ubuntu 20.04 | DigitalOcean

用到的工具:

nginx,nginx rtmp插件,OBS,ffmpeg,ubuntu,youtube-dl

Step1:安装和配置nginx

安装 nginx 和 rtmp 模块

sudo apt install nginx
sudo apt update
sudo apt install libnginx-mod-rtmp

增加如下内容到nginx配置文件 nginx.conf

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                allow publish 127.0.0.1;
                deny publish all;

                application live {
                        live on;
                        record off;
                }
        }
}

说明:

  • listen 1935 means that RTMP will be listening for connections on port 1935, which is standard.
  • chunk_size 4096 means that RTMP will be sending data in 4KB blocks, which is also standard.
  • allow publish 127.0.0.1 and deny publish all mean that the server will only allow video to be published from the same server, to avoid any other users pushing their own streams.
  • application live defines an application block that will be available at the /live URL path.
  • live on enables live mode so that multiple users can connect to your stream concurrently, a baseline assumption of video streaming.
  • record off disables Nginx-RTMP’s recording functionality, so that all streams are not separately saved to disk by default.

打开1935端口的防火墙限制

sudo ufw allow 1935/tcp

nginx重新加载配置文件nginx.conf

sudo systemctl reload nginx.service

Step2: 点播场景,把媒体文件推送给nginx rtmp服务进行代理

安装ffmpeg

sudo apt install ffmpeg

安装youtube-dl

sudo pip install youtube-dl

(可选)从youtube上下载一个文件备用,也可以随便找一个MP4文件

youtube-dl https://www.youtube.com/watch?v=iom_nhYQIYk

使用ffmpeg处理媒体文件,并将其代理给rtmp服务器

ffmpeg -re -i "Introducing App Platform by DigitalOcean-iom_nhYQIYk.mkv" -c:v copy -c:a aac -ar 44100 -ac 1 -f flv rtmp://localhost/live/stream

 rtmp://localhost/live/stream 中的 localhost 代表本机,不用动,live是nginx.conf文件里的 application live,如果是 application live1,那么这里就是 live1 , stream 是当前流的标识,可以自定义为任何字符串。

Note: You can also stream directly to, for example, Facebook Live using ffmpeg without needing to use Nginx-RTMP at all by replacing rtmp://localhost/live/stream in your ffmpeg command with rtmps://live-api-s.facebook.com:443/rtmp/your-facebook-stream-key. YouTube uses URLs like rtmp://a.rtmp.youtube.com/live2. Other streaming providers that can consume RTMP streams should behave similarly.

Step3:直播场景,使用OBS进行直播流代理

ffmpeg只能处理点播场景,直播场景需要使用OBS进行流代理。

安装OBS,check Open Broadcaster Software | OBS

Streaming via ffmpeg is convenient when you have a prepared video that you want to play back, but live streaming can be much more dynamic. The most popular software for live streaming is OBS, or Open Broadcaster Software – it is free, open source, and very powerful.

OBS is a desktop application, and will connect to your server from your local computer.

After installing OBS, configuring it means customizing which of your desktop windows and audio sources you want to add to your stream, and then adding credentials for a streaming service. This tutorial will not be covering your streaming configuration, as it is down to preference, and by default, you can have a working demo by just streaming your entire desktop. In order to set your streaming service credentials, open OBS’ settings menu, navigate to the Stream option and input the following options:

Streaming Service: Custom
Server: rtmp://your_domain/live
Play Path/Stream Key: obs_stream

obs_stream is an arbitrarily chosen path – in this case, your video would be available at rtmp://your_domain/live/obs_stream. You do not need to enable authentication, but you do need to add an additional entry to the IP whitelist that you configured in Step 1.

Back on the server, open Nginx’s main configuration file, /etc/nginx/nginx.conf, and add an additional allow publish entry for your local IP address. If you don’t know your local IP address, it’s best to just go to a site like What’s my IP which can tell you where you accessed it from:

  1. sudo nano /etc/nginx/nginx.conf

Copy

/etc/nginx/nginx.conf

. . .
                allow publish 127.0.0.1;
                allow publish your_local_ip_address;
                deny publish all;
. . .

Save and close the file, then reload Nginx:

  1. sudo systemctl reload nginx.service

Copy

You should now be able to close OBS’ settings menu and click Start Streaming from the main interface! Try viewing your stream in a media player as before. Now that you’ve seen the fundamentals of streaming video in action, you can add a few other features to your server to make it more production-ready.

Step4:管理rtmp资源

Now that you have Nginx configured to stream video using the Nginx-RTMP module, a common next step is to enable the RTMP statistics page. Rather than adding more and more configuration details to your main nginx.conf file, Nginx allows you to add per-site configurations to individual files in a subdirectory called sites-available/. In this case, you’ll create one called rtmp:

  1. sudo nano /etc/nginx/sites-available/rtmp

Copy

Add the following contents:

/etc/nginx/sites-available/rtmp

server {
    listen 8080;
    server_name  localhost;

    # rtmp stat
    location /stat {
        rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }
    location /stat.xsl {
        root /var/www/html/rtmp;
    }

    # rtmp control
    location /control {
        rtmp_control all;
    }
}

Save and close the file. The stat.xsl file from this configuration block is used to style and display an RTMP statistics page in your browser. It is provided by the libnginx-mod-rtmp library that you installed earlier, but it comes zipped up by default, so you will need to unzip it and put it in the /var/www/html/rtmp directory to match the above configuration. Note that you can find additional information about any of these options in the Nginx-RTMP documentation.

Create the /var/www/html/rtmp directory, and then uncompress the stat.xsl.gz file with the following commands:

  1. sudo mkdir /var/www/html/rtmp
  2. sudo gunzip -c /usr/share/doc/libnginx-mod-rtmp/examples/stat.xsl.gz > /var/www/html/rtmp/stat.xsl

Copy

Finally, to access the statistics page that you added, you will need to open another port in your firewall. Specifically, the listen directive is configured with port 8080, so you will need to add a rule to access Nginx on that port. However, you probably don’t want others to be able to access your stats page, so it’s best only to allow it for your own IP address. Run the following command:

  1. sudo ufw allow from your_ip_address to any port http-alt

Copy

Next, you’ll need to activate this new configuration. Nginx’s convention is to create symbolic links (like shortcuts) from files in sites-available/ to another folder called sites-enabled/ as you decide to enable or disable them. Using full paths for clarity, make that link:

  1. sudo ln -s /etc/nginx/sites-available/rtmp /etc/nginx/sites-enabled/rtmp

Copy

Now you can reload Nginx again to process your changes:

  1. sudo systemctl reload nginx.service

Copy

You should now be able to go to http://your_domain:8080/stat in a browser to see the RTMP statistics page. Visit and refresh the page while streaming video and watch as the stream statistics change.

You’ve now seen how to monitor your video stream and push it to third party providers. In the final section, you’ll learn how to provide it directly in a browser without the use of third party streaming platforms or standalone media player apps.

Step5:支持hls和dash,以便通过浏览器播放

浏览器目前都不支持rtmp协议播放流媒体,如果希望通过浏览器播放,那么需要打开hls和dash协议支持。

打开 nginx.conf 文件,添加如下内容:

. . .
rtmp {
        server {
. . .
                application live {
                    	live on;
                    	record off;
                        hls on;
                        hls_path /var/www/html/stream/hls;
                        hls_fragment 3;
                        hls_playlist_length 60;

                        dash on;
                        dash_path /var/www/html/stream/dash;
                }
        }
}
. . .

打开 sites-available/rtmp ,添加如下内容:

. . .
server {
    listen 8088;

    location / {
        add_header Access-Control-Allow-Origin *;
        root /var/www/html/stream;
    }
}

types {
    application/dash+xml mpd;
}

放开8088端口的防火墙:

sudo ufw allow 8088/tcp

创建临时媒体文件存放路径给nginx用(参考上面的nginx.conf里的配置):

sudo mkdir /var/www/html/stream

重启nginx:

sudo systemctl reload nginx

浏览器上访问如下地址即可播放hls和dash

http://your_domain:8088/hls/stream.m3u8

http://your_domain:8088/dash/stream.mpd

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/429986.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2核4G服务器支持多少人在线?腾讯云全访问测试

腾讯云轻量应用服务器2核4G5M配置一年优惠价165元、252元15个月、三年756元,100%CPU性能,5M带宽下载速度640KB/秒,60GB SSD系统盘,月流量500GB,折合每天16.6GB流量,超出月流量包的流量按照0.8元每GB的价格支…

WSL2安装Ubuntu18.04到指定路径(非C盘)

1 系统设置开启WSL 1.1 在搜索框搜索“启动或关闭Windows功能”或在“控制面板”->“程序”->“启用或关闭 windows 功能” 开启 Windows 虚拟化和 Linux 子系统(WSL2)以及Hyper-V 按照提示重启计算机,开启WSL。 2 将WSL2 设置为默认版本 wsl --se…

Mysql删除重复项:力扣196. 删除重复的电子邮箱

题目链接:196. 删除重复的电子邮箱 - 力扣(LeetCode) 题目描述 sql语句 # Write your MySQL query statement below delete a from person as a inner join person as b where a.email b.email and a.id > b.id 思路:内连接…

MySQL NDB Cluster 分布式架构搭建 自定义启动、重启和关闭集群Shell脚本

此次NDB Cluster使用三台虚拟机进行搭建,一台作为管理节点;而对于另外两台服务器,每一台都充当着数据节点和SQL节点的角色。注意不是MGR主从复制架构,而是分布式MySQL架构。 创建 /var/lib/mysql-cluster/config.ini Cluster全局…

uipath调用python代码获取网站验证码

用uipath自带的ocr读验证码不是很准确,选择调用python读验证码,需要导入ddddocr(3.8以下版本支持ddddocr) 用uipath程序将验证码图片保存到本地(也可以直接用python处理图片,保存到本地比较简单&#xff0…

xss.haozi.me:0X0D

alert(1) -> 记住要回车一下-->是js的一个注释符但是只能用在最前面前面有一个空格都不行

C++:String的模拟实现

模拟实现的节奏比较快,大家可以先去看看博主的关于string的使用,然后再来看这里的模拟实现过程 C:String类的使用-CSDN博客 String模拟实现大致框架迭代器以及迭代器的获取(public定义,要有可读可写的也要有可读不可写…

基于springboot+vue的医院药品管理系统

博主主页:猫头鹰源码 博主简介:Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战,欢迎高校老师\讲师\同行交流合作 ​主要内容:毕业设计(Javaweb项目|小程序|Pyt…

【Android】源码解析 Activity 的构成

本文是基于 Android 14 的源码解析。 当我们写 Activity 时会调用 setContentView() 方法来加载布局。现在来看看 setContentView() 方法是怎么实现的,源码如下所示: 路径:/frameworks/base/core/java/android/app/Activity.javapublic void…

Linux中服务端开发

1 创建socket,返回一个文件描述符lfd---socket(); 2 将lfd和IP,PROT进行绑定---bind(); 3 将lfd由主动变成被动监听---listen(); 4 接收一个新的连接,得到一个的文件描述符cfd--accept() --该文件描述符用于与客户端通信 5 while(1) { 接受数据&a…

【扩散模型系列3】DiT开源项目

文章目录 DiT原始项目Fast-DiT readmeSamplingTraining训练之前的准备训练DiTPyTorch 训练结果改进训练效果 Evaluation (FID, Inception Score, etc.) 总结 DiT原始项目 该项目仅针对DiT训练,并未包含VAE 的训练 项目地址 论文主页 Fast-DiT readme 该项目仅针…

性能优化篇(七) UI优化注意事项以及使用Sprite Atlas打包精灵图集

UI优化注意事项 1.尽量避免使用IMGUI(OnGUI)来做游戏时的UI,因为IMGUI的开销比较大。 2.如果一个UGUI的控件不需要进行射线检测,则可以取消勾选Raycast Target 3.尽量避免使用完全透明的图片和UI控件。因为即使完全透明,我们看不见它&#xf…

论文笔记:Code Llama: Open Foundation Models for Code

导语 Code Llama是开源模型Llama 2在代码领域的一个专有模型,作者通过在代码数据集上进行进一步训练得到了了适用于该领域的专有模型,并在测试基准中超过了同等参数规模的其他公开模型。 链接:https://arxiv.org/abs/2308.12950机构&#x…

[cg] Games 202 - NPR 非真实感渲染

NPR特性(基于真实感渲染) 真实感--》翻译成非真实感的过程 NPR风格 需要转换为渲染中的操作 1.描边 B-->普通边界(不是下面几种的) C-->折痕 M-->材质边界 S-->需要在物体外面一圈上,并且是多个面共享…

win11部署自己的privateGpt(2024-0304)

什么是privateGpt? privategpt开源项目地址 https://github.com/imartinez/privateGPT/tree/main 官方文档 https://docs.privategpt.dev/overview/welcome/welcome PrivateGPT是一个可投入生产的人工智能项目,利用大型语言模型(LLMs)的…

流行 NFT 的必备指南

​作者:stellafootprint.network 编译:mingfootprint.network 来源:Footprint Analytics Blog 随着爱好者们对 NFT 的兴趣不断高涨,Footprint Analytics 发布了一系列文章,重点介绍各种热门 NFT 系列。这些文章深入…

GBU808-ASEMI整流桥GBU808参数、封装、尺寸

编辑:ll GBU808-ASEMI整流桥GBU808参数、封装、尺寸 型号:GBU808 品牌:ASEMI 封装:GBU-4 最大重复峰值反向电压:800V 最大正向平均整流电流(Vdss):8A 功率(Pd):中小功率 芯片个数&#…

【网站项目】075学生信息管理系统

🙊作者简介:拥有多年开发工作经验,分享技术代码帮助学生学习,独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。🌹赠送计算机毕业设计600个选题excel文件,帮助大学选题。赠送开题报告模板&#xff…

xss.haozi.me:0x0B

<svg><script>(1)</script>

WebSocket 详解教程

概述 WebSocket 是什么&#xff1f; WebSocket 是一种网络通信协议。RFC6455 定义了它的通信标准。 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。 为什么需要 WebSocket &#xff1f; 了解计算机网络协议的人&#xff0c;应该都知道&#x…