准備環境和文件
1、下載ffmpeg的包[https://foxbaby.lanzoui.com/iYjPmup51cd]
地址:https://ffmpeg.org/download.html#build-windows
鼠標放到Windows上選擇一個構建,點擊ffmpeg-release-full.7z下載。
2、下載nginx[https://foxbaby.lanzoui.com/irkdzup6hra]
地址:http://nginx-win.ecsds.eu/download/
下載nginx 1.7.11.3 Gryphon.zip
3、下載截取屏幕的插件
地址:https://foxbaby.lanzoui.com/imI5Vup50za
操作步驟
4、安裝截取屏幕的插件
5、解壓ffmpeg
把bin目錄設置成全局環境變量方便在cmd里使用ffmpeg推流
6、解壓nginx
在根目錄下,創建三個目錄:m3u8File、rec、vod;
進入conf文件夾,新建記事本並改名nginx.conf,內容如下
點擊查看代碼
worker_processes 1; #Nginx進程數,建議設置為等於CPU總核數
events {
worker_connections 1024; #工作模式與連接數上限
}
rtmp_auto_push on;
#RTMP服務
rtmp{
server{
listen 1935; #推流的端口
chunk_size 4096; #數據傳輸塊的大小
application vod{
play ./vod; #視頻文件存放位置
}
application live{
live on;
hls on; #開啟hls直播。這個參數把直播服務器改造成實時回放服務器
#wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了
hls_path ./html/hls; #切片視頻文件存放位置(HLS,m3u8文件存放位置)
hls_fragment 2s; #每個視頻切片的時長
hls_playlist_length 16s;
recorder myRecord{
record all manual;
record_suffix _.flv;
record_path ./rec;
}
#hls_continuous on; #連續模式
#hls_cleanup on; #對多余的切片進行刪除
#hls_nested on; #嵌套模式
}
}
}
#HTTP服務
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /live_hls{
types{
#m3u8 type設置
application/vnd.apple.mpegurl m3u8;
#ts分片文件設置
video/mp2t ts;
}
#指向訪問m3u8文件目錄
alias ./html/hls;
add_header Cache-Control no-cache; #禁止緩存
}
location /control{
rtmp_control all;
}
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root ./nginx-rtmp-module-master;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
7、回到nginx根目錄,命令行啟動nginx
點擊查看代碼
啟動:start nginx
快速停止:nginx -s stop
正常停止:nginx -s quit
配置重載:nginx -s reload
使用ffmpeg推流
新建cmd命令行,輸入:
ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="virtual-audio-capturer" -vcodec libx264 -preset:v ultrafast -pix_fmt yuv420p -acodec aac -f flv rtmp://127.0.0.1/live
最后跟着的rtmp://中的端口號,就是上面nginx配置的rtmp服務端口
前端訪問
新建html文件,內容如下:
點擊查看代碼
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 直播</title>
<link href="http://vjs.zencdn.net/5.19/video-js.min.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/5.19/video.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/videojs-flash@2/dist/videojs-flash.min.js"></script>
</head>
<body>
<video id="myvideo" class="video-js vjs-default-skin" controls preload="auto" width="1280" height="720"
poster="" data-setup="{}">
<source src="rtmp://127.0.0.1:8008/live/" type="rtmp/flv">
</video>
<div>
<p>ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="virtual-audio-capturer" -vcodec libx264 -preset:v ultrafast -pix_fmt yuv420p -acodec aac -f flv rtmp://127.0.0.1/live</p>
</div>
<div>
<p>ffmpeg -list_devices true -f dshow -i dummy</p>
</div>
</body>
</html>