• Nginx搭建视频流媒体服务(Win)


    下载Nginx,nginx 1.7.11.3 Gryphon版本

    地址

    2、配置nginx

    #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_auto_push on;
     
    #RTMP服务
    rtmp{
    	server{
    		listen 1935;        #服务端口
    		chunk_size 4096;    #数据传输块的大小
    		#rtmp点播配置,访问方式:rtmp://127.0.0.1:1935/vod/xxx.mp4
    		application vod {
    			play ./vod;    #点播视频存放目录,里面放一个xxx.mp4可以直接播放
    		}
    		#rtmp协议直播推流
    		application live {
    			live on;
    			record all;
    			record_path ./rec; # 保存路径
    			#record_max_size 1K;
    			#append current timestamp to each flv
    			record_unique on;
    			#publish only from localhost
    			#allow publish 127.0.0.1;
    			#deny publish all; 
    		}
    		 # HLS协议直播推流
    		application hls {
    			live on;                    #开启rtmp直播
    			hls on;                     #开启hls支持
    			wait_video on;              #第一个视频帧发送前禁用音频
    			hls_path ./m3u8File;     #文件保存路径,需要先创建,不然执行推流会报错
    			hls_fragment 5s;            #用来设置每一个块的大小。默认是5秒。只能为整数
    			hls_playlist_length 30s;    #设置播放列表的长度,单位是秒,听说设置成3秒延迟低点
    			hls_nested off;              #默认是off。打开后的作用是每条流自己有一个文件夹
    			hls_cleanup off;            #不清理ts	, on|off 默认是开着的,是否删除列表中已经没有的媒体块
    			#hls_continuous:            #on|off 设置连续模式,是从停止播放的点开始还是直接跳过
    	   }
    	}
    }
    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       9090;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
    		#hls点播地址
    		location /hls {
    			types {
    				application/vnd.apple.mpegurl m3u8; 
    				#或 application/x-mpegURL
    				video/mp2t ts;
    			}
    			alias ./m3u8File; #点播视频文件(.ts;.m3u8)存放位置
    			expires -1;
    			add_header Cache-Control no-cache; #跨域支持,不然网页播放不了
    			add_header Access-Control-Allow-Origin *;
    			add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    			add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    		}
        # 直播状态监听
        location /stat{
    		    rtmp_stat all;
    			rtmp_stat_stylesheet stat.xsl;
    		}
    		location /stat.xsl{
    		    root ./nginx-rtmp-module-master;
    		}    
    		error_page   500 502 503 504  /50x.html;
    		location = /50x.html {
    			root   html;
    		}
     
    	}	
    	autoindex on;# 显示目录
    	autoindex_exact_size on;# 显示文件大小
    	autoindex_localtime on;# 显示文件时间
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    3、在nginx目录下面创建三个文件夹 m3u8File、 vod、rec

    javacv的依赖

     
            <!-- 媒体只用到以下两个,javacv、ffmpeg
            >
                <groupId>org.bytedecogroupId>
                <artifactId>javacvartifactId>
                <version>1.5.7version>
            dependency>
            
            <dependency>
                <groupId>org.bytedecogroupId>
                <artifactId>ffmpeg-platformartifactId>
                <version>5.0-1.5.7version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    推流命令

    package com.zj.ffmpeg;
    
    import org.bytedeco.javacpp.Loader;
    
    import java.io.IOException;
    
    /**
     * @see ffmpeg 常用命令汇总
     * @see ffmpeg 常用命令汇总
     *
     * @author 小布
     * @version 1.0.0
     * @className PushStream.java
     * @createTime 2023年08月19日 15:25:00
     */
    public class PushStream {
        public static void main(String[] args) {
            String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
            // ffmpeg -re -i video3.mp4 -vcodec h264 -acodec copy -f flv rtmp://127.0.0.1/live/test
            // ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-re", "-stream_loop", "-1", "-i", "D:\\1.mp4", "-c", "copy", "-f", "flv", "rtmp://127.0.0.1:1935/live/test");
            // 直播
            ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-re",  "-i", "D:\\1.mp4", "-c", "copy", "-f", "flv", "rtmp://127.0.0.1:1935/live/test");
            //切片
            // ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-re",  "-i", "D:\\1.mp4", "-vcodec", "copy", "-acodec", "copy","-f", "flv", "rtmp://127.0.0.1:1935/hls/test");
            try {
                pb.inheritIO().start().waitFor();
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    用VLC软件进行播放

    点播地址

    rtmp://127.0.0.1:1935/vod/1.flv
    
    • 1

    切片地址

    rtmp://127.0.0.1:1935/hls/test
    
    • 1

    直播地址

    rtmp://127.0.0.1:1935/hls/test
    
    • 1

    文件点播不支持mp4 文件

    检测配置是否正确

    nginx.exe -t
    
    • 1

    启动

    start nginx
    
    • 1

    关闭

    nginx -s stop
    
    • 1

    重新加载配置

    nginx.exe -s reload
    
    • 1

    关闭所有nginx进程

    taskkill /IM nginx.exe /F
    
    • 1

    windows任务管理器下Nginx的进程命令

    tasklist /fi "imagename eq nginx.exe"
    
    • 1

    Nginx搭建视频流媒体服务(直播&点播)
    在windows下搭建、配置nginx流媒体服务器,并进行rtmp流的推流、拉流测试

  • 相关阅读:
    GRACE球谐数据滤波处理(利用matlab实现GRACE月水储量的二维傅里叶变化滤波)
    JDBC 实现批量插入-任意表名
    重温javascript --(三)对象
    蓝桥杯倒计时 36天-DFS练习2
    vue3源码解析
    【深度学习框架格式转化】【GPU】Pytorch模型转ONNX模型格式流程详解【入门】
    VMWare 虚拟机如何扩展磁盘空间并挂载到已存在的根目录
    道路标识检测模型更新
    JAVASE语法零基础——封装与包
    git commit后如何撤销或修改
  • 原文地址:https://blog.csdn.net/tanhongwei1994/article/details/132742433