一、前言
大家晚上好,今天給大家來一個簡單的流媒體的推流和拉流的實際操作,廢話不多說,趕緊上車!
二、srs流媒體服務器
1、什么是srs流媒體服務器:
srs(Simple Realtime Server),簡單實時服務器,它支持是一個流媒體集成,支持RTMP / HLS / FLV,高效,穩(wěn)定,易用,簡單而快樂。SRS
是RTMP / HLS / FLV流式群集,高效,穩(wěn)定,簡單。它是一個國內(nèi)大佬寫的一個非常優(yōu)秀的一個流媒體服務器,它的開源地址在github和gitee上都有:
https://github.com/ossrs/srs
https:///winlinvip/srs.oschina
從這個github上大概我了解到,現(xiàn)在srs暫時支持3.0版本,4.0版本應該快發(fā)布了,在3.0版本的wiki教程中有一部分視頻教程在b站上,感興趣的小伙伴可以看看:

https://github.com/ossrs/srs/wiki/v3_CN_Home
2安裝srs流媒體服務器:
git clone https:///winlinvip/srs.oschina.git cd srs.oschina
root@ubuntu:/home/txp/share/srs# cd srs.oschina/
root@ubuntu:/home/txp/share/srs/srs.oschina# ls
AUTHORS.txt LICENSE README.md trunk
root@ubuntu:/home/txp/share/srs/srs.oschina# git branch -a
* develop
remotes/origin/2.0release
remotes/origin/3.0release
remotes/origin/4.0release
remotes/origin/HEAD -> origin/develop
remotes/origin/develop
remotes/origin/feature/h265
remotes/origin/master
remotes/origin/min
root@ubuntu:/home/txp/share/srs/srs.oschina# git checkout -b 3.0 remotes/origin/3.0release
Branch 3.0 set up to track remote branch 3.0release from origin.
Switched to a new branch '3.0'
root@ubuntu:/home/txp/share/srs/srs.oschina# git branch -a
* 3.0
develop
remotes/origin/2.0release
remotes/origin/3.0release
remotes/origin/4.0release
remotes/origin/HEAD -> origin/develop
remotes/origin/develop
remotes/origin/feature/h265
remotes/origin/master
remotes/origin/min
root@ubuntu:/home/txp/share/srs/srs.oschina# cd trunk/
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# ./configure
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# make
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# ls
3rdparty auto conf configure doc etc ide Makefile modules objs research scripts src usr
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# vim conf/rtmp.conf
#現(xiàn)在后期的版本不用配置,默認就是已經(jīng)配置好的
# the config for srs to delivery RTMP
# @see https://github.com/ossrs/srs/wiki/v1_CN_SampleRTMP
# @see full.conf for detail config.
listen 1935;
max_connections 1000;
vhost __defaultVhost__ {
}
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# ./objs/srs -c conf/rtmp.conf
[2021-03-13 23:14:46.387][Trace][1974][0] XCORE-SRS/3.0.156(OuXuli)
[2021-03-13 23:14:46.387][Trace][1974][0] config parse complete
[2021-03-13 23:14:46.387][Trace][1974][0] you can check log by: tail -f ./objs/srs.log (@see https://github.com/ossrs/srs/wiki/v1_CN_SrsLog)
[2021-03-13 23:14:46.387][Trace][1974][0] please check SRS by: ./etc/init.d/srs status
我們可以查看日志來看啟動過程中詳細的信息:
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# tail -f ./objs/srs.log
[2021-03-13 23:14:46.389][Trace][1976][0] son(daemon) process running.
[2021-03-13 23:14:46.390][Trace][1974][0] grandpa process exit.
[2021-03-13 23:14:46.390][Trace][1976][0] st_init success, use epoll
[2021-03-13 23:14:46.390][Trace][1976][764] server main cid=764, pid=1976, ppid=1568, asprocess=0
[2021-03-13 23:14:46.390][Error][1976][764][11] srs is already running!(Resource temporarily unavailable)
[2021-03-13 23:14:46.390][Error][1976][764][11] Failed, code=1035 : run : daemon run master : acquire pid file : srs is already running
thread [1976][764]: do_main() [src/main/srs_main_server.cpp:186][errno=11]
thread [1976][764]: run() [src/main/srs_main_server.cpp:448][errno=11]
thread [1976][764]: run_master() [src/main/srs_main_server.cpp:467][errno=11]
thread [1976][764]: acquire_pid_file() [src/app/srs_app_server.cpp:824][errno=11](Resource temporarily unavailable)
注意上面的提示說我的服務器已經(jīng)在運行了,不能再運行了,我們把它給關閉了來:
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# sudo lsof -i:1935
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
srs 1601 root 8u IPv4 20067 0t0 TCP *:1935 (LISTEN)
通過查看srs的端口被一個進程給占用了,所以我們現(xiàn)在把這個進程給殺死掉來:
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# sudo kill -9 1601
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# ./objs/srs -c conf/rtmp.conf
[2021-03-13 23:25:49.235][Trace][2037][0] XCORE-SRS/3.0.156(OuXuli)
[2021-03-13 23:25:49.235][Trace][2037][0] config parse complete
[2021-03-13 23:25:49.235][Trace][2037][0] you can check log by: tail -f ./objs/srs.log (@see https://github.com/ossrs/srs/wiki/v1_CN_SrsLog)
[2021-03-13 23:25:49.235][Trace][2037][0] please check SRS by: ./etc/init.d/srs status
root@ubuntu:/home/txp/share/srs/srs.oschina/trunk# tail -f ./objs/srs.log
[2021-03-13 23:25:49.238][Trace][2037][0] start daemon mode...
[2021-03-13 23:25:49.239][Trace][2038][0] father process exit
[2021-03-13 23:25:49.239][Trace][2039][0] son(daemon) process running.
[2021-03-13 23:25:49.239][Trace][2039][0] st_init success, use epoll
[2021-03-13 23:25:49.239][Trace][2039][236] server main cid=236, pid=2039, ppid=1568, asprocess=0
[2021-03-13 23:25:49.240][Trace][2039][236] write pid=2039 to ./objs/srs.pid success!
[2021-03-13 23:25:49.240][Trace][2037][0] grandpa process exit.
[2021-03-13 23:25:49.240][Trace][2039][236] RTMP listen at tcp://0.0.0.0:1935, fd=8
[2021-03-13 23:25:49.240][Trace][2039][236] signal installed, reload=1, reopen=10, fast_quit=15, grace_quit=3
[2021-03-13 23:25:49.241][Trace][2039][236] http: api mount /console to ./objs/nginx/html/console
三、實戰(zhàn)演示推流和拉流操作:
1、使用ffmpeg進行推流和ffplay進行拉流:
所謂的推流,簡單理解就是把流媒體文件上傳到流媒體服務器上去;所以拉流,也可以簡單理解就是從流媒體服務器上獲取流媒體文件。下面我演示把一個流媒體文件格式為flv的視頻流上傳到srs上去:


E:\ffmpeg\bin>ffmpeg -re -i test.flv -vcodec copy -acodec copy -f flv -y rtmp://192.168.0.104/live/livestream
上傳上去后,我們可以使用ffplay來進行拉流:
E:\ffmpeg\bin>ffplay rtmp://192.168.0.104/live/livestream

注意:這里拉流的時候,你的虛擬機最好搞成橋接模式,和本地的物理機windows在同一個網(wǎng)段;而且上面192.168.0.104代表的是我安裝srs流媒體服務器的那臺ubunut機器的ip地址。

2、使用vlc來進行拉流:
vlc下載地址:
<!--StartFragment--><https://get./vlc/3.0.12/win64/vlc-3.0.12-win64.exe><!--EndFragment-->



3、使用OBS進行推流
OBS下載地址:
https://pc.qq.com/detail/4/detail_23604.html

下面是OBS推流演示(兩種方式,另外一種是使用電腦攝像頭來獲取):





