Nginx ngx_http_mp4_module模块基本指令整理

  categories:nginx  author:

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的ngx_http_mp4_module模块做简单介绍,本文具体包括如下指令:

mp4,mp4_buffer_size,mp4_max_buffer_size

ngx_http_mp4_module模块对具有.mp4, .m4v, and .m4a.扩展名的H.264/AAC格式文件提供服务器端的伪流支持。

伪流工作方式同 Flash players方式类似。一个播放器发送在请求的uri查询参数中具有开始时间常数的http请求给服务器, 服务器用一个开始时间点在请求的开始时间处的响应流给发起请求的客户端,例如:

http://example.com/elephants_dream.mp4?start=238.88

这个特性允许在任何一个时间点进行搜索或者在某个时间中间进行播放。

我了支持搜索功能, H.264格式文件存储着成为“moov atom.”的原数据在文件中, 这些原信息是文件索引信息的一部分。

为了播放, 播放器首先需要获取元数据, 这个工作通过发送一个特殊的start=0的参数的请求来完成。许多编码软件在文件结尾插入元数据信息,对于伪流来说是个坏消息,伪流的元数据需要在文件开始被定位到。否则整个文件将不得不被全部下载下来然后才能播放。如果一个文件是一个良好结构的伪流文件(在文件开始部分包括合适的原信息数据), nginx仅仅需要发送文件内容。否则nginx为了准备元数据,要重新开始一个新流读取数据。这涉及到一些CPU,内存,磁盘I/O开销。所以准备伪流的原始文件是一个不错的想法,而不是在每一个这样的要求做这个nginx。

对于一个要求匹配一个非零的开始请求,nginx会从文件中读取元数据,从流请求的起始偏移量进行准备流,并将其发送到客户端。这与上面所描述的相同的开销。

如果匹配的请求不包括开始时间,没有任何开销,这个文件仅仅作为一个静态的资源来处理。一些播放器也支持字节范围的要求,因此不需要在这些模块。

这个模块不是内建的默认模块,它应使用–with-http_mp4_module配置参数启用。

如果一个第三方的MP4模块被使用,它需要被禁用。

一个类似于伪流FLV文件的支持是由模块提供的ngx_http_flv_module。

配置示例如下:

location /video/ {

        mp4;

        mp4_buffer_size     1m;

        mp4_max_buffer_size 5m;

    }

Nginx原文:

The module ngx_http_mp4_module provides pseudo-streaming server-side support for H.264/AAC files typically having filename extensions .mp4, .m4v, and .m4a.

Pseudo-streaming works in alliance with conforming Flash players. A player sends an HTTP request to the server with a start time argument in the request URI’s query string (named simply start and specified in seconds), and the server responds with a stream so that its start position corresponds to the requested time, for example:

http://example.com/elephants_dream.mp4?start=238.88

This allows for a random seeking at any time, or starting playback in the middle of a timeline.

To support seeking, H.264-based formats store the metadata in the so-called “moov atom.” It is a part of the file that holds the index information for the whole file.

To start playback, a player first needs to read metadata. This is done by sending a special request with the start=0 argument. Many encoding software will insert the metadata at the end of the file. This is bad for pseudo-streaming: the metadata needs to be located at the beginning of the file, or else the entire file will have to be downloaded before it starts playing. If a file is well-formed (with metadata at the beginning of a file), nginx just sends back the contents of a file. Otherwise, it has to read the file and prepare a new stream so that metadata comes before media data. This involves some CPU, memory, and disk I/O overhead, so it is a good idea to prepare an original file for pseudo-streaming, rather than having nginx do this on every such request.

For a matching request with a non-zero start argument, nginx will read metadata from the file, prepare the stream starting from the requested offset, and send it to a client. This has the same overhead as described above.

If a matching request does not include the start argument, there is no overhead, and the file is just sent as a static resource. Some players also support byte-range requests, and thus do not require this module at all.

This module is not built by default, it should be enabled with the –with-http_mp4_module configuration parameter.

If a third-party mp4 module was previously used, it needs to be disabled.

A similar pseudo-streaming support for FLV files is provided by the module ngx_http_flv_module.

Example Configuration

    location /video/ {

        mp4;

        mp4_buffer_size     1m;

        mp4_max_buffer_size 5m;

    }

1. mp4

syntax:

mp4;

default:

context:

location

在配置这个指令的location中打开相应功能

Nginx原文:

Turns on module processing in a surrounding location.

2. mp4_buffer_size

syntax:

mp4_buffer_size   size;

default:

mp4_buffer_size 512K;

context:

http, server, location

设置一个用于处理MP4文件内存缓冲区的初始大小。

Nginx原文:

Sets the initial size of a memory buffer used to process MP4 files.

3. mp4_max_buffer_size

syntax:

mp4_max_buffer_size         size;

default:

mp4_max_buffer_size 10M;

context:

http, server, location

在元数据处理期间,一个更大的buffer可能被需要。这个更大的缓存的尺寸不能被超过,否则nginx将返回500的错误码(服务器内部错误), 相关日志将向向下面情况

  "/some/movie/file.mp4" mp4 moov atom is too large:

    12583268, you may want to increase mp4_max_buffer_size

me, a connection is closed.

Nginx原文:

During metadata processing, a larger buffer may become necessary. Its size cannot exceed the specified size, or else nginx will return the server error 500 (Internal Server Error), and log the following:

    "/some/movie/file.mp4" mp4 moov atom is too large:

    12583268, you may want to increase mp4_max_buffer_size

me, a connection is closed.



快乐成长 每天进步一点点      京ICP备18032580号-1