ffmpeg+nginx+video实现rtsp流转hls流(H5)
本文档以大华摄像头为基础进行研究,基于window环境,Linux类似,首先了解不同协议之间的区别,本次研究主要使用rtmp(长连接) 和 hls(短连接)。
大华提供的视频流地址格式:
rtsp://{账号}:{密码}@{IP}:{端口}/cam/realmonitor?channel=1&subtype=0
例如:
rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0
一、视频流解码 - 环境配置
1、FFmpeg下载:http://ffmpeg.zeranoe.com/builds/
下载并解压FFmpeg文件夹,配置环境变量:在“Path”变量原有变量值内容上加上d:\ffmpeg\bin,验证:ffmpeg -version 出现版本号则成功
2、安装nginx服务器
(1) 下载具有rtmp的nginx
https://github.com/illuspas/nginx-rtmp-win32 可以直接使用
(以下配置已经存在)
rtmp { #增加rtmp配置
server {
listen 1935;
chunk_size 4096;
application live { #使用rtmp 协议播放 (例如:播放器访问地址 rtmp://localhost:1935/live/test)
live on; #开启实时
}
application hls { #增加hls 协议支持
live on; #开启实时
hls on; #开启hls
hls_path C:/nginx-1.12.2/html/hls; #切片存放位置
hls_fragment 1s; #每个TS文件包含1秒的视频内容
hls_playlist_length 3s; #HLS播放列表长度。 默认为30秒
}
}
}
http {
server {
listen 20000;
location / {
root html;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root html;
}
#新增能访问到hls流协议配置
location /hls {
#server hls fragments
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
}
在nginx\conf\mime.types中为了支持hls流协议新增:
application/x-mpegURL m3u8;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
(2) 启动nginx
进入nginx目录,
启动 start nginx。
停止 nginx.exe -s stop
3、转码
进入cmd.exe命令框, 输入 ffmpeg -i "rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0" -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 -f flv rtmp://localhost:1935/live/test