NGINX MPEG-TS Live Module

Features

  • receives MPEG-TS over HTTP

  • produces and manages live HLS

  • produces and manages live MPEG-DASH

Compatibility

Build

Building nginx with the module:

# static module $ ./configure --add-module=/path/to/nginx-ts-module

# dynamic module $ ./configure --add-dynamic-module=/path/to/nginx-ts-module

Directives

ts

Syntax: ts                Context: location

Sets up a live MPEG-TS handler for the location. This directive is required for HLS or MPEG-DASH generation.

The last URI component is used as a stream name. For example, if the URI is /foo/bar/baz, the stream name is baz.

A simple way to stream MPEG-TS over HTTP is by running ffmpeg:

$ ffmpeg ... -f mpegts http://127.0.0.1:8000/foo

By default, HTTP request body size is limited in nginx. To enable live streaming without size limitation, use the directive client_max_body_size 0.

ts_hls

Syntax:  ts_hls path=PATH [segment=MIN[:MAX]] [segments=NUMBER] [max_size=SIZE] [noclean]

Context: location

Enables generating live HLS in the location. The PATH parameter specifies a directory where HLS playlist and segment files will be created. The directory is created if missing. For every publshed stream a subdirectory with the stream name is created under the PATH directory. The HLS playlist file created in the stream subdirectory is named index.m3u8. A path handler is installed to watch files in the directory. The old files in the directory are automatically deleted once they get old enough and are not supposed to be accessed by clients anymore. It is not allowed to reuse the path in other ts_hls or ts_dash directives.

The segment parameter specifies minimum and maximum segment durations. If the stream has video, segments are started at video key frames. If a key frame does not appear within MAX duration, the segment is truncated. The default value for minimum segment duration is 5 seconds. If unspecified, maximum segment duration is set to be twice as much as the minimum.

The segments parameter specifies the maximum number of segments in a playlist. As new segments are added to the playlist, the oldest segments are removed from it.

The max_size parameter specifies the maximum size of a segment. A segment is truncated once it reaches this size.

The noclean parameter indicates that the old files (segments and the playlist) should not be automatically deleted from disk.

Example:

location / {    ts;    ts_hls path=/var/hls segment=10s;}

ts_dash

Syntax: ts_dash path=PATH [segment=MIN[:MAX]] [segments=NUMBER] [max_size=SIZE] [noclean]

Context: location

Enables generating live MPEG-DASH in the location. The PATH parameter specifies a directory where MPEG-DASH manifest and segment files will be created. The directory is created if missing. For every publshed stream a subdirectory with the stream name is created under the PATH directory. The MPEG-DASH manifest file created in the stream subdirectory is named index.mpd. A path handler is installed to watch files in the directory. The old files in the directory are automatically deleted once they get old enough and are not supposed to be accessed by clients anymore. It is not allowed to reuse the path in other ts_hls or ts_dash directives.

The segment parameter specifies minimum and maximum segment durations. If the stream has video, segments are started at video key frames. If a key frame does not appear within MAX duration, the segment is truncated. The default value for minimum segment duration is 5 seconds. If unspecified, maximum segment duration is set to be twice as much as the minimum.

When setting an explicit value for the MAX parameter, the following note should be taken into account. If the next segment is shorter than the previous one by a factor more that two, dash.js can end up in a busy cycle requesting the second segment over and over again.

The segments parameter specifies the maximum number of segments in a manifest. As new segments are added to the manifest, the oldest segments are removed from it.

The max_size parameter specifies the maximum size of a segment. A segment is truncated once it reaches this size.

The noclean parameter indicates that the old files (segments and the manifest) should not be automatically deleted from disk.

Example:

location / {    ts;    ts_dash path=/var/hls segment=10s;}

Example

nginx.conf:

# nginx.confevents {}http {    server {        listen 8000;        location / {            root html;        }        location /publish/ {            ts;            ts_hls path=/var/media/hls segment=10s;            ts_dash path=/var/media/dash segment=10s;            client_max_body_size 0;        }        location /play/ {            types {                application/x-mpegURL m3u8;                application/dash+xml mpd;                video/MP2T ts;                video/mp4 mp4;            }            alias /var/media/;        }    }}

HLS in HTML:

<body>  <video width="640" height="480" controls autoplay         src="http://127.0.0.1:8000/play/hls/sintel/index.m3u8">  </video></body>

MPEG-DASH in HTML using the dash.js player:

<script src="http://cdn.dashjs.org/latest/dash.all.min.js"></script><body>  <video data-dashjs-player         width="640" height="480" controls autoplay         src="http://127.0.0.1:8000/play/dash/sintel/index.mpd">  </video></body>

