【docker】使用docker安装nginx-rtmp实现rtmp和hls本地推流和播放

1、nginx

http://nginx.org/en/

 

2、docker-hub

https://hub.docker.com/r/alqutami/rtmp-hls

https://hub.docker.com/_/nginx


3、github

https://github.com/TareqAlqutami/rtmp-hls-server

https://github.com/arut/nginx-rtmp-module

 

4、安装

默认安装

docker run -d -p 1935:1935 -p 8080:8080 alqutami/rtmp-hls

指定配置文件安装

docker run -d --name nginx-hls -p 1935:1935 -p 8080:8080 -v $HOME/Tools/nginx-hls/nginx-hls.conf:/etc/nginx/nginx.conf alqutami/rtmp-hls

nginx-hls.conf

worker_processes  auto;
#error_log  logs/error.log;

events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000; 
        # ping 30s;
        # notify_method get;

        # This application is to accept incoming stream
        application live {
            live on; # Allows live input

            # for each received stream, transcode for adaptive streaming
            # This single ffmpeg command takes the input and transforms
            # the source into 4 different streams with different bitrates
            # and qualities. # these settings respect the aspect ratio.
            exec_push  /usr/local/bin/ffmpeg -i rtmp://localhost:1935/$app/$name -async 1 -vsync -1
                        -c:v libx264 -c:a aac -b:v 256k  -b:a 64k  -vf "scale=480:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_low
                        -c:v libx264 -c:a aac -b:v 768k  -b:a 128k -vf "scale=720:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_mid
                        -c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_high
                        -c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_hd720
                        -c copy -f flv rtmp://localhost:1935/show/$name_src;            
            drop_idle_publisher 10s; 
        }

        # This is the HLS application
        application show {
            live on; # Allows live input from above application
            deny play all; # disable consuming the stream from nginx as rtmp
            
            hls on; # Enable HTTP Live Streaming
            hls_fragment 3;
            hls_playlist_length 20;
            hls_path /mnt/hls/;  # hls fragments path
            # Instruct clients to adjust resolution according to bandwidth
            hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
            hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
            hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
            hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
            hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
            
            # MPEG-DASH
            dash on;
            dash_path /mnt/dash/;  # dash fragments path
            dash_fragment 3;
            dash_playlist_length 20;            
        }
    }
}


http {
    sendfile off;
    tcp_nopush on;
    directio 512;
    # aio on;
    
    # HTTP server required to serve the player and HLS fragments
    server {
        listen 8080;
        
        # Serve HLS fragments
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            
            root /mnt;

            add_header Cache-Control no-cache; # Disable cache
            
            # CORS setup
            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;
            }
        }
        
        # Serve DASH fragments
        location /dash {
            types {
                application/dash+xml mpd;
                video/mp4 mp4;
            }

            root /mnt;
            
            add_header Cache-Control no-cache; # Disable cache


            # CORS setup
            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;
            }
        }        
        
        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet 
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            root /usr/local/nginx/html;
        }

    }
}

是从容器复制出来的,方便修改

 

5、简单了解各种协议及应用

https://blog.csdn.net/qq_42604176/article/details/125475864

 

6、推流到rtmp server

播放一遍

ffmpeg -re -i ./test.flv -c copy -f flv rtmp://192.168.10.225:1935/live/test

循环推流

ffmpeg -re -stream_loop -1 -i ./test.flv -c copy -f flv rtmp://192.168.10.225:1935/live/test

 

7、使用镜像自带的players播放

  • To play RTMP content (requires Flash): http://localhost:8080/players/rtmp.html
  • To play HLS content: http://localhost:8080/players/hls.html
  • To play HLS content using hls.js library: http://localhost:8080/players/hls_hlsjs.html
  • To play DASH content: http://localhost:8080/players/dash.html
  • To play RTMP and HLS contents on the same page: http://localhost:8080/players/rtmp_hls.html

 

 

 

8、使用VLC播放器播放

点击“打开网络串流”

 

 

 点击播放

 

 

 至此,测试环境,网络推流的测试环境就搭建好了

 

9、关于视频格式转换

https://www.cnblogs.com/lemos/p/8188344.html

 

参考链接:

https://blog.csdn.net/cai6811376/article/details/74783269/

https://blog.csdn.net/fareast_mzh/article/details/104413012

 

posted @ 2022-08-23 15:23  代码诠释的世界  阅读(3059)  评论(0编辑  收藏  举报