Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_mp4_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9
<module name="Module ngx_http_mp4_module"
10
link="/en/docs/http/ngx_http_mp4_module.html"
11
lang="en"
12
rev="9">
13
14
<section id="summary">
15
16
<para>
17
The <literal>ngx_http_mp4_module</literal> module provides pseudo-streaming
18
server-side support for MP4 files.
19
Such files typically have the <path>.mp4</path>, <path>.m4v</path>,
20
or <path>.m4a</path> filename extensions.
21
</para>
22
23
<para>
24
Pseudo-streaming works in alliance with a compatible media player.
25
The player sends an HTTP request to the server with the start time
26
specified in the query string argument (named simply
27
<literal>start</literal>
28
and specified in seconds), and the server responds with the stream
29
such that its start position corresponds to the requested time,
30
for example:
31
<example>
32
http://example.com/elephants_dream.mp4?start=238.88
33
</example>
34
This allows performing a random seeking at any time, or starting playback
35
in the middle of the timeline.
36
</para>
37
38
<para>
39
To support seeking, H.264-based formats store metadata
40
in a so-called “moov atom”.
41
It is a part of the file that holds the index information for the
42
whole file.
43
</para>
44
45
<para>
46
To start playback, the player first needs to read metadata.
47
This is done by sending a special request with the
48
<literal>start=0</literal> argument.
49
A lot of encoding software insert the metadata at
50
the end of the file.
51
This is suboptimal for pseudo-streaming, because the player
52
has to download the entire file before starting playback.
53
If the metadata are located at the beginning of the file,
54
it is enough for nginx to simply start sending back the file contents.
55
If the metadata are located at the end of the file,
56
nginx must read the entire file and prepare a new stream so that
57
the metadata come before the media data.
58
This involves some CPU, memory, and disk I/O overhead,
59
so it is a good idea to
60
<link
61
url="https://github.com/flowplayer/flowplayer/wiki/7.1.1-video-file-correction">
62
prepare an original file for pseudo-streaming</link> in advance,
63
rather than having nginx do this on every such request.
64
</para>
65
66
<para>
67
The module also supports the <literal>end</literal> argument of an HTTP request
68
(1.5.13) which sets the end point of playback.
69
The <literal>end</literal> argument can be specified with the
70
<literal>start</literal> argument
71
or separately:
72
<example>
73
http://example.com/elephants_dream.mp4?start=238.88&amp;end=555.55
74
</example>
75
</para>
76
77
<para>
78
For a matching request with a non-zero
79
<literal>start</literal> or <literal>end</literal>
80
argument, nginx will read the metadata from the file, prepare the
81
stream with the requested time range, and send it to the client.
82
This has the same overhead as described above.
83
</para>
84
85
<para id="keyframe">
86
If the <literal>start</literal> argument points to
87
a non-key video frame,
88
the beginning of such video will be broken.
89
To fix this issue, the video
90
<link id="mp4_start_key_frame">can</link> be prepended with
91
the key frame before <literal>start</literal> point
92
and with all intermediate frames between them.
93
These frames will be hidden from playback
94
using an edit list (1.21.4).
95
</para>
96
97
<para>
98
If a matching request does not include the
99
<literal>start</literal> and <literal>end</literal>
100
arguments, there is no overhead, and the file is sent simply as a static
101
resource.
102
Some players also support byte-range requests, and thus do not require
103
this module.
104
</para>
105
106
<para>
107
This module is not built by default, it should be enabled with the
108
<literal>--with-http_mp4_module</literal>
109
configuration parameter.
110
<note>
111
If a third-party mp4 module was previously used, it should be disabled.
112
</note>
113
</para>
114
115
<para>
116
A similar pseudo-streaming support for FLV files is provided by the
117
<link doc="ngx_http_flv_module.xml">ngx_http_flv_module</link> module.
118
</para>
119
120
</section>
121
122
123
<section id="example" name="Example Configuration">
124
125
<para>
126
<example>
127
location /video/ {
128
mp4;
129
mp4_buffer_size 1m;
130
mp4_max_buffer_size 5m;
131
mp4_limit_rate on;
132
mp4_limit_rate_after 30s;
133
}
134
</example>
135
</para>
136
137
</section>
138
139
140
<section id="directives" name="Directives">
141
142
<directive name="mp4">
143
<syntax/>
144
<default/>
145
<context>location</context>
146
147
<para>
148
Turns on module processing in a surrounding location.
149
</para>
150
151
</directive>
152
153
154
<directive name="mp4_buffer_size">
155
<syntax><value>size</value></syntax>
156
<default>512K</default>
157
<context>http</context>
158
<context>server</context>
159
<context>location</context>
160
161
<para>
162
Sets the initial <value>size</value> of the buffer used for
163
processing MP4 files.
164
</para>
165
166
</directive>
167
168
169
<directive name="mp4_max_buffer_size">
170
<syntax><value>size</value></syntax>
171
<default>10M</default>
172
<context>http</context>
173
<context>server</context>
174
<context>location</context>
175
176
<para>
177
During metadata processing, a larger buffer may become necessary.
178
Its size cannot exceed the specified <value>size</value>,
179
or else nginx will return the
180
<http-status code="500" text="Internal Server Error"/> server error,
181
and log the following message:
182
<example>
183
"/some/movie/file.mp4" mp4 moov atom is too large:
184
12583268, you may want to increase mp4_max_buffer_size
185
</example>
186
</para>
187
188
</directive>
189
190
191
<directive name="mp4_limit_rate">
192
<syntax>
193
<literal>on</literal> |
194
<literal>off</literal> |
195
<value>factor</value></syntax>
196
<default>off</default>
197
<context>http</context>
198
<context>server</context>
199
<context>location</context>
200
201
<para>
202
Limits the rate of response transmission to a client.
203
The rate is limited based on the average bitrate of the
204
MP4 file served.
205
To calculate the rate, the bitrate is multiplied by the specified
206
<value>factor</value>.
207
The special value “<literal>on</literal>” corresponds to the factor of 1.1.
208
The special value “<literal>off</literal>” disables rate limiting.
209
The limit is set per a request, and so if a client simultaneously opens
210
two connections, the overall rate will be twice as much
211
as the specified limit.
212
</para>
213
214
<para>
215
<note>
216
This directive is available as part of our
217
<commercial_version>commercial subscription</commercial_version>.
218
</note>
219
</para>
220
221
</directive>
222
223
224
<directive name="mp4_limit_rate_after">
225
<syntax><value>time</value></syntax>
226
<default>60s</default>
227
<context>http</context>
228
<context>server</context>
229
<context>location</context>
230
231
<para>
232
Sets the initial amount of media data (measured in playback time)
233
after which the further transmission of the response to a client
234
will be rate limited.
235
</para>
236
237
<para>
238
<note>
239
This directive is available as part of our
240
<commercial_version>commercial subscription</commercial_version>.
241
</note>
242
</para>
243
244
</directive>
245
246
247
<directive name="mp4_start_key_frame">
248
<syntax><literal>on</literal> | <literal>off</literal></syntax>
249
<default>off</default>
250
<context>http</context>
251
<context>server</context>
252
<context>location</context>
253
<appeared-in>1.21.4</appeared-in>
254
255
<para>
256
Forces output video to always start with a key video frame.
257
If the <literal>start</literal> argument does not point to a key frame,
258
initial frames are hidden using an mp4 edit list.
259
Edit lists are supported by major players and browsers such as
260
Chrome, Safari, QuickTime and ffmpeg,
261
partially supported by Firefox.
262
</para>
263
264
</directive>
265
266
</section>
267
268
</module>
269
270