一、目的:通过搭建直播软件环境来了解直播软件的原理
二、环境:
1、Win10
2、nginx - (rtmp服务器)
3、OBS Studio或FFmpeg.exe - (用于推流)
4、VLC media player或PotPlayer - (用于播放直播)
本示例以最简单的方式进行尝试:FFmpeg.exe(推流)+VLC(播放)
三、步骤:
a、下载并配置nginx+rtmp服务器 ,修改配置文件(..\nginx\conf\nginx.conf)如下
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 | worker_processes 1; error_log logs/error.log debug; events { worker_connections 1024; } rtmp { server { listen 1935; timeout 20s; application hls { live on; hls on; hls_path temp/hls; hls_playlist_length 5s; hls_fragment 1s; } } } http { server { listen 8089; location / { root www; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root www; } location /hls { #server hls fragments types{ application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias temp/hls; expires -1; } location /daniu9966cotlive<GODWisH> { rtmp_control all; } } } |
b、双击启动运行nginx
c、浏览器输入地址:http://127.0.0.1:8089/stat 显示信息如下

d、应用FFmpeg.exe推流(可以推送桌面、可以推送视频文件、也可以推送摄像头,本示例以最简单的推送桌面为示例,其他方式类同)
只需运行如下命令:ffmpeg -f gdigrab -i desktop -f flv rtmp://127.0.0.1:1935/hls/12345 即可将屏幕信息推送到rtmp服务器上,当执行后统计信息页面变成如下:

其中12345 是自定义的串流密钥,根据此密钥进行抓流
e、通过VLC Player抓流:
打开VLC Player 点击菜单:媒体-打开网络串流-输入串流地址:rtmp://127.0.0.1:1935/hls/12345-点击播放
如下图可以看到,VLC播放当前桌面信息

四、总结:
通过简单的示例可以了解到,部署直播环境主要有三个方面:
1、推流
2、服务器
3、播放
后面有时间将会尝试通过 WPF+FFmpeg API +SDL2实现一个简单的直播桌面程序
五、下载地址: