Trail of Explore🚲.

CloudFlare Tunnel 搭建直播推流拉流

Word count: 930Reading time: 5 min
2023/09/14

cf tunnel 推流流程

tunnel服务

hls HEALTHY hls.scboot.icu 13 minutes 2023年9月14日

stream HEALTHY stream.scboot.icu 20 minutes 2023年9月14日

stream

1
2
3
4
5
6
7
8
9
10
11
# 隧道的 UUID, 就是登录凭证的json文件名称
tunnel: uuid
# 鉴权文件的全路径,注意替换为自己的
credentials-file: C:\Users\XX\.cloudflared\uuid.json

ingress:
# 你的freenom二级域名
- hostname: stream.scboot.icu
service: rtmp://192.168.132.107:1935 #wsl的rtmp服务,通过宿主机转发给wsl
# 默认错误404
- service: http_status:404

hls

1
2
3
4
5
6
7
8
9
10
11
# 隧道的 UUID, 就是登录凭证的json文件名称
tunnel: uuid
# 鉴权文件的全路径,注意替换为自己的
credentials-file: C:\Users\XX\.cloudflared\uuid.json

ingress:
# 你的freenom二级域名
- hostname: hls.scboot.icu
service: http://192.168.132.107:80 # 同样转发给wsl,这里是转发给nginx的普通http服务,用来获取ts和m3u8文件
# 默认错误404
- service: http_status:404

配置好后,启动两个tunnel 服务

1
2
cloudflared --config=./config_stream.yaml tunnel run
cloudflared --config=./config_hls.yaml tunnel run

问题

本来配置到这,看着是没什么问题了,用ffmpeg推流试下,结果🤯🤯
ffmpeg -re -stream_loop -1 -i coaster.mp4 -vcodec copy -acodec copy -f flv rtmp://stream.scboot.icu/live/cf

1
2
3
[tcp @ 0x55ec7befb9c0] Connection to tcp://stream.scboot.icu:1935 failed: Connection timed out
[rtmp @ 0x55ec7befb000] Cannot open connection tcp://stream.scboot.icu:1935
rtmp://stream.scboot.icu/live/cf: Connection timed out

出了这么个错,再想下,如果在本地再转发一次行不行
宿主机运行
cloudflared access tcp --hostname stream.scboot.icu --url tcp://10.10.22.46:11935 --loglevel debug

wsl运行
ffmpeg -re -stream_loop -1 -i coaster.mp4 -vcodec copy -acodec copy -f flv rtmp://10.10.22.46:11935/live/cf

简单来说,就是把rtmp的字节流先推到10.10.22.46:11935,之后再转发到stream.scboot.icu

好像可以哦,看输出,/tmp/hls 目录也出现了ts文件
frame= 154 fps= 31 q=-1.0 size= 8648kB time=00:00:05.03 bitrate=14058.7kbits/s speed= 1x

cf tunnel 拉流流程


访问https://hls.scboot.icu/hls/cf.m3u8获取m3u8文件

验证

打开VLC media player的网络串流,输入地址https://hls.scboot.icu/hls/cf.m3u8

这速度真的,🥱🥱🥱🥱
玩玩可以,拿来用还是自己买个服务器吧

1
2
3
4
5
6
7
8
9
10
11
root@DESKTOP-OMVTVB8:/tmp/hls# tail -f /usr/local/nginx/logs/access.log
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 119 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 119 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 119 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 PUBLISH "live" "cf" "" - 55814150 409 "" "FMLE/3.0 (compatible; Lavf58.29" (11m 15s)
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 142 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 142 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf-0.ts HTTP/1.1" 200 39506884 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 142 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf-1.ts HTTP/1.1" 200 17445648 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"
192.168.128.1 - - "GET /hls/cf.m3u8 HTTP/1.1" 200 142 "-" "VLC/3.0.17.4 LibVLC/3.0.17.4"

最后贴下nginx的配置

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
56
#user  nobody;
worker_processes 2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}

rtmp {
server {
listen 1935;
application live {
live on;
interleave on;

hls on;
hls_path /tmp/hls;
hls_fragment 15s;
hls_cleanup off;
}
}
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

sendfile on;
keepalive_timeout 65;

server {
listen 80 default_server;
server_name _;

location / {
root /tmp;
}

types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
text/html html;
}
}
}
CATALOG
  1. 1. cf tunnel 推流流程
    1. 1.1. tunnel服务
      1. 1.1.1. stream
      2. 1.1.2. hls
    2. 1.2. 问题
  2. 2. cf tunnel 拉流流程
    1. 2.1. 验证