zoukankan      html  css  js  c++  java
  • 视频直播点播nginx-rtmp开发手册中文版

    2016年8月18日12:42:35 参照官方文档https://github.com/arut/nginx-rtmp-module/wiki/Directives

    请注意这个是粗翻译版,仅供参考,不是精校版 精校版后面才会更新

     目录

    • 核心
      • rtmp
      • server
      • listen
      • application
      • timeout
      • ping
      • ping_timeout
      • max_streams
      • ack_window
      • chunk_size
      • max_queue
      • max_message
      • buflen
      • out_queue
      • out_cork
    • 访问权限
      • allow
      • deny
    • Exec 一簇函数(进程往往要调用一种exec函数以执行另一个程序)
    • exec_push
    • exec_pull
    • exec
    • exec_options
    • exec_static
    • exec_kill_signal
    • respawn
    • respawn_timeout
    • exec_publish
    • exec_play
    • exec_play_done
    • exec_publish_done
    • exec_record_done
    • 直播
      • live
      • meta
      • interleave
      • wait_key
      • wait_video
      • publish_notify
      • drop_idle_publisher
      • sync
      • play_restart
      • idle_streams
    • 录像
      • record
      • record_path
      • record_suffix
      • record_unique
      • record_append
      • record_lock
      • record_max_size
      • record_max_frames
      • record_interval
      • recorder
      • record_notify
    • 视频点播
      • play
      • play_temp_path
      • play_local_path
    • 中继
      • pull
      • push
      • push_reconnect
      • session_relay
    • 通知
      • on_connect
      • on_play
      • on_publish
      • on_done
      • on_play_done
      • on_publish_done
      • on_record_done
      • on_update
      • notify_update_timeout
      • notify_update_strict
      • notify_relay_redirect
      • notify_method
    • HLS协议
      • hls
      • hls_path
      • hls_fragment
      • hls_playlist_length
      • hls_sync
      • hls_continuous
      • hls_nested
      • hls_base_url
      • hls_cleanup
      • hls_fragment_naming
      • hls_fragment_naming_granularity
      • hls_fragment_slicing
      • hls_variant
      • hls_type
      • hls_keys
      • hls_key_path
      • hls_key_url
      • hls_fragments_per_key
    • MPEG-DASH(HTTP动态自适应流媒体)
      • dash
      • dash_path
      • dash_fragment
      • dash_playlist_length
      • dash_nested
      • dash_cleanup
    • 访问日志
      • access_log
      • log_format
    • Limits 限制
      • max_connections
    • 数据统计
      • rtmp_stat
      • rtmp_stat_stylesheet
    • Multi-worker live streaming  多线程直播流
      • rtmp_auto_push
      • rtmp_auto_push_reconnect
      • rtmp_socket_dir
    • Control  控制模块
      • rtmp_control

    上下文可以理解为,默认配置文件或者参数,如果没有就会报错,因为每个函数都需一些定义好的参数和传入函数的参数,这个有些可以少,但是有些是必须的

    核心

    语法: rtmp { ... }
    上下文: root

     声明RTMP服务器实​​例

    rtmp {
      server {
      }
    }

    语法: listen (addr[:port]|port|unix:path) [bind] [ipv6only=on|off] [so_keepalive=on|off|keepidle:keepintvl:keepcnt|proxy_protocol]
    上下文: server

    添加监听套接字NGINX接受RTMP连接

    server {
    listen 1935;
    }

    application

    语法: application name { ... }
    上下文: server

    创建RTMP应用。不同于HTTP位置应用程序的名称不能是一个模式。

    server {
        listen 1935;
        application myapp {
        }
    }

    timeout 超时

    语法:超时值
    上下文:RTMP,服务器

     套接字超时。该值主要用于写入。大多数时间RTMP模块预计不会对除发行插座所有套接字的任何活动。如果你想断插座得到迅速断开连接采用主动式工具,如保活或RTMP平。默认为1分钟。

     timeout 60s;

    ping

    语法: ping value
    上下文: rtmp, server

     RTMP ping间隔。零变为平掉。 RTMP平是主动连接检查的协议功能。一个特殊的数据包被发送到远程对等体和答复有望与ping_timeout指令指定的超时时间内。

    如果ping回复未在此时间内收到然后关闭连接。平安默认值为1分钟。默认Ping超时是30秒。

    ping 3m;
    ping_timeout 30s;
    ping_timeout

    ping_timeout

    语法: ping_timeout value
    上下文: rtmp, server

    见上面ping的描述

     max_streams 单一流数据最大限制

    语法: max_streams value
    上下文: rtmp, server

    设置RTMP流的最大数目。数据流被复用成一个单一的数据流。不同信道用于发送命令,音频,视频等的默认值是32,它通常是确定为许多情况。

    max_streams 32;

    ack_window 

    语法: ack_window value
    上下文: rtmp, server

     设置RTMP确认窗口的大小。它接收到的字节之后对等体应发送确认包到远程侧的次数。默认值是5000000。

    ack_window 5000000;

    chunk_size 数据块大小

    语法: chunk_size value
    上下文: rtmp, server

    最大的块的大小为最大流复数。默认值是4096越大该值越低CPU开销。这个值不能小于128。

    chunk_size 4096;

    max_queue 最大队列数

    语法: max_queue value
    上下文: rtmp, server

    照上面的语法习惯是这样,官方平没有给出文档,应该是设置最大连接数的参数,具体想知道这个是做什么的请参看源代码

    max_message

    语法: max_queue value
    上下文: rtmp, server

    输入数据消息的最大大小。所有输入数据来源(成块,并进一步)分成的消息。的部分信息保存在内存中,而等待它完成。在理论上传入消息可以是非常大的,可以是对于服务器的稳定性的问题。默认值是1M足够许多情况下。

    max_message 1M;

    buflen 缓冲区

    语法: buflen time
    上下文: rtmp, server

    设置默认的缓冲区长度。通常客户端发送播放前RTMP set_buflen命令并重置该设置。默认值是1000毫秒。

    buflen 5S;

    out_queue

    目前没有文档

    out_cork

    目前没有文档

    Access 访问控制

    allow

    语法: allow [play|publish] address|subnet|all
    上下文: rtmp, server, application

    允许发布/从指定的地址或所有地址播放。允许/拒绝指令出现的顺序检查。

    allow publish 127.0.0.1;
    deny publish all;
    allow play 192.168.0.0/24;
    deny play all;



    deny

    语法: deny [play|publish] address|subnet|all
    上下文: rtmp, server, application

    参见allow说明

    Exec

    exec_push

     


    语法: exec_push command arg*
    上下文: rtmp, server, application

    指定将公布每一个流执行带参数的外部命令。当发布停止处理结束。二进制完整路径应指定为第一个参数。没有这个过程应该做的假设。然而,这功能与ffmpeg的对流转换非常有用。

    FFmpeg的应该连接到nginx的,RTMP作为客户端和输出转换流回到nginx的,RTMP作为发行人。形式是$var / ${VAR}的可替换命令行中使用

    $name - stream name
    $app - application name
    $addr - client address
    $flashver - client flash version
    $swfurl - client swf url
    $tcurl - client tc url
    $pageurl - client page url

    shell风格重定向可以exec_push指令指定写入输出和接收输入。支持的是

    truncating output >file
    appending output >>file
    descriptor redirects like 1>&2
    input <file

    确保有重定向字符和流名称/编号之间没有空格。

    您可以指定要执行的命令或短命令名的完整路径。在后一种情况下,二进制文件由PATH环境变量指定的目录中查找。默认情况下nginx的清除环境通常会使RTMP模块只运行位于标准目录如/ bin和/ USR / bin中的二进制文件。

    为了使这个总是工作,请保持原来的PATH变量值具有以下nginx的指令。

     env PATH; 

    下面的ffmpeg的通话呼入转码流HLS就绪流(H264 / AAC)。 FFmpeg的应libx264&libfaac支持被编译在这个例子中工作。

    application src {
        live on;
        exec_push ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -s 300x200 -acodec libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/hls/$name 2>>/var/log/ffmpeg-$name.log;
    }
    
    application hls {
        live on;
        hls on;
        hls_path /tmp/hls;
        hls_fragment 15s;
    }

    exec_pull

    语法: exec_pull command arg*
    上下文: rtmp, server, application

    指定与要在打事件中执行参数外部命令。当第一个客户端连接到该流,当最后一个断开被杀害时,执行命令。该指令使得可以在本地客户端的任何格式拉远程流。

    该功能仅在单进程模式下可靠地工作。这样做的原因是,我们不能确保外部进程始终连接到合适的工人。它显然连接到一个随机的。虽然这仍然会在大多数情况下这不是一个建议的体系结构,这将是不稳定和可能出现bug。

    指令参数是相同exec_push

    application myapp {
        live on;
        exec_pull ffmpeg -i http://example.com/video_$name.ts -c copy -f flv rtmp://localhost/$app/$name;
    }

    在上述配置exec_pull指令提供所有数据流。这导致远程流名称格式一定的局限性。它应该能够利用现有的变量,比如$应用程序构建远程URL,$app等。

    当这是不可能的,你可以添加指令exec_options允许设置在exec-family指令附加流选项。现在支持的唯一的选择是名称选项。

    application myapp {
        live on;
        exec_options on;
        exec_pull ffmpeg -i http://example.com/tv1.ts -c copy -f flv rtmp://localhost/$app/$name name=mystream;
        exec_pull ffmpeg -i http://another.example.com/video_plus.ts -c copy -f flv rtmp://localhost/$app/$name name=anotherstream;
    }

     exec

    语法: exec command arg*
    上下文: rtmp, server, application

    EXEC是exec_push的别名

    exec_options

    语法: exec_options on|off
    上下文: rtmp, server, application

    该指令切换Exec选模式。当被激活时,你可以添加一个exec家族指令选项。唯一支持的EXEC选项名称。通过该选项,可以应用EXEC只有指定的流。默认为关闭状态。

    exec_options on;
    # 调用on_publish只为“mystream”
    exec_publish http://localhost/on_publish name=mystream;

    #调用on_play只为“另一个”
    exec_play http://localhost/on_play name=another;

    #执行不同的ffmpeg对不同的流
    exec_pull http://example.com/abc.ts -c copy -f flv rtmp://localhost/$name/$app name=mystream;
    exec_pull http://my.example.com/tele.ts -c copy -f flv rtmp://localhost/$name/$app name=tv;
    exec_pull http://enother.example.com/hello/f.ts -c copy -f flv rtmp://localhost/$name/$app name=fun;

    exec_static

    语法: exec_static command arg*
    上下文: rtmp, server, application

     类似给exec,但在运行nginx的启动指定的命令。不支持替换,因为没有session 环境。

    exec_static ffmpeg -i http://example.com/video.ts -c copy -f flv rtmp://localhost/myapp/mystream;

    exec_kill_signal

    语法: exec_kill_signal signal
    上下文: rtmp, server, application

    集进程终止的信号。默认值是杀(SIGKILL)。您可以指定数字或符号的名称(POSIX.1-1990信号)
    exec_kill_signal term;
    exec_kill_signal usr1;
    exec_kill_signal 3;

    respawn 

    语法: respawn on|off
    上下文: rtmp, server, application

    如果重生子进程打开的时候同时发布仍然在它的终止。默认为上;

     respawn off;

    respawn_timeout

    语法: respawn_timeout timeout
    上下文: rtmp, server, application

     套重生超时开始新的子实例之前等待。默认值是5秒。

    respawn_timeout 10s;

    exec_publish

    语法: exec_publish command arg*
    上下文: rtmp, server, application

    指定与参数外部命令要在发布事件执行。返回代码不进行分析。 EXEC的替换是同时支持在这里。此外ARGS变量赞成举行查询字符串参数。

    exec_play

    语法: exec_play command arg*
    上下文: rtmp, server, application

    指定与要在打事件中执行参数外部命令。返回代码不进行分析。替代列表和exec_publish相同

    exec_play_done

    语法: exec_play_done command arg*
    上下文: rtmp, server, application

    指定与参数外部命令要在play_done事件执行。返回代码不进行分析. 替代列表和exec_publish相同

    exec_publish_done

    语法: exec_publish_done command arg*
    上下文: rtmp, server, application

    指定与参数外部命令要在publish_done事件执行。返回代码不进行分析. 替代列表和exec_publish相同

    exec_record_done

    语法: exec_record_done command arg*
    上下文: rtmp, server, application, recorder

    指定录音结束时要执行带有参数的外部命令。 exec_publish的取代在这里支持以及附加变量

    • recorder - recorder name
    • path - recorded file path (/tmp/rec/mystream-1389499351.flv)
    • filename - path with directory omitted (mystream-1389499351.flv)
    • basename - file name with extension omitted (mystream-1389499351)
    • dirname - directory path (/tmp/rec)

    例子

    # 跟踪客户信息
    exec_play bash -c "echo $addr $pageurl >> /tmp/clients";
    exec_publish bash -c "echo $addr $flashver >> /tmp/publishers";
    
    # 转换录制的文件格式的MP4
    exec_record_done ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4;

    Live

    live

    语法: live on|off
    上下文: rtmp, server, application

    切换现场模式,即一对多的广播。

    live on;
    

    meta

    语法: meta on|copy|off
    上下文: rtmp, server, application

    集元数据发送模式。上的价值,使用户收到一封包含像宽度,高度等的副本设定值,使客户得到出版商的元数据块的精确副本,包括标准和具体领域的预定义字段重建的元数据包。关的值将关闭发送任何RTMP元数据给用户。默认为on。

    meta copy;
    

    interleave

    语法: interleave on|off
    上下文: rtmp, server, application

    切换交错模式。在这种模式下的音频和视频数据上相同的RTMP块流传输。默认为off。

    interleave on;
    

    wait_key

    语法: wait_key on|off
    上下文: rtmp, server, application

    使视频流开始一个关键帧。默认为关闭。

    wait_key on;
    

    wait_video

    语法: wait_video on|off
    上下文: rtmp, server, application

    直到第一个视频帧发送禁用音频。默认为关闭。可以用wait_key决定相结合,使客户端接收视频关键帧但其后所有其他数据。然而,这通常会增加连接延迟。你可以在你的编码器,以减少延​​迟调整关键帧间隔

    IE浏览器的最新版本需要这个选项正常播放启用。

    wait_video on;
    

    publish_notify

    语法: publish_notify on|off
    上下文: rtmp, server, application

    发送NetStream.Play.PublishNotify和NetStream.Play.UnpublishNotify给用户。默认为关闭。

    publish_notify on;
    

    drop_idle_publisher

    语法: drop_idle_publisher timeout
    上下文: rtmp, server, application

    丢弃在指定时间内发行者连接这一直空闲(无音频/视频数据)。默认是关闭的。注意:这只能当连接处于发布模式(发送发布命令后)。

    drop_idle_publisher 10s;
    

    sync

    语法: sync timeout
    上下文: rtmp, server, application

    同步的音频和视频流。如果订户带宽不够以接收发布速率数据,某些帧被服务器丢弃。这将导致同步问题。当时间戳差值超过指定为同步参数的值绝对帧发送固定的。默认为300ms。

    sync 10ms;
    

    play_restart

    语法: play_restart on|off
    上下文: rtmp, server, application

    如果启用nginx的,RTMP发送NetStream.Play.Start和NetStream.Play.Stop到每个用户每次发行启动或停止发布。如果禁用每个用户只在开始和重放结束接收这些通知。默认是off。

    play_restart off;
    

    idle_streams

    语法: idle_streams on|off
    上下文: rtmp, server, application

    如果禁用nginx的,RTMP防止连接到空闲/不存在的实时流用户,断开所有用户的数据流时,断开publisher。默认为on。

    idle_streams off;

    record

    语法: record [off|all|audio|video|keyframes|manual]*
    上下文: rtmp, server, application, recorder

    切换录音模式。流可以被记录在FLV文件。该指令指定正是应记录哪些:

    • off - 不记录在所有
    • all - 音频和视频(一切)
    • audio -音频
    • video -  视频
    • keyframes -唯一的关键帧视频
    • manual - 从来没有自动启动记录仪,使用控制界面来启动/停止

    可以有键,在一个记录指令任何相容组合。

    record all;
    
    record audio keyframes;
    

    record_path

    语法: record_path path
    上下文: rtmp, server, application, recorder

    指定将录制的FLV文件来记录路径。

    record_path /tmp/rec;
    

    record_suffix

    语法: record_suffix value
    上下文: rtmp, server, application, recorder

    设置记录文件的后缀。默认为'。FLV“。

    record_suffix _recorded.flv;
    

    记录后缀可以是的strftime格式的模式。下面的指令

    record_suffix -%d-%b-%y-%T.flv;
    

    将产生的形式mystream-24-Apr-13-18:23:38.flv 所有支持的strftime格式选项可以的strftime手册页上找到。

    record_unique

    语法: record_unique on|off
    上下文: rtmp, server, application, recorder

    如果打开追加当前的时间戳,以录制的文件。否则,同样的文件,每次新的记录发生重写。默认是关闭的

    record_unique on;
    

    record_append

    语法: record_append on|off
    上下文: rtmp, server, application, recorder

    切换文件追加模式。当录音机打开追加新数据旧文件或创建它时,它的缺失。有旧的数据,并在文件中的新数据之间不存在时间间隙。默认是关闭的。

    record_append on;
    

    record_lock

    语法: record_lock on|off
    上下文: rtmp, server, application, recorder

    当打开当前记录的文件被锁住的fcntl调用。这可以从其他地方进行检查,以找出正在录制的文件。默认是关闭的。

    record_lock on;

    在FreeBSD上可以使用的羊群工具来检查。在Linux上的flock 和fcntl是无关的,所以你只剩下编写一个简单的脚本文件检查锁定状态。下面是这种脚本isunlocked.py的一个例子。

    #!/usr/bin/python
    
    import fcntl, sys
    
    sys.stderr.close()
    fcntl.lockf(open(sys.argv[1], "a"), fcntl.LOCK_EX|fcntl.LOCK_NB)
    

    record_max_size

    语法: record_max_size size
    上下文: rtmp, server, application, recorder

    设置最大记录文件大小

    record_max_size 128K;
    

    record_max_frames

    语法: record_max_frames nframes
    上下文: rtmp, server, application, recorder

    设置每个录像文件的视频帧的最大数量。

    record_max_frames 2;
    

    record_interval

    语法: record_interval time
    上下文: rtmp, server, application, recorder

    重新启动该数字(毫)秒后拍摄。默认关闭。零表示记录之间没有延迟。如果record_unique是关闭的,那么所有的记录片段被写入到同一个文件。否则,时间戳就会追加这使得文件不同(因为record_interval大于1秒以上)。

    record_interval 1s;
    
    record_interval 15m;
    

    recorder

    语法: recorder name {...}
    上下文: application

    创建记录块。多个刻录机可以withing单一的应用程序来创建。所有上述记录相关指令可以在记录{}块中指定。所有的设置是从更高层次继承。

    application {
        live on;
    
        # default recorder
        record all;
        record_path /var/rec;
    
        recorder audio {
            record audio;
            record_suffix .audio.flv;
        }
    
        recorder chunked {
            record all;
            record_interval 15s;
            record_path /var/rec/chunked;
        }
    }
    

    record_notify

    语法: record_notify on|off
    上下文: rtmp, server, application, recorder

    切换发送NetStream.Record.Start和NetStream.Record.Stop状态信息(的onStatus),以发行时的具体记录开始或停止录音文件。状态描述字段保存记录的域名(空默认记录器)。默认关闭。

    recorder myrec {
        record all manual;
        record_path /var/rec;
        record_notify on;
    }
    

     

     Video on demand 视频点播

    play

    语法: play dir|http://loc [dir|http://loc]*
    上下文: rtmp, server, application


    从指定目录或HTTP位置播放FLV或MP4文件。如果参数前缀以http://那么假定文件应该从远程HTTP位置播放前下载。注意打不启动,直到整个文件下载。您可以使用nginx的本地缓存本地计算机上的文件。

    多个播放位置可以在一个单一的播放指令指定。当多个播放指令指定的位置列被合并,并从更高的作用域继承。发挥每个位置试图直到成功定位被发现。如果没有找到这样的位置错误状态被发送到客户端

    索引FLV是为了随机搜索能力。未编入索引FLV是玩谋求/暂停禁用(仅重启动模式)。使用的FLV索引(例如,yamdi)索引。

    如果您播放录制的记录指令FLV文件,请不要打前忘记他们的索引。在创建未编入索引。

    如果视频和音频编解码器是由RTMP支持的mp4文件只能播放。最常见的情况是H264 / AAC。

    application vod {
        play /var/flvs;
    }
    
    application vod_http {
        play http://myserver.com/vod;
    }
    
    application vod_mirror {
        # try local location first, then access remote location
        play /var/local_mirror http://myserver.com/vod;
    }
    

    播放 /var/flvs/dir/file.flv:

    ffplay rtmp://localhost/vod//dir/file.flv
    

    VOD后的两条斜线使ffplay使用VOD和应用程序名称和URL作为playpath的其余部分。

    play_temp_path

    语法: play_temp_path dir
    上下文: rtmp, server, application

    设置将在其中远程VOD播文件的播放之前存储位置。默认值是/ tmp;

    play_temp_path /www;
    play http://example.com/videos;
    

    play_local_path

    语法: play_local_path dir
    上下文: rtmp, server, application

    设置从哪里目录play_temp_path复制远程点播文件,他们完全下载后的位置。空值禁用该功能。默认情况下它是空的。该特征可用于在本地缓存远程文件。

    此路径应该是相同的装置,play_temp_path上。

    #  在/tmp/videos上搜索文件
    #  如果没有找到就找远程的location上寻找
    # 而且储存在
    /tmp/videos

    play_local_path /tmp/videos;
    play /tmp/videos http://example.com/videos;


    Relay  中继

    pull

    语法: pull url [key=value]*
    上下文: application

    Creates pull relay. Stream is pulled from remote machine and becomes available locally. It only happens when at least one player is playing the stream locally.

    创建拉流中继 ,流从远程计算机上拉,并且成为本地可用。它仅当至少一个播放流正在玩本地流发生。

    Url syntax: [rtmp://]host[:port][/app[/playpath]]

    如果应用程序丢失,则使用本地应用程序的名称。如果playpath缺少那么当前流的名字来代替。

    以下参数被支持:

    • app - 明确应用程序的名称
    • name - 绑定继电器当地流名;如果为空或不指定,则应用程序中的所有当地的溪流被拉到
    • tcUrl - 如果空的自动构建
    • pageUrl - 伪装网页网址
    • swfUrl - SWF网址假装
    • flashVer - flash版本假装,默认为“LNX.11,1,102,55”
    • playPath - 远程播放路径
    • live - 切换特殊行为直播,值:0,1
    • start - 以秒为单位的开始时间
    • stop -  在几秒钟内停止时间
    • static - 使静态拉流 如拉在nginx的启动创建

    If a value for a parameter contains spaces then you should use quotes around the WHOLEkey=value pair like this : 'pageUrl=FAKE PAGE URL'.

    如果参数的值包含空格,那么你应该使用引号围绕整个key = value对这样的:'pageUrl=FAKE PAGE URL“。

    pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a;
    
    pull rtmp://cdn2.example.com/another/a?b=1&c=d pageUrl=http://www.example.com/video.html swfUrl=http://www.example.com/player.swf live=1;
    
    pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a static;
    

    push

    语法: push url [key=value]*
    上下文: application

    推动具有相同的语法拉动。不像推拉指令发布流远程服务器。

    push_reconnect

    语法: push_reconnect time
    上下文: rtmp, server, application

    重新连接超时之前等待的推后断开连接。默认设置为3秒。

    push_reconnect 1s;
    

    session_relay

    语法: session_relay on|off
    上下文: rtmp, server, application

    切换会话中继模式。在这种模式下中继当连接关闭时被破坏。当设置为关闭时,流关闭,使得其他的中继也可能会被以后创建的继电器被破坏。默认是关闭的。

    session_relay on;
    
    

     

    Notify

    on_connect

    语法: on_connect url
    上下文: rtmp, server

    设置HTTP连接的回调。当客户端连接问题的命​​令HTTP请求是异步发出命令和处理被挂起,直到它返回结果代码。如果返回HTTP 2XX代码,然后RTMP会话继续。

    3XX的代码使RTMP重定向到另一个应用程序的名字是从位置HTTP响应头拍摄。否则,连接被丢弃。

    注意:这个指令不应用范围允许的,因为应用还处于连接阶段不明。

    HTTP请求接收多个参数。 POST方法用于application/x-www-form-urlencoded MIME type ,下面的参数被传递给调用者:

    • call=connect
    • addr - client IP address
    • app - application name
    • flashVer - client flash version
    • swfUrl - client swf url
    • tcUrl - tcUrl
    • pageUrl - client page url

    除了上述项目明确地传递给connect命令的所有参数也与回调发送。您应当区分连接参数从播放/发布参数。玩家通常具有播放/流发布名设置连接字符串分开的一种特殊方式。作为一个例子下面是这些参数是如何在JWPlayer设置

    ...
    streamer: "rtmp://localhost/myapp?connarg1=a&connarg2=b",
    file: "mystream?strarg1=c&strarg2=d",
    ...
    

    Ffplay (with librtmp) example

    ffplay "rtmp://localhost app=myapp?connarg1=a&connarg2=b playpath=mystream?strarg1=c&strarg2=d"
    

    Usage example

    on_connect http://example.com/my_auth;
    

    Redirect example

    location /on_connect {
        if ($arg_flashver != "my_secret_flashver") {
            rewrite ^.*$ fallback? permanent;
        }
        return 200;
    }
    

    on_play

    语法: on_play url
    上下文: rtmp, server, application

    Sets HTTP play callback. Each time a clients issues play command an HTTP request is issued asynchronously and command processing is suspended until it returns result code. HTTP result code is then analyzed.

    • HTTP 2XX代码继续RTMP会议
    • HTTP重定向3XX RTMP另一个流的名字是从位置HTTP响应头拍摄。如果新的流名字开始与rtmp:// 代替创建,继而远程中继会被创建。中继需要IP地址被指定,而不是域名,只用nginx的版本低于1.3.10更大的工作。又见notify_relay_redirect。
    • 否则,RTMP连接断开


    重定向的例如

    http {
        ...
        location /local_redirect {
            rewrite ^.*$ newname? permanent;
        }
        location /remote_redirect {
            # no domain name here, only ip
            rewrite ^.*$ rtmp://192.168.1.123/someapp/somename? permanent;
        }
        ...
    }
    
    rtmp {
        ...
        application myapp1 {
            live on;
            # stream will be redirected to 'newname'
            on_play http://localhost:8080/local_redirect;
        }
        application myapp2 {
            live on;
            # stream will be pulled from remote location
            # requires nginx >= 1.3.10
            on_play http://localhost:8080/remote_redirect;
        }
        ...
    }

    HTTP请求接收多个参数。 POST方法用于application/x-www-form-urlencoded MIME type. 。下面的参数被传递给调用者:

    • call=play
    • addr - client IP address
    • clientid - nginx client id (displayed in log and stat)
    • app - application name
    • flashVer - client flash version
    • swfUrl - client swf url
    • tcUrl - tcUrl
    • pageUrl - client page url
    • name - stream name

    除了上述项目通过明确玩命令的所有参数也与回调发送。

    例如,如果流与URL访问 rtmp://localhost/app/movie?a=100&b=face&foo=bar then ab & foo 也会被发送回来

    on_play http://example.com/my_callback;
    

    on_publish

    语法: on_publish url
    上下文: rtmp, server, application

    上面这个指令设置了发布命令回调,唯一的区别一样on_play。代替远程拉推在这种情况下进行的。

    on_done

    语法: on_done url
    上下文: rtmp, server, application

    设置 play/publish  终止回调。上述所有适用于此。然而HTTP状态代码不检查此回调。

    on_play_done

    语法: on_play_done url
    上下文: rtmp, server, application

    和 on_done 一样的行为 但只作用于 play和 event 

    on_publish_done

    语法: on_publish_done url
    上下文: rtmp, server, application

    相同的行为on_done 但只作用于 publish 和 event 

    on_record_done

    语法: on_record_done url
    上下文: rtmp, server, application, recorder

    设置record_done回调。除了常见的HTTP回调变量它接收到以下值

    • recorder - recorder name in config or empty string for inline recorder 在配置或空字符串内联录音机录像机名
    • path -  录制的文件路径

    Example

    on_record_done http://example.com/recorded;
    

    on_update

    语法: on_update url
    上下文: rtmp, server, application

    Set update callback. This callback is called with period of notify_update_timeout. If a request returns HTTP result other than 2xx connection is terminated. This can be used to synchronize expired sessions. Two additional arguments time and timestamp are passed to this handler:

    设置更新回调。此回调调用notify_update_timeout的时期。如果请求返回比2XX连接其他HTTP结果被终止。这可用于同步过期会话。另外两个参数的 时间和时间戳 传递到该处理程序

    • time  从 play/publish 回调开始的时间
    • timestamp is RTMP timestamp of the last audio/video packet sent to the client 发送到客户端的最后的音频/视频分组的RTMP时间戳

    您可以使用时间戳参数单独限制播放时长为每个用户。

    on_update http://example.com/update;
    

    notify_update_timeout

    语法: notify_update_timeout timeout
    上下文: rtmp, server, application

    集on_update回调之间超时。默认值是30秒。

    notify_update_timeout 10s;
    on_update http://example.com/update;
    

    notify_update_strict

    语法: notify_update_strict on|off
    上下文: rtmp, server, application

    对于切换回调on_update严格模式。默认是关闭的。当所有的连接错误打开,超时以及HTTP分析错误和空的反应被视为更新失败,​​并导致连接终止。当关闭唯一有效的HTTP响应代码等的2XX导致失败。

    notify_update_strict on;
    on_update http://example.com/update;
    

    notify_relay_redirect

    语法: notify_relay_redirect on|off
    上下文: rtmp, server, application

    启用对on_play和on_publish远程重定向本地流重定向。新的流名字是用于远程重定向RTMP网址的MD5哈希值。默认是关闭的。

    notify_relay_redirect on;
    

    notify_method

    语法: notify_method get|post
    上下文: rtmp, server, application, recorder

    设置通知的HTTP方法。默认值是POST与 application/x-www-form-urlencoded content type.在某些情况下得到的是可取的,例如,如果您打算处理Nginx的HTTP {}部分呼叫。在这种情况下,你可以使用arg_*变量访问参数。

    notify_method get;
    

    在HTTP GET方法处理 http{}部分可以做这样

    location /on_play {
        if ($arg_pageUrl ~* localhost) {
            return 200;
        }
        return 500;
    }

     -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    HLS

    语法: hls on|off
    上下文: rtmp, server, application

    切换HLS上的应用。

    hls on;
    hls_path /tmp/hls;
    hls_fragment 15s;
    

    在HTTP {}部分客户打HLS设置以下的位置。

    http {
        ...
        server {
            ...
            location /hls {
                types {
                    application/vnd.apple.mpegurl m3u8;
                }
                root /tmp;
                add_header Cache-Control no-cache;
    
                # 为了避免与跨域的HTTP请求(例如,在开发过程中)的问题
                add_header Access-Control-Allow-Origin *;
            }
        }
    }
    

    hls_path

    语法: hls_path path
    上下文: rtmp, server, application

    设置HLS播放列表和片段目录。如果该目录不存在,将会被创建。

    hls_fragment

    语法: hls_fragment time
    上下文: rtmp, server, application

    设置HLS片段长度。默认为5秒。

    hls_playlist_length

    语法: hls_playlist_length time
    上下文: rtmp, server, application

    设置播放列表HLS长度。默认为30秒。

    hls_playlist_length 10m;
    

    hls_sync

    语法: hls_sync time
    上下文: rtmp, server, application

    Sets HLS timestamp synchronization threshold. Default is 2ms. This feature prevents crackling noises after conversion from low-resolution RTMP (1KHz) to high-resolution MPEG-TS (90KHz).

    设置HLS时间戳同步的门槛。默认为2ms。此功能可以防止噼啪的噪声从低分辨率RTMP(1KHz的)转换为高分辨率MPEG-TS(90KHz的)之后。

    hls_sync 100ms;
    

    hls_continuous

    语法: hls_continuous on|off
    上下文: rtmp, server, application

    切换HLS连续模式。在这种模式下HLS序列号从它停止了最后一次启动。旧片段依然古色古香。默认是关闭的。

    hls_continuous on;
    

    hls_nested

    语法: hls_nested on|off
    上下文: rtmp, server, application

    切换HLS嵌套模式。在这种模式下hls_path的子目录为每个数据流创建。播放列表和片段在子目录中创建。默认是关闭的。

    hls_nested on;
    

    hls_base_url

    语法: hls_base_url url
    上下文: rtmp, server, application

    设置HLS播放列表项基本URL。当空这些项目没有前缀并且假设是在相同的位置父播放列表或低一个级别时使用hls_nested。此功能同时适用于主(变量)和从HLS播放列表。它可以让你下载播放列表,因为它包含儿童播放列表或片段完全引用本地播放。默认为空。

    hls_base_url http://myserver.com/hls/;
    

    hls_cleanup

    语法: hls_cleanup on|off
    上下文: rtmp, server, application

    切换HLS清理。默认情况下,该功能已开启。在这种模式下nginx的缓存管理器进程会删除HLS目录老HLS片段和播放列表。

    hls_cleanup off;
    

    hls_fragment_naming

    语法: hls_fragment_naming sequential|timestamp|system
    上下文: rtmp, server, application

    设置片段的命名方式

    • sequential - 使用增加的整数
    • timestamp -  使用流时间戳
    • system -使用系统时间

    默认值是连续的

    hls_fragment_naming system;
    

    hls_fragment_naming_granularity

    语法: hls_fragment_naming_granularity number
    上下文: rtmp, server, application

    设置HLS片段IDS粒度。如果大于零,改变IDS来划分提供的价值。默认值是零。

    # use system time rounded to 500ms as fragment names
    hls_fragment_naming system;
    hls_fragment_naming_granularity 500;
    

    hls_fragment_slicing

    语法: hls_fragment_slicing plain|aligned
    上下文: rtmp, server, application

    设置片段切片模式。

    • plain - 当达到目标时的持续时间片段切换
    • aligned -开关片段时传入的时间戳片段持续时间的倍数。该模式使得能够产生不同的nginx实例相同片段

    默认值是 plain.

    hls_fragment_slicing aligned;
    

    hls_variant

    语法: hls_variant suffix [param*]
    上下文: rtmp, server, application

    添加HLS变项(variant entry 变量条目,翻译不准确)。当后缀是在流名称匹配,然后,对用hls_variant指示当前应用程序中指定的所有条目当前流创建播放列表中的变体。不带后缀名剥离用作变量流名称。原来的流像往常一样处理。

    以下后缀可选参数追加到播放列表M3U8 EXT-X-STREAM-INF。见HLS规范。 3.3.10。 EXT-X-STREAM-INF支持的参数的完整列表。

    rtmp {
        server {
            listen 1935;
    
            application src {
                live on;
    
                exec ffmpeg -i rtmp://localhost/src/$name
                  -c:a libfdk_aac -b:a 32k  -c:v libx264 -b:v 128K -f flv rtmp://localhost/hls/$name_low
                  -c:a libfdk_aac -b:a 64k  -c:v libx264 -b:v 256k -f flv rtmp://localhost/hls/$name_mid
                  -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 512K -f flv rtmp://localhost/hls/$name_hi;
            }
    
            application hls {
                live on;
    
                hls on;
                hls_path /tmp/hls;
                hls_nested on;
    
                hls_variant _low BANDWIDTH=160000;
                hls_variant _mid BANDWIDTH=320000;
                hls_variant _hi  BANDWIDTH=640000;
            }
        }
    }
    

    hls_type

    语法: hls_type live|event
    上下文: rtmp, server, application

    将X-PLAYLIST型播放列表指令指定HLS播放列表类型。直播HLS流通常是从当前实际位置是几个片段到播放列表的末尾播放。事件HLS流总是从播放开始播放。当事件模式确保播放列表的长度足以让整个事件。默认值是live;

    hls_type event;
    

    hls_keys

    语法: hls_keys on|off
    上下文: rtmp, server, application

    启用HLS加密。 AES-128的方法用来加密整个HLS片段。默认关闭。

    hls_keys on;
    

    下面是一个使用HLS加密示例配置。此配置要求nginx的与--with-http_ssl_module建为HTTPS支持。

    ...
    
    http {
        ...
        server {
            listen 443 ssl;
            server_name example.com;
    
            ssl_certificate /var/ssl/example.com.cert;
            ssl_certificate_key /var/ssl/example.com.key;
    
            location /keys {
                root /tmp;
            }
        }
    
        server {
            listen 80;
            server_name example.com;
    
            location /hls {
                root /tmp;
            }
        }
    }
    
    rtmp {
        server {
            listen 1935;
    
            application myapp {
                live on;
    
                hls on;
                hls_path /tmp/hls;
    
                hls_keys on;
                hls_key_path /tmp/keys;
                hls_key_url https://example.com/keys/;
                hls_fragments_per_key 10;
            }
        }
    }
    

    hls_key_path

    语法: hls_key_path path
    Context: rtmp, server, application

    设置将在其中自动生成HLS密钥保存的目录。密钥文件与在OpenSSL RAND_bytes()例行程序创建的.key延伸和伪随机的16字节的内容。如果该目录不存在,它在运行时创建的。默认情况下,hls_path目录用于密钥文件​​。但是请记住,你通常应该限制访问关键文件,这是容易当这些文件从播放列表和片段分开存放。

    hls_key_path /tmp/keys;
    

    hls_key_url

    语法: hls_key_url url
    上下文: rtmp, server, application

    设置HLS密钥文件条目的URL。当空这些项目没有前缀和密钥被认为是在相同的位置播放列表。默认为空

    hls_key_url https://myserver.com/keys/;
    

    用上述设定例的播放列表条目

    #EXT-X-KEY:METHOD=AES-128,URI="https://myserver.com/keys/337.key",IV=0x00000000000000000000000000000151
    

    hls_fragments_per_key

    语法: hls_fragments_per_key value
    上下文: rtmp, server, application

    设置具有相同的密钥加密的HLS片段的数目。零意味着只有一个密钥的发布开始创建,并在会话中的所有片段与此密钥加密。默认值是零。

    hls_fragments_per_key 10;

    MPEG-DASH MPEG组织批准了MPEG-DASH(HTTP动态自适应流媒体)标准

    dash

    语法: dash on|off
    上下文: rtmp, server, application

    应用程序切换MPEG-DASH。

    dash on;
    dash_path /tmp/dash;
    dash_fragment 15s;
    

    在HTTP {}部分设置以下的位置,为客户播放MPEG-DASH。

    http {
        ...
        server {
            ...
            location /dash {
                root /tmp;
                add_header Cache-Control no-cache;
    
                # #为了避免与跨域的HTTP请求(例如,在开发过程中)的问题
                add_header Access-Control-Allow-Origin *;
            }
        }
    }
    

    dash_path

    语法: dash_path path
    上下文: rtmp, server, application

    设置MPEG-DASH播放列表,片段目录。如果该目录不存在,它将被创建。

    dash_fragment

    语法: dash_fragment time
    上下文: rtmp, server, application

    设置MPEG-DASH片段长度。默认为5秒。

    dash_playlist_length

    语法: dash_playlist_length time
    上下文: rtmp, server, application

    设置MPEG-DASH播放列表的长度。默认为30秒。

    dash_playlist_length 10m;
    

    dash_nested

    语法: dash_nested on|off
    上下文: rtmp, server, application

    切换MPEG-DASH嵌套模式。在这种模式下dash_path的子目录为每个数据流创建。播放列表和片段在子目录中创建。默认是关闭的。

    dash_nested on;
    

    dash_cleanup

    语法: dash_cleanup on|off
    上下文: rtmp, server, application

    切换MPEG-DASH清理。默认情况下,该功能已开启。在这种模式下nginx的缓存管理器进程从MPEG-DASH目录中删除旧的MPEG-DASH碎片和体现。流清单被删除后,初始化片段被删除。

    dash_cleanup off;
    

    Access log

    access_log

    语法: access_log off|path [format_name]
    上下文: rtmp, server, application

    设置访问日志参数。日志默认情况下开启。要关闭它使用ACCESS_LOG关闭指令。默认情况下访问日志记录是为了同一个文件的HTTP访问记录器(log/ access.log)。您可以指定访问日志指令另一个日志文件的路径。第二个参数是可选的。它可用于通过名称来指定记录格式。见log_format指令有关格式的详细信息。

    log_format new '$remote_addr';
    access_log logs/rtmp_access.log new;
    access_log logs/rtmp_access.log;
    access_log off;
    

    log_format

    语法: log_format format_name format
    上下文: rtmp

    创建一个名为日志格式。日志格式看起来非常相同的nginx的HTTP日志格式。几个变量的日志格式中支持:

    • connection - connection number
    • remote_addr - client address
    • app - application name
    • name - last stream name
    • args - last stream play/publish arguments
    • flashver - client flashVer
    • swfurl - client swfUrl
    • tcurl - client tcUrl
    • pageurl - client pageUrl
    • command - play/publish commands sent by client: NONEPLAYPUBLISHPLAY+PUBLISH
    • bytes_sent - number of bytes sent to client
    • bytes_received - number of bytes received from client
    • time_local - local time at the end of client connection
    • session_time - connection duration in seconds
    • session_readable_time - connection duration in human-readable format
    • msec - current unix timestamp in SEC.MSEC format

    默认的日志格式有名称相结合。下面是这种格式的定义

    $remote_addr [$time_local] $command "$app" "$name" "$args" - 
    $bytes_received $bytes_sent "$pageurl" "$flashver" ($session_readable_time)
    

    Limits

    max_connections

    语法: max_connections number
    上下文: rtmp, server, application

    设置RTMP发动机的最大连接数。默认关闭

    max_connections 100;
    

    Statistics

    统计模块是不同于此处列出的所有其他模块NGINX HTTP模块。因此,统计指令应位于HTTP {}块之内

    rtmp_stat

    语法: rtmp_stat all
    上下文: http, server, location

    设置RTMP统计处理当前的HTTP位置。 RTMP统计是动态的XML文档。要观看在浏览器XHTML页面使用rtmp_stat_stylesheet指令这份文件。

    http {
        server {
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                root /path/to/stat/xsl/file;
            }
        }
    }
    

    rtmp_stat_stylesheet

    语法: rtmp_stat_stylesheet path
    上下文: http, server, location

    添加XML样式表引用统计XML,使其在浏览器查看。见rtmp_stat说明和示例的详细信息。

    Multi-worker live streaming

    多进程直播通过推流剩余nginx的工人执行。

    rtmp_auto_push

    语法: rtmp_auto_push on|off
    上下文: root

    切换自动推(多进程直播)模式。默认是关闭的。

    rtmp_auto_push_reconnect

    语法: rtmp_auto_push_reconnect timeout
    上下文: root

    设置自动推送重新连接超时,当工作进程被杀害。默认值是100毫秒。

    rtmp_socket_dir

    语法: rtmp_socket_dir dir
    上下文: root

    设置用于流推动UNIX域套接字目录。默认值是/ tmp。

    rtmp_auto_push on;
    rtmp_auto_push_reconnect 1s;
    rtmp_socket_dir /var/sock;
    
    rtmp {
        server {
            listen 1935;
            application myapp {
                live on;
            }
        }
    }
    
    

    Control  控制模块

    
    

    控制模块是HTTP模块,这使得它可以从使用​​HT​​TP协议之外的控制设rtmp模块。下面是如何使能控制一个例子。


    http { ... server { listen 8080; server_name localhost; .... location /control { rtmp_control all; } } }

    有控制模块内的几个子模块的每个控制不同的功能。

    录制

    该子模块启动和停止与手动标志创建记录。语法:

    http://server.com/control/record/start|stop?srv=SRV&app=​​APP&name=NAME&rec=REC

    SRV = SRV - 可选的服务器{}块RTMP {}块中号,默认为第一个服务器{}块
    应用= APP - 所需的应用程序名称
    NAME =名称 - 需要流名称
    REC = REC - 可选的记录名称,默认为根(未命名)记录

    rtmp {
        server {
            listen 1935;
            application myapp {
                live on;
                recorder rec1 {
                   record all manual;
                   record_suffix all.flv;
                   record_path /tmp/rec;
                   record_unique on;
               }
           }
        }
    }

    用下面的命令发布该流

    ffmpeg -i http://someserver.com/mychannel.ts -c:v copy -c:a nellymoser -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream

    使用下面的命令来启动和停止录音

    curl "http://localhost:8080/control/record/start?app=myapp&name=mystream&rec=rec1"
    curl "http://localhost:8080/control/record/stop?app=myapp&name=mystream&rec=rec1"

    如果记录开始/停止请求有时会返回什么都没有,你应该检查,如果你使用多进程,单个进程的比较好

    删除 drop

    该子模块提供了一种简单的方法来删除客户端连接。语法:

    http://server.com/control/drop/publisher|subscriber|client?srv=SRV&app=APP&name=NAME&addr=ADDR&clientid=CLIENTID

    srv, app, name -相同

    addr - 可选的客户端地址(同样由rtmp_stat返回)
    clientid  - 可选nginx的客户端ID(日志和统计显示)
    第一种方法 删除/发布者 发行下降的连接​​。第二方法是 删除或者客户端删除,如果没有指定地址每连接匹配addr参数或所有客户端(包括出版商)。

    
    
    curl http://localhost:8080/control/drop/publisher?app=myapp&name=mystream
    curl http://localhost:8080/control/drop/client?app=myapp&name=mystream
    curl http://localhost:8080/control/drop/client?app=myapp&name=mystream&addr=192.168.0.1
    curl http://localhost:8080/control/drop/client?app=myapp&name=mystream&clientid=1
    
    

    Redirect 重定向

    重定向播放/客户端发布到一个新的数据流。语法:

    http://server.com/control/redirect/publisher|subscriber|client?srv=SRV&app=APP&name=NAME&addr=ADDR&clientid=CLIENTID&newname=NEWNAME

    srv, app, name, addr, clients - 和上面相同
    newname - 新的流名重定向到

     
    
    

    Debug log

    如果您需要解决您可能需要观看调试日志流的问题。对于--with调试标志配置Nginx的。

    cd nginx-X.Y.Z
    ./configure --add-module=/path/to/nginx-rtmp-module --with-debug ...
    

    编译一套nginx的error.log中水平nginx.conf调试后

    error_log logs/error.log debug;
    

    这之后,你将有很多的error.log中调试信息。

    Exec wrapper in bash

    你可以用任何语言编写的exec包装。然而,你应该注意终止进程。当出版商关闭流的所有执行处理被终止。如果指定的exec指令,而不是真正的ffmpeg的包装,那么你最终可能会与你的ffmpeg还活着,孤儿,直到超时读取输入数据。

    该解决办法是用信号陷阱。下面是在bash这种包装的一个例子。

    #!/bin/bash
    
    on_die ()
    {
        # kill all children
        pkill -KILL -P $$
    }
    
    trap 'on_die' TERM
    ffmpeg -i rtmp://localhost/myapp/$1 -c copy -f flv rtmp://localhost/myapp2/$1 &
    wait
    

    该脚本注册它终止子的ffmpeg SIGTERM处理器。 nginx的,RTMP发送默认的信号是SIGKILL不能被捕获。对于上面的脚本按预期的行为,你需要exec_kill_signal指令来改变EXEC终止信号。它接受数字或符号信号名称(POSIX.1-1990信号)。这里的示例应用程序。

    application myapp {
        live on;
    
        exec /var/scripts/exec_wrapper.sh $name;
        exec_kill_signal term;
    }
    
    application myapp2 {
        live on;
    }

    Examples 一些实例

    简单的视频点播

    rtmp {
        server {
            listen 1935;
            application vod {
                play /var/flvs;
            }
        }
    }
    

    简单的直播服务

    rtmp {
        server {
            listen 1935;
            application live {
                live on;
            }
        }
    }
    

    重新翻译远程流

    rtmp {
        server {
            listen 1935;
            application tv {
                live on;
                pull rtmp://cdn.example.com:443/programs/main pageUrl=http://www.example.com/index.html name=maintv;
            }
        }
    }
    

    重新翻译与HLS支持远程流

    rtmp {
        server {
            listen 1935;
            application tv {
                live on;
    
                hls on;
                hls_path /tmp/tv2;
                hls_fragment 15s;
    
                pull rtmp://tv2.example.com:443/root/new name=tv2;
            }
        }
    }
    
    http {
        server {
            listen 80;
            location /tv2 {
                alias /tmp/tv2;
            }
        }
    }
    

    物流通过RTMP你的X屏幕

    ffmpeg -f x11grab -follow_mouse centered -r 25 -s cif -i :0.0 -f flv rtmp://localhost/myapp/screen


    Set up live streaming

    要建立RTMP支持,你需要添加RTMP{}部分nginx.conf(PREFIX/conf/nginx.conf))。股票nginx.conf只包含HTTP{}部分。

    使用此nginx.conf,而不是原始的配置:

    #user  nobody;
    worker_processes  1;
    
    error_log  logs/error.log debug;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       8080;
            server_name  localhost;
    
            # sample handlers
            #location /on_play {
            #    if ($arg_pageUrl ~* localhost) {
            #        return 201;
            #    }
            #    return 202;
            #}
            #location /on_publish {
            #    return 201;
            #}
    
            #location /vod {
            #    alias /var/myvideos;
            #}
    
            # rtmp stat
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                # you can move stat.xsl to a different location
                root /usr/build/nginx-rtmp-module;
            }
    
            # rtmp control
            location /control {
                rtmp_control all;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    rtmp {
        server {
            listen 1935;
            ping 30s;
            notify_method get;
    
            application myapp {
                live on;
    
                # sample play/publish handlers
                #on_play http://localhost:8080/on_play;
                #on_publish http://localhost:8080/on_publish;
    
                # sample recorder
                #recorder rec1 {
                #    record all;
                #    record_interval 30s;
                #    record_path /tmp;
                #    record_unique on;
                #}
    
                # sample HLS
                #hls on;
                #hls_path /tmp/hls;
                #hls_sync 100ms;
            }
    
            # Video on demand
            #application vod {
            #    play /var/Videos;
            #}
    
            # Video on demand over HTTP
            #application vod_http {
            #    play http://localhost:8080/vod/;
            #}
        }
    }

    Statistics (统计)

    在浏览器中输入 http://localhost:8080/stat  看到目前的流媒体统计,连接的客户端,带宽等。

    Publishing with ffmpeg

    发布现场视频流最简单的方法是使用的ffmpeg(或avconv)。它已经安装在大多数系统中,也方便安装在其他的上面。

    RTMP仅支持的编解码器的数量有限。最流行的RTMP视频编解码器是H264,索伦森-H263(又名FLV)和音频编解码器AAC,MP3,得到Nellymoser,Speex语音。

    如果您的视频与这些编解码器编码(最常见的是对H264 / AAC),那么你不需要任何转换。否则,你需要将视频转换为支持的编解码之一。

    我们将流测试文件 /var/videos/test.mp4 to server with ffmpeg.

    Streaming without conversion (given test.mp4 codecs are compatible with RTMP)

    ffmpeg -re -i /var/Videos/test.mp4 -c copy -f flv rtmp://localhost/myapp/mystream
    

    Streaming and encoding audio (AAC) and video (H264), need libx264 and libfaac

    ffmpeg -re -i /var/Videos/test.mp4 -c:v libx264 -c:a libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
    

    Streaming and encoding audio (MP3) and video (H264), need libx264 and libmp3lame

    ffmpeg -re -i /var/Videos/test.mp4 -c:v libx264 -c:a libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
    

    Streaming and encoding audio (Nellymoser) and video (Sorenson H263)

    ffmpeg -re -i /var/Videos/test.mp4 -c:v flv -c:a nellymoser -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
    

    Publishing video from webcam

    ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream
    

    Playing with ffplay

    ffplay rtmp://localhost/myapp/mystream
    

    Publishing and playing with flash

    See test/rtmp-publisher directory for test flash applets and html.

     
  • 相关阅读:
    c++中重载、重写、覆盖的区别
    c++值传递,指针传递,引用传递以及指针与引用的区别
    c/c++中static与extern关键字介绍
    Typedef和#define之间的区别
    c++中sizeof()的用法介绍
    c++笔试题:不使用第三个变量来交换俩个变量的数值
    c++位运算符介绍
    3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建
    2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离
    1.高并发教程-基础篇-之nginx负载均衡的搭建
  • 原文地址:https://www.cnblogs.com/zx-admin/p/5783523.html
Copyright © 2011-2022 走看看