跳至正文
首页 » 搭建 RTMP 视频服务器

搭建 RTMP 视频服务器

Real-TIme Messageing Protocol(RTMP)是实时消息协议。本文将介绍 搭建 RTMP 视频服务器 。用最简洁的方式利用 NGINX 搭建 RTMP 服务器,使用 FFMPEG 将摄像头的影像推流至 RTMP服务器,可以使用 VLC 拉流播放视频。

搭建 RTMP 视频服务器

NGINX配置

安装 NGINX 和 RTMP 模块

sudo apt-get install nginx libnginx-mod-rtmp

修改 /etc/nginx/nginx.conf,在其中添加 rtmp 部分

rtmp {
   server {
       listen 1935;
       chunk_size 4096;

       application vod {
           play /var/www/html/video;#视频所在位置
       }
   }
}

其中, listen 为服务器端口, 默认为1935;chunk_size 为数据块尺寸;appliction 后为应用名称;play 为视频文件存放位置。

配置完成后,重启 nginx 服务

sudo systemctl restart nginx

/var/www/html/video 中存储了名为 test.mp4 的视频。这时就可以在 VLC 通过 rtmp://SERVERIP/void/test.mp4 看到我们在服务器的视频,其中 SESRVERIP 为服务器IP。

HLS推流

修改 /etc/nginx/nginx.conf,在上节添加的 rtmp > server 中添加 hls 应用

application hls{
    live on;
    hls on;
    hls_path /var/www/html/hls;
    hls_fragment 3;
    hls_playlist_length 60;
    deny play all;
}

在最外面添加 http 配置

http {
        sendfile off;
        tcp_nopush on;
        default_type application/octet-stream;

        server {
                listen 80;

                location / {
                        add_header 'Cache-Control' 'no-cache';
                        add_header 'Access-Control-Allow-Origin' '*' always;
                        add_header 'Access-Control-Expose-Headers' 'Content-Length';

                        # allow CORS preflight requests
                        if ($request_method = 'OPTIONS') {
                            add_header 'Access-Control-Allow-Origin' '*';
                            add_header 'Access-Control-Max-Age' 1728000;
                            add_header 'Content-Type' 'text/plain charset=UTF-8';
                            add_header 'Content-Length' 0;
                            return 204;
                        }

                        types{
                                application/dash+xml mpd;
                                application/vnd.apple.mpegurl m3u8;
                                video/mp2t ts;
                        }
                        root /var/www/html/;
                }
        }
}

/var/www/html/ 为 RTMP 相关文件的存储路径。

ffmpeg -re -i /dev/video0 -r 30 -video_size 640x480 -vcodec libx264 -acodec aac -strict -2 -f flv -vf transpose=1 rtmp://127.0.0.1/hls/room

其中 /dev/video0 为摄像头的设备地址。rtmp://127.0.0.1/hls/room 为推送的地址,因为推送的路径中包含了 hls,需要在运行前创建该路径(/var/www/html/hls)。

ffmpeg 运行一段运行一段时间后就可以看到 room.m3u8 产生了,这时就可以在 VLC 通过 http://SERVERIP/hls/room.m3u8 看到摄像头影像。

参考

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注