Nginx搭建直播服务器,并用rtmp,http-flv,hls三种模式拉流观看直播的流程

一、首先搭建直播服务器

环境widows,并且已经集成了 :nginx-http-flv-module模块

nginx.conf配置如下:

复制代码
worker_processes  1;

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

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

# 添加RTMP服务
rtmp {
    server {
        listen 1935; # 监听端口

        chunk_size 4000;
        application live {
            live on;
	    gop_cache on;
	    hls on;
	    hls_path html/hls
        }
    }
}

# HTTP服务
http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    server {
        listen       8080; # 监听端口

	location /flv {
	    flv_live on;
	    chunked_transfer_encoding on;
	    add_header 'Access-Control-Allow-Origin' '*';
	    add_header 'Access-Control-Allow-Credentials' 'true';
	    add_header 'Access-Control-Allow-Methods' '*';
	    add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
	    add_header 'Access-Control-Expose-Headers' '*';
	
	}
 
	location /stat.xsl {
            root html;
        }
		location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
		location / {
            root html;
        }
    }
}

application live {

}

是指推流的目录在live,hls on是指开启切片,hls_path: html/hls是指切片直播的目录,手动创建,hls_fragment:3,每3秒创建一个切片

注意loaction /flv配置的是跨域

二、推流:

复制代码
ffmpeg -re -i tuiliuceshi.mp4 -vcodec libx264 -acodec aac -f flv -y rtmp://127.0.0.1:1935/live/test1

三、播放

1、vlc播放:(有以下三种播放方式:)

**http-flv方式:**http://localhost:8080/flv?port=1935\&app=live\&stream=test1

Rtmp方式:rtmp://127.0.0.1:1935/live/test1

**Hls方式:**http://localhost:8080/hls/test1.m3u8

2、网页播放:

网页需要解决跨域问题,需要在nginx中配置:

复制代码
location /flv {
	    flv_live on;
	    chunked_transfer_encoding on;
	    add_header 'Access-Control-Allow-Origin' '*';
	    add_header 'Access-Control-Allow-Credentials' 'true';
	    add_header 'Access-Control-Allow-Methods' '*';
	    add_header "Access-Control-Allow-Headers" "Content-Type,Access-Token";
	    add_header 'Access-Control-Expose-Headers' '*';
	
	}

我们这个nginx推流服务器的http地址是:http://localhost:8080

我们再开一个nginx服务器,

我们再开一个nginx,端口为:80,http://localhost/可直接访问:

在网页 flv.html中,引用到flv.js

复制代码
https://cdn.bootcss.com/flv.js/1.3.3/flv.min.js
html 复制代码
<html>
	<head>
		<script src="https://cdn.bootcss.com/flv.js/1.3.3/flv.min.js">
		</script>
	</head>
	<body>
	  <video id = "videoElement" controls></video>
	  <script>
		if(flvjs.isSupported()){
			var videoElement = document.getElementById('videoElement')
			var flvPlayer = flvjs.createPlayer({
				type:'flv',
				url:'http//localhost:8080/flv?port=1935&app=live&stream=test1'
			})
			flvPlayer.attachMediaElement(videoElement)//将flv视频装载进来
			flvPlayer.load()//载入视频
			flvPlayer.play()//播放视频,如果不想自动播放,可以不要本行
		}

	  </script>
	</body>
</html>

这样就播放出来了视频

注意是两个nginx,一个是8080,负责直播推流,80的负责播放

ffmpeg检查摄像头、麦克风设备的命令:

复制代码
ffmpeg -list_devices true -f dshow -i dummy

播放摄像头命令:

复制代码
ffplay -f dshow -i video="Integrated Camera"

用ffmpeg推流的命令:

复制代码
ffmpeg -f -dshow -i video="useb2.0 PC Camera" -vcodec libx264 -f flv -y rtmp://127.0.0.1:1935/live/test001
相关推荐
网硕互联的小客服4 小时前
Linux服务器磁盘应该如何合理分区?
linux·运维·服务器
楚识科技4 小时前
合同比对全流程自动化:OCR识别+差异比对+法律风险字段抽取实战指南
运维·自动化·ocr
代码方舟5 小时前
零信任架构实战:基于天远人企关联构建自动化供应链金融网关
运维·人工智能·架构·自动化
lisanmengmeng5 小时前
nacos nrpe 监控机及被监控机部署
服务器·nacos·nagios·nrpe
Lsetea5 小时前
OpenSSL verify报error 20:CAfile、fullchain与本地信任链分层排查
nginx·https·ssl证书·openssl·证书链
贵沫末6 小时前
Ubuntu——常用软件安装
linux·运维·ubuntu
Doraemomo6 小时前
Linux内核驱动开发——中断与定时器
linux·运维·驱动开发
2301_808414386 小时前
Linux中动静态库的理解
linux·运维·服务器
分布式存储与RustFS7 小时前
模型 checkpoint 放对象存储:分片上传、断点续传与残留清理
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
一条破秋裤8 小时前
Linux 线程创建:pthread_create 与基本回收
java·linux·运维