view xml/en/docs/http/ngx_http_mp4_module.xml @ 3043:9eadb98ec770

Free nginx: removed commercial version documentation.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 14 Feb 2024 20:05:49 +0300
parents 17ed19394953
children
line wrap: on
line source

<?xml version="1.0"?>

<!--
  Copyright (C) Nginx, Inc.
  -->

<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">

<module name="Module ngx_http_mp4_module"
        link="/en/docs/http/ngx_http_mp4_module.html"
        lang="en"
        rev="9">

<section id="summary">

<para>
The <literal>ngx_http_mp4_module</literal> module provides pseudo-streaming
server-side support for MP4 files.
Such files typically have the <path>.mp4</path>, <path>.m4v</path>,
or <path>.m4a</path> filename extensions.
</para>

<para>
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
<literal>start</literal>
and specified in seconds), and the server responds with the stream
such that its start position corresponds to the requested time,
for example:
<example>
http://example.com/elephants_dream.mp4?start=238.88
</example>
This allows performing a random seeking at any time, or starting playback
in the middle of the timeline.
</para>

<para>
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.
</para>

<para>
To start playback, the player first needs to read metadata.
This is done by sending a special request with the
<literal>start=0</literal> 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 I/O overhead,
so it is a good idea to
<link
url="https://github.com/flowplayer/flowplayer/wiki/7.1.1-video-file-correction">
prepare an original file for pseudo-streaming</link> in advance,
rather than having nginx do this on every such request.
</para>

<para>
The module also supports the <literal>end</literal> argument of an HTTP request
(1.5.13) which sets the end point of playback.
The <literal>end</literal> argument can be specified with the
<literal>start</literal> argument
or separately:
<example>
http://example.com/elephants_dream.mp4?start=238.88&amp;end=555.55
</example>
</para>

<para>
For a matching request with a non-zero
<literal>start</literal> or <literal>end</literal>
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.
</para>

<para id="keyframe">
If the <literal>start</literal> argument points to
a non-key video frame,
the beginning of such video will be broken.
To fix this issue, the video
<link id="mp4_start_key_frame">can</link> be prepended with
the key frame before <literal>start</literal> point
and with all intermediate frames between them.
These frames will be hidden from playback
using an edit list (1.21.4).
</para>

<para>
If a matching request does not include the
<literal>start</literal> and <literal>end</literal>
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.
</para>

<para>
This module is not built by default, it should be enabled with the
<literal>--with-http_mp4_module</literal>
configuration parameter.
<note>
If a third-party mp4 module was previously used, it should be disabled.
</note>
</para>

<para>
A similar pseudo-streaming support for FLV files is provided by the
<link doc="ngx_http_flv_module.xml">ngx_http_flv_module</link> module.
</para>

</section>


<section id="example" name="Example Configuration">

<para>
<example>
location /video/ {
    mp4;
    mp4_buffer_size       1m;
    mp4_max_buffer_size   5m;
    mp4_limit_rate        on;
    mp4_limit_rate_after  30s;
}
</example>
</para>

</section>


<section id="directives" name="Directives">

<directive name="mp4">
<syntax/>
<default/>
<context>location</context>

<para>
Turns on module processing in a surrounding location.
</para>

</directive>


<directive name="mp4_buffer_size">
<syntax><value>size</value></syntax>
<default>512K</default>
<context>http</context>
<context>server</context>
<context>location</context>

<para>
Sets the initial <value>size</value> of the buffer used for
processing MP4 files.
</para>

</directive>


<directive name="mp4_max_buffer_size">
<syntax><value>size</value></syntax>
<default>10M</default>
<context>http</context>
<context>server</context>
<context>location</context>

<para>
During metadata processing, a larger buffer may become necessary.
Its size cannot exceed the specified <value>size</value>,
or else nginx will return the
<http-status code="500" text="Internal Server Error"/> server error,
and log the following message:
<example>
"/some/movie/file.mp4" mp4 moov atom is too large:
12583268, you may want to increase mp4_max_buffer_size
</example>
</para>

</directive>


<directive name="mp4_start_key_frame">
<syntax><literal>on</literal> | <literal>off</literal></syntax>
<default>off</default>
<context>http</context>
<context>server</context>
<context>location</context>
<appeared-in>1.21.4</appeared-in>

<para>
Forces output video to always start with a key video frame.
If the <literal>start</literal> 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.
</para>

</directive>

</section>

</module>