1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
=encoding utf-8
=head1 NAME
ngx_http_mp4_module - Module ngx_http_mp4_module
=head1
The C<ngx_http_mp4_module> module provides pseudo-streaming
server-side support for MP4 files.
Such files typically have the F<.mp4>, F<.m4v>,
or F<.m4a> filename extensions.
Pseudo-streaming works in alliance with a compatible media player.
The player sends an HTTP request to the server with the start time
specified in the query string argument (named simply
C<start>
and specified in seconds), and the server responds with the stream
such that its start position corresponds to the requested time,
for example:
http://example.com/elephants_dream.mp4?start=238.88
This allows performing a random seeking at any time, or starting playback
in the middle of the timeline.
To support seeking, H.264-based formats store metadata
in a so-called “moov atom”.
It is a part of the file that holds the index information for the
whole file.
To start playback, the player first needs to read metadata.
This is done by sending a special request with the
C<start=0> argument.
A lot of encoding software insert the metadata at
the end of the file.
This is suboptimal for pseudo-streaming, because the player
has to download the entire file before starting playback.
If the metadata are located at the beginning of the file,
it is enough for nginx to simply start sending back the file contents.
If the metadata are located at the end of the file,
nginx must read the entire file and prepare a new stream so that
the metadata come before the media data.
This involves some CPU, memory, and disk IE<sol>O overhead,
so it is a good idea to
L<prepare an original file for pseudo-streaming|https://github.com/flowplayer/flowplayer/wiki/7.1.1-video-file-correction> in advance,
rather than having nginx do this on every such request.
The module also supports the C<end> argument of an HTTP request
(1.5.13) which sets the end point of playback.
The C<end> argument can be specified with the
C<start> argument
or separately:
http://example.com/elephants_dream.mp4?start=238.88&end=555.55
For a matching request with a non-zero
C<start> or C<end>
argument, nginx will read the metadata from the file, prepare the
stream with the requested time range, and send it to the client.
This has the same overhead as described above.
If the C<start> argument points to
a non-key video frame,
the beginning of such video will be broken.
To fix this issue, the video
can be prepended with
the key frame before C<start> point
and with all intermediate frames between them.
These frames will be hidden from playback
using an edit list (1.21.4).
If a matching request does not include the
C<start> and C<end>
arguments, there is no overhead, and the file is sent simply as a static
resource.
Some players also support byte-range requests, and thus do not require
this module.
This module is not built by default, it should be enabled with the
C<--with-http_mp4_module>
configuration parameter.
B<NOTE>
If a third-party mp4 module was previously used, it should be disabled.
A similar pseudo-streaming support for FLV files is provided by the
L<ngx_http_flv_module|ngx_http_flv_module> module.
=head1 Example Configuration
location /video/ {
mp4;
mp4_buffer_size 1m;
mp4_max_buffer_size 5m;
mp4_limit_rate on;
mp4_limit_rate_after 30s;
}
=head1 Directives
=head2 mp4
B<context:> I<location>
Turns on module processing in a surrounding location.
=head2 mp4_buffer_size
B<syntax:> mp4_buffer_size I<I<C<size>>>
B<default:> I<512K>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
Sets the initial I<C<size>> of the buffer used for
processing MP4 files.
=head2 mp4_max_buffer_size
B<syntax:> mp4_max_buffer_size I<I<C<size>>>
B<default:> I<10M>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
During metadata processing, a larger buffer may become necessary.
Its size cannot exceed the specified I<C<size>>,
or else nginx will return the
C<500> (C<Internal Server Error>) server error,
and log the following message:
"/some/movie/file.mp4" mp4 moov atom is too large:
12583268, you may want to increase mp4_max_buffer_size
=head2 mp4_limit_rate
B<syntax:> mp4_limit_rate I<
C<on> E<verbar>
C<off> E<verbar>
I<C<factor>>>
B<default:> I<off>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
Limits the rate of response transmission to a client.
The rate is limited based on the average bitrate of the
MP4 file served.
To calculate the rate, the bitrate is multiplied by the specified
I<C<factor>>.
The special value “C<on>” corresponds to the factor of 1.1.
The special value “C<off>” disables rate limiting.
The limit is set per a request, and so if a client simultaneously opens
two connections, the overall rate will be twice as much
as the specified limit.
B<NOTE>
This directive is available as part of our
commercial subscription.
=head2 mp4_limit_rate_after
B<syntax:> mp4_limit_rate_after I<I<C<time>>>
B<default:> I<60s>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
Sets the initial amount of media data (measured in playback time)
after which the further transmission of the response to a client
will be rate limited.
B<NOTE>
This directive is available as part of our
commercial subscription.
=head2 mp4_start_key_frame
B<syntax:> mp4_start_key_frame I<C<on> E<verbar> C<off>>
B<default:> I<off>
B<context:> I<http>
B<context:> I<server>
B<context:> I<location>
This directive appeared in version 1.21.4.
Forces output video to always start with a key video frame.
If the C<start> argument does not point to a key frame,
initial frames are hidden using an mp4 edit list.
Edit lists are supported by major players and browsers such as
Chrome, Safari, QuickTime and ffmpeg,
partially supported by Firefox.
|