Broadcasting a single-bitrate mp4 file:

$ ffmpeg -re -i ~/Movies/sintel.mp4 -bsf:v h264_mp4toannexb          -c copy -f mpegts http://127.0.0.1:8000/publish/sintel

Broadcasting an mp4 file in multiple bitrates. For proper HLS generation streams should be grouped into MPEG-TS programs with the -program option of ffmpeg:

$ ffmpeg -re -i ~/Movies/sintel.mp4 -bsf:v h264_mp4toannexb         -map 0:0 -map 0:1 -map 0:0 -map 0:1         -c:v:0 copy         -c:a:0 copy         -c:v:1 libx264 -b:v:1 100k         -c:a:1 libfaac -ac:a:1 1 -b:a:1 32k         -program "st=0:st=1" -program "st=2:st=3"         -f mpegts http://127.0.0.1:8000/publish/sinte
(0)

相关推荐

  • 音视频开发——通信直播协议和视频推流丨RTMP-RTSP

    近年来直播已成为互联网行业的大热话题,直播答题.游戏直播.竞赛直播,抖音直播,直播教育等层出不穷,直播早已成为人们耳熟能详的技术.事实上直播的兴起不仅与新时代人们要求为自己代言的心理有关,同时也得益于 ...

  • ffmepg将mp4视频切割成ts文件从而形成m3u8文件

    切片生成m3u8列表命令: ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls output.m3u8 此转换命令默认的每片(即一个ts文件)时长是2s, ...

  • SAP Spartacus 定义在app.module.ts里的providers依赖注入元数据何时得到处理

    我基于 SAP Spartacus library创建了一个自定义的Storefront实现.在其app.module.ts里,我采用自定义的MyCurrentProductService,去替换 S ...

  • NGINX MPEG-TS Live Module & Dash JS

    直播流程 视频直播:采集.前处理.编码.传输.解码.渲染 采集: 一般是由客户端(IOS.安卓.PC或其它工具,如OBS)完成的,iOS是比较简单的,Android则要做些机型适配工作,PC最麻烦各种 ...

  • 【课文 单词mp3】外研版九下Module 1

    对话音频: [对话翻译] 玲玲:欢迎归来,各位! 贝蒂:你好,玲玲!你的假期怎么样? 玲玲:还不错!我去看望了在河南省的祖父母.火车里挤满了人,我不得不站了3个多小时! 贝蒂:真倒霉.为什么在冬天旅行 ...

  • 【课文 单词mp3】外研版八下Module 10

    对话音频: [对话翻译] 陈欢:嗨,大家好.我是北京广播电台新闻部的主任. 玲玲:感谢您带我们参观 陈欢:不客气!现在,这边请.看那个红灯.当它亮时,表示我们 正在广播.在背景中我们应该避免制造任何噪 ...

  • 【课文 单词mp3】外研版八下Module 9

    对话音频: [对话翻译] 服务热线:你好,友谊服务热线.请问您是哪位? 玲玲:你好,我是玲玲. 服务热线:你好,玲玲!我可以为你做些什么? 玲玲:我和我最好的朋友之间有点问题.她叫-- 服务热线:不, ...

  • 【课文 单词mp3】外研版八下Module 8

    对话音频: [对话翻译] 玲玲:我们到了.欢迎来到北海公园. 托尼:哇!这里如此安静以至于我甚至能听见鸟儿在歌唱!我几乎不能相信我们在市中心. 玲玲:这个公园因为它的湖.桥和山上的古建筑而闻名.湖占据 ...

  • 【课文 单词mp3】外研版八下Module 7

    对话音频: [对话翻译] (玲玲正在为她的洛杉矾之旅作准备.) 玲玲:你好,贝蒂,我正在列旅行用品清单.我喜欢提前准备好东西.你能帮我吗? 贝蒂:当然可以.我怎么帮你? 玲玲:嗯,这听起来荒唐,但是我 ...

  • 【课文 单词mp3】外研版八下Module 6

    对话音频: [对话翻译] 玲玲:嗨,大家好!进来找地方坐下.不好意思,(家里)有点乱.我来把这些扇子放到架子上. 贝蒂:让我看一看.你有这么多扇子. 玲玲:是啊,大约六十把. 贝蒂:你从哪儿得到它们的 ...

  • 【课文 单词mp3】外研版八下Module 5

    对话音频: [对话翻译] 托尼:大明,我们已经完成了家庭作业.到看动画片的时间了. 大明:好主意,托尼.让我们看<超人>吧! 托尼:我们昨天看过<超人>了.我们为什么不看< ...