The scenario is: machine A installs FFmpeg to connect to the camera, machine B installs nginx, machine A pushes the video stream to machine B, and the page program accesses the address of machine B to play the video.
1. FFmpeg must be installed
Official website: http://ffmpeg.org
FFmpeg is powerful, mainly used to push and pull streams, and the built-in ffplay can also realize playback.
2. nginx must be installed
nginx needs to enable the rtmp module
Linux: First clone the rtmp module https://github.com/arut/nginx-rtmp-module , add the rtmp module when compiling.
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module --with-pcre=../pcre-8.42 --with-openssl=../openssl-1.1.0i --with-zlib=../zlib-1.2.11
make && make install
windows: directly download the nginx address with rtmp module http://nginx-win.ecsds.eu/download/nginx 1.7.11.3 Gryphon.zip .
3. nginx configuration
It is nginx.conf under linux and nginx-win.conf under windows. Two places need to be changed :
1. Add rtmp{} to the outer layer.
rtmp { #保存所有rtmp配置的块
server { #声明一个rtmp实例
listen 1935; #给Nginx添加一个监听端口以接收rtmp连接
chunk_size 4096; #流整合的最大的块大小。默认值为4096。这个值设置的越大CPU负载就越小。这个值不能低于128
application hls { #hls的地址
live on;
hls on;
hls_path html/hls;
hls_fragment 1s;
}
application live { #rtmp的地址
live on;
}
}
}
2. http-->server added:
# ffmpeg生成hls流的http访问配置
location /hls {
#若nginx\conf\mime.types中没有配置如下type,请加上,或直接在mime.types加
types{
#application/vnd.apple.mpegurl m3u8;
application/x-mpegURL m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
4. Start nginx
Under windows:
nginx.exe -c conf\nginx-win.conf
Visit localhost to see the nginx welcome page and it is successful.
5. FFmpeg push streaming
Method 1: rtsp to rtmp
ffmpeg -i "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream" -vcodec libx264 -acodec aac -f flv rtmp://192.168.100.170:1935/live/movie
The A machine uses the ffmpeg command to push the stream, after -i is the camera stream address, and after -flv is the address of the nginx machine.
Use the command: ffplay.exe rtmp://localhost:1935/live/movie to view the video.
Method 2: rtsp to hls
ffmpeg -i "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream" -vcodec libx264 -acodec aac -f flv rtmp://192.168.100.170:1935/hls/movie
The A machine uses the ffmpeg command to push the stream, after -i is the camera stream address, and after -flv is the address of the nginx machine.
After receiving by machine B, you can see the ts video clip under the nginx html/hls directory.
Use the command: ffplay.exe http://localhost/hls/movie.m3u8 to view the video.
to sum up:
hls has a higher latency than rtmp, but rtmp pages need to call flash to play, this is the biggest disadvantage, hls is not used.
Reference article
https://blog.csdn.net/qq_21108311/article/details/94567915 nginx+ffmpeg to achieve streaming media
https://blog.csdn.net/wenqiangluyao/article/details/97897794?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.add_param_isCf&depth_1-utm_source=distribute.pc_relevantFrom.none-Learn task-blog 3.add_param_isCf Nginx+FFmpeg realizes rtsp stream to hls stream, realizes video playback through H5 video on WEB
https://blog.csdn.net/zongyue_wang/article/details/82698281 Nginx supports HLS configuration