9步實現 Docker部署 SRS rtmp/flv流媒體服務器


這是基於centos7.6系統部署的
一、docker鏡像用docker-hub官方的:

docker pull ossrs/srs:3

二、創建 bridge 網絡並指定 IP 區間:

#創建自定義網絡
docker network create --driver bridge --subnet 172.0.0.0/16 srs_network
 
#查看已存在網絡
docker network ls

三、創建數據目錄

# 創建數據目錄
mkdir -p /home/docker/srs3

四、安裝並啟動SRS的容器

# 安裝並啟動 srs
docker run -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
ossrs/srs:3

運行容器會直接停留在容器運行界面,通過Ctrl+P+Q可后台形式退出容器

這時候其實已經運行成功!可以通過以下命令查看SRS控制台

# SRS 控制台訪問地址 IP 為服務器ip,
http://[IP]:8080

五、步驟四會安裝在默認的位置,我們把它復制到步驟三創建的目錄下

# 把容器中的配置文件復制出來
docker cp -a srs:/usr/local/srs/conf /home/docker/srs3/conf

# 把容器中的數據文件復制出來
docker cp -a srs:/usr/local/srs/objs /home/docker/srs3/objs

# 刪除 srs 容器
docker rm -f srs

進入/home/docker/srs3 可查看配置文件是否復制成功

六、重新啟動SRS(啟動復制到/home/docker/srs3目錄的SRS)

docker run -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
--network srs_network \
--ip 172.0.0.35 \
--restart=always \
-v /home/docker/srs3/conf/:/usr/local/srs/conf/ \
-v /home/docker/srs3/objs/:/usr/local/srs/objs/ \
ossrs/srs:3

這是可能會報錯:

docker: Error response from daemon: Conflict. The container name "/srs" is already in use by container "4fbdca724d84c1398b4c54cbe032dfc64baf84546f1c06d07014eec0249eeee6". You have to remove (or rename) that container to be able to reuse that name.

因為步驟四我們意見運行一個容器,已經使用過srs這個名字了,我們可以換一個名字,或者把剛才那個容器刪除。

運行容器會直接停留在容器運行界面,通過Ctrl+P+Q可后台形式退出容器

七、查看服務

# 查看srs服務是否啟動成功
docker ps

--------
[root@qtouch-prd-test conf]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS                                                                    NAMES
2b141ebf6cc8   ossrs/srs:3   "./objs/srs -c conf/…"   13 minutes ago   Up 13 minutes   0.0.0.0:1935->1935/tcp, 0.0.0.0:1985->1985/tcp, 0.0.0.0:8080->8080/tcp   srs

要想外網可以訪問,需要打開1935、8080、1985 三個端口號(雲服務安全組)

此時流服務器已經可以使用,如果需要自定義配置文件可繼續按照下面步驟。

八、自定義配置文件(僅供參考:具體去看官方配置文檔 https://github.com/ossrs/srs/wiki/v3_CN_Home
再 /home/docker/srs3/conf 創建自己的配置文件 srs.my.conf

# SRS 參考配置

listen              1935;
max_connections     1000;
srs_log_tank        file;
srs_log_file        ./objs/srs.log;
daemon              on;
http_api {
    enabled         on;
    listen          1985;
}
http_server {
    enabled         on;
    listen          8080;
    dir             ./objs/nginx/html;
	# 開啟 https 支持,需要開放 8088端口
	# https {
        # enabled on;
        # listen 8088;
        # key ./conf/xxxx.key;
        # cert ./conf/xxxx.crt;
    # }
}
vhost __defaultVhost__ {
    # http-flv設置
    http_remux{
        enabled    on;
        mount      [vhost]/[app]/[stream].flv;
        hstrs      on;
    }
 
    # hls設置
    hls {
        enabled         on;
        hls_fragment    1;
        hls_window      2;
        hls_path        ./objs/nginx/html;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[stream]-[seq].ts;
    }
	
	# dvr設置
	dvr {
        enabled             off;
        dvr_path            ./objs/nginx/html/[app]/[stream]/[2006]/[01]/[02]/[timestamp].flv;
        dvr_plan            segment;
        dvr_duration        30;
        dvr_wait_keyframe   on;
    }
	
	# rtc 設置
	rtc {
		enabled     on;
		bframe      discard;
    }
	
	# SRS支持refer防盜鏈:檢查用戶從哪個網站過來的。譬如不是從公司的頁面過來的人都不讓看。
    refer {
        # whether enable the refer hotlink-denial.
        # default: off.
        enabled         off;
        # the common refer for play and publish.
        # if the page url of client not in the refer, access denied.
        # if not specified this field, allow all.
        # default: not specified.
        all           github.com github.io;
        # refer for publish clients specified.
        # the common refer is not overrided by this.
        # if not specified this field, allow all.
        # default: not specified.
        publish   github.com github.io;
        # refer for play clients specified.
        # the common refer is not overrided by this.
        # if not specified this field, allow all.
        # default: not specified.
        play      github.com github.io;
    }
	
	# http 回調
	http_hooks {
	
		# 事件:發生該事件時,即回調指定的HTTP地址。
		# HTTP地址:可以支持多個,以空格分隔,SRS會依次回調這些接口。
		# 數據:SRS將數據POST到HTTP接口。
		# 返回值:SRS要求HTTP服務器返回HTTP200並且response內容為整數錯誤碼(0表示成功),其他錯誤碼會斷開客戶端連接。
		
        # whether the http hooks enable.
        # default off.
        enabled         on;
        
		# 當客戶端連接到指定的vhost和app時
        on_connect      http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients;
        
		# 當客戶端關閉連接,或者SRS主動關閉連接時
        on_close        http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients;
       
		# 當客戶端發布流時,譬如flash/FMLE方式推流到服務器
        on_publish      http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams;
        
		# 當客戶端停止發布流時
        on_unpublish    http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams;
        
		# 當客戶端開始播放流時
        on_play         http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
        
		# 當客戶端停止播放時。備注:停止播放可能不會關閉連接,還能再繼續播放。
        on_stop         http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
        
		# 當DVR錄制關閉一個flv文件時
        on_dvr          http://127.0.0.1:8085/api/v1/dvrs http://localhost:8085/api/v1/dvrs;
		
        # 當HLS生成一個ts文件時
        on_hls          http://127.0.0.1:8085/api/v1/hls http://localhost:8085/api/v1/hls;
		
        # when srs reap a ts file of hls, call this hook,
        on_hls_notify   http://127.0.0.1:8085/api/v1/hls/[app]/[stream]/[ts_url][param];
    }
}

九、通過自定義配置文件啟動

docker run -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
--name srs \
--network src_network \
--ip 172.0.0.35 \
--restart=always \
-v /home/docker/srs3/conf/:/usr/local/srs/conf/ \
-v /home/docker/srs3/objs/:/usr/local/srs/objs/ \
ossrs/srs:3 \
./objs/srs -c conf/srs.my.conf

十、在自己電腦安裝配置FFmpeg(步驟很簡單,下載下來,配置個環境變量就行),把局域網的攝像頭推流到雲服務上部署的流媒體服務器

ffmpeg -i "rtsp://admin:sit123456@192.168.1.251:554/cam/realmonitor?channel=1&subtype=0" -g 25 -vcodec libx264  -preset:v ultrafast -tune:v zerolatency -c copy -f flv -y rtmp://[雲服務ip]/live/livestream

使用vlc播放器播放

http://[雲服務ip]:8080/live/livestream.flv


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM