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

这篇具有很好参考价值的文章主要介绍了[多媒体服务器] 通过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:静态流点播场景,通过hls或dash将MP4之类的多媒体封装文件转换为m3u8进行点播

通过ffmpeg把MP4文件转换为m3u8列表集:

ffmpeg -i input.mp4 -codec: copy -start_number 0 -hls_time 10 -hls_list_size 0 -hls_wrap 10 output.m3u8

参数解释:

  • -i input.mp4: 输入文件

  • -codec: copy: 复制编解码器

  • -start_number 0: 分片从0开始编号

  • -hls_time 10: 每个分片的时长为10秒

  • -hls_list_size 0: 播放列表不限制大小

  • -hls_wrap 10: 当达到指定的段数时,重新开始

  • output.m3u8: 输出的播放列表文件

配置nginx以支持HLS,打开/etc/nginx/nginx.conf,添加如下内容:

http {
    ...
 
    server {
        listen 80;
        server_name localhost.com;
 
        location /hls/ {
            # 假设m3u8和TS文件存储在/home/yk/VOD/hls/1/目录下
            root /home/yk/VOD/hls/1/;
            # 添加跨域配置(如果需要)
            add_header 'Access-Control-Allow-Origin' '*' always;
            # 添加HLS所需的MIME类型
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            # 禁止浏览目录
            autoindex off;
        }
    }
}

nginx重新加载配置:

sudo nginx -s reload

浏览器中访问:

 比如 ffmpeg 输出的 ts 文件集 和 m3u8 列表在 /home/yk/VOD/1/ 目录下,那么在浏览器中输入

http://localhost.com:80/output.m3u8




Step3: 静态流直播场景,把媒体文件推送给nginx rtmp服务进行代理(rtmp不支持浏览器播放,只能通过vlc等播放器播放)

安装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.




Step4:静态流直播场景,支持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




Step5:管理rtmp资源(可选,数据统计页面)

经过step2以后,我们便可以通过支持rtmp协议的播放器进行点播了。接着,需要开启RTMP的统计页面,这样就不需要不断地往nginx.conf文件里加配置来实现rtmp功能。我们可以创建一个配置文件:

/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;
    }
}

 接着创建目录:

/var/www/html/rtmp

然后通过如下命令把 libnginx-mod-rtmp的xsl配置文件解压到上述目录中:

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

打开访问统计数据页面的防火墙:

sudo ufw allow from your_ip_address to any port http-alt

创建软连接(这步是惯例,可以不做,具体原因后续补充):

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

重新加载nginx配置

sudo systemctl reload nginx.service

至此,统计页面可以通过如下方式访问

http://your_domain:8080/stat 

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.




Step6:动态流直播场景,使用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.文章来源地址https://www.toymoban.com/news/detail-857362.html

到了这里,关于[多媒体服务器] 通过nginx搭建 rtmp/hls/dash 媒体服务器,支持点播和直播的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 多媒体音频焦点浅析

    多个音源可以同时向同一个输出流进行播放音频,如果没有音频焦点管控,就会出现多个音源同时播放的现象,给用户带来不便;而Android为了避免多个音源同时播放,就引入了音频焦点的概念,所有音频应用都统一按照音频焦点的规定执行,就可以避免该现象发生。 当应用

    2024年02月13日
    浏览(28)
  • 多媒体API

    许小墨のBlog —— 菜鸡博客直通车 系列文章完整版,配图更多,CSDN博文图片需要手动上传,因此文章配图较少,看不懂的可以去菜鸡博客参考一下配图! 前端系列文章——传送门 后端系列文章——传送门 video 只接受几种视屏格式:ogg、mp4、avi 基本使用: controls属性,出现

    2024年02月02日
    浏览(44)
  • 多媒体开发之cgo

         go语言作为近十年来优秀的现代开发语言的代表,由于继承了c语言的简洁和很多现代语言的表达方式,在广泛的应用场景中得到众多爱好者的喜爱,如何将go和c、c++进行联合开发,拓展整个开发生态,不用重复造轮子,掌握cgo可以让你得心应手的在c和go之间传递信息,

    2024年02月16日
    浏览(34)
  • AIGC生成多媒体流程

    给定 生成多个故事标题 多个故事标题进行反向推导出 再生成标题 直到达到一个相似度 多个标题固定总结合并为一个标题 根据生成故事多个章节标题 多个章节标题反向生成一个标题 对比前后两个标题相似度 不断重复直到达到一定相似度 第一个章

    2024年02月12日
    浏览(32)
  • 鸿蒙实战多媒体运用:【音频组件】

    音频组件用于实现音频相关的功能,包括音频播放,录制,音量管理和设备管理。 图 1  音频组件架构图 基本概念 采样 采样是指将连续时域上的模拟信号按照一定的时间间隔采样,获取到离散时域上离散信号的过程。 采样率 采样率为每秒从连续信号中提取并组成离散信号

    2024年03月10日
    浏览(41)
  • 计算机网络——多媒体网络

    通俗易懂,风趣幽默,忍不住分享一下给大家, 跳转到网站 我的计算机网络专栏,是自己在计算机网络学习过程中的学习笔记与心得,在参考相关教材,网络搜素的前提下,结合自己过去一段时间笔记整理,而推出的该专栏,整体架构是根据计算机网络 自顶向下 方法而整理

    2024年02月20日
    浏览(25)
  • Java UI组件和多媒体

    目录 1、使用单选按钮 2、选择几何图形  3、交通信号灯  4、演示TextField的属性 5、演示TextArea的属性 6、选择一种字体  7、演示 Label 的属性  8、使 用ComboBox 和 ListView  9、使 用 ScrollBar 和 Slider    10、模拟:一个转动的风扇 编写一个 GUI 程序如图所示 。 可以使用按钮将消

    2024年02月09日
    浏览(32)
  • HTML5多媒体单元测试

    (单选题, 10.0分) 为元素指定多个视频源使用( )标签(元素)。 A select B datalist C source D src (单选题, 10.0分) 判断浏览器是否支持指定的媒体类型需用到audio或video对象的( )方法。 A load() B play() C pause() D canPlayType() (多选题, 10.0分) HTML5新增了强大的多媒体的功能,主要体现在

    2024年02月04日
    浏览(31)
  • 设计HTML5图像和多媒体

    在网页中的文本信息直观、明了,而多媒体信息更富内涵和视觉冲击力。恰当使用不同类型的多媒体可以展示个性,突出重点,吸引用户。在HTML5之前,需要借助插件为网页添加多媒体,如Adobe Flash Player、苹果的QuickTime等。HTML5引入原生的多媒体技术,设计多媒体更简便,用户

    2024年02月12日
    浏览(27)
  • 多媒体网络教学模式的评价方式

    传统教学评价只注重学生掌握的基本知识,忽视其他各方面的技能,已经不能 满足多媒体网络教学模式下的评价要求。建构主义指导下的评价方式也应遵循以学 习者为中心的原则,强调合作学习,体现学习过程的重要性。因此,评价方式的多 元化必然会取代传统单一的评价

    2024年02月11日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包