Windows上搭建Nginx RTMP服务器并使用FFmpeg实现本地视频推流

场景

RTMP

RTMP协议
(1)是流媒体协议。
(2)RTMP协议是 Adobe 的私有协议,未完全公开。
(3)RTMP协议一般传输的是 flv,f4v 格式流。
(4)RTMP一般在 TCP 1个通道上传输命令和数据。

FFmpeg

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

官方ffmpeg下载地址:FFmpeg

下载之后找到bin下的三个exe文件,只需要这几个即可。

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、下载nginx-rtmp-win64并修改配置文件

github地址:GitHub - zhongwcool/nginx-rtmp-win64

下载之后其配置文件已经添加了rtmp的配置

rtmp {
    server {
        listen 1935;

        application live {
            live on;
        }
  
        application hls {
            live on;
            hls on; 
            hls_path temp/hls; 
            hls_fragment 8s; 
        }
    }
}

这里的配置的意思是

listen 代表监听端口

application live 跟的是推流请求路径

live on 代表开启实时

hls on 代表开启hls

hls_path 代表rtmp推流请求路径,文件存放路径

hls_fragment 8s 代表每个TS文件包含5秒的视频内容

完整配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        application live {
            live on;
        }
		
        application hls {
            live on;
            hls on;  
            hls_path temp/hls;  
            hls_fragment 8s;  
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       110;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

所以这里需要在nginx目录下新建temp/hls目录

2、启动nginx

然后双击nginx.exe,启动完成后去任务管理器中查看是否启动成功,如果没有,则去看logs下的错误日志。

3、下载ffmpeg

按照上面下载地址下载ffmpeg,并将视频和ffmpeg.exe放在同一目录下

4、编写启动脚本实现推流

新建start.bat

ffmpeg.exe -re -i D:\test\1.mp4 -vcodec libx264 -acodec aac -f flv rtmp://127.0.0.1:1935/live/badao

其中D:\test\1.mp4 就是本地视频的位置

后面1935是上面nginx中配置的端口

live是nginx中配置推流的路径

badao是这里自定义的推流地址

双击bat启动

5、引流测试

下载VLC

Official download of VLC media player, the best Open Source player - VideoLAN

VLC 是一款自由、开源的跨平台多媒体播放器及框架,可播放大多数多媒体文件,以及 DVD、音频 CD、VCD 及各类流媒体协议。

下载之后安装启动-文件-打开网络串流

输入地址为

rtmp://127.0.0.1:1935/live/badao

效果为

猜你喜欢

转载自blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/120868728