m3u8 包含了若干个 ts 文件的名称,按播放顺序有序排列,还包括版本、是否加密等信息
网站上直接 F12,看一下网络,里面有 m3u8 文件,直接复制 url 到浏览器就能下载,还有个 enc.key

image.png
image.png

下载下来之后是这样的

image.png
image.png

把所有的 .ts 文件提取出来,合成每个 .ts 文件的 url 存在一个 .txt 中

1
2
3
4
5
6
7
8
9
10
11
12
url = 'url地址,自己替换'
f = open("index.m3u8", encoding='gb18030', errors='ignore')
lines = f.readlines()
new=[]
for line in lines:
if '.ts' in line:
line = url + line;
new.append(line)
file_out=open('out.txt','w', encoding='gb18030', errors='ignore')
for line in new:
file_out.writelines(line)
file_out.close()

使用 wget 下载 url 中存放的 .ts 文件

1
wget --content-disposition --trust-server-names -i out.txt

用脚本把 .m3u8 中的文件改成本地的文件位置(前面加个 ./)

1
2
3
4
5
6
7
8
9
10
11
f = open("./index.m3u8", encoding='gb18030', errors='ignore')
lines = f.readlines()
new=[]
for line in lines:
if '.ts' in line:
line = './' + line;
new.append(line)
file_out=open('out.m3u8','w', encoding='gb18030', errors='ignore')
for line in new:
file_out.writelines(line)
file_out.close()
image.png
image.png

然后用 ffmpeg 直接输出就行了

1
ffmpeg -allowed_extensions ALL -i out.m3u8 -c copy out.mp4
image.png
image.png

综合脚本

用的 wsl(kali),需要安装 ffmpeg 和 wget

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
import os
import time
import requests

url = 'http://127.0.0.1/videos/f5ac593aa49b944b15916585edbe0cbd/'
urlm3u8 = 'http://127.0.0.1/videos/f5ac593aa49b944b15916585edbe0cbd/f5ac593aa49b944b15916585edbe0cbd.m3u8'
urlkey = url + 'enc.key'

############# 下载m3u8与key文件 #############
r = requests.get(urlm3u8)
with open("index.m3u8",'wb') as f:
f.write(r.content)

r = requests.get(urlkey)
with open("enc.key",'wb') as f:
f.write(r.content)
time.sleep(1)

############# 提取m3u8中的ts文件url到out.txt #############
f = open("index.m3u8", encoding='gb18030', errors='ignore')
lines = f.readlines()
new=[]
for line in lines:
if '.ts' in line:
line = url + line;
new.append(line)
file_out=open('out.txt','w', encoding='gb18030', errors='ignore')
for line in new:
file_out.writelines(line)
file_out.close()
time.sleep(1)

############# 下载out.txt中的ts文件 #############
os.system("wget --content-disposition --trust-server-names -i out.txt")
time.sleep(1)

############# 把m3u8中的ts、key文件路径改为本地 #############
f = open("./index.m3u8", encoding='gb18030', errors='ignore')
lines = f.readlines()
new=[]
for line in lines:
if 'enc.key' in line:
temp1=line[0:31]
temp2=line[85:132]
line = temp1 + './' + temp2
if '.ts' in line:
line = './' + line;
new.append(line)
file_out=open('out.m3u8','w', encoding='gb18030', errors='ignore')
for line in new:
file_out.writelines(line)
file_out.close()
time.sleep(1)

############# 使用ffmpeg解密合并为out.mp4 #############
os.system("ffmpeg -allowed_extensions ALL -i out.m3u8 -c copy out.mp4")