hs-m3u8 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hs_m3u8/__init__.py +3 -0
- hs_m3u8/main.py +340 -0
- hs_m3u8-0.1.0a1.dist-info/METADATA +65 -0
- hs_m3u8-0.1.0a1.dist-info/RECORD +5 -0
- hs_m3u8-0.1.0a1.dist-info/WHEEL +4 -0
hs_m3u8/__init__.py
ADDED
hs_m3u8/main.py
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"""
|
|
2
|
+
M3U8 下载器
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import platform
|
|
7
|
+
import posixpath
|
|
8
|
+
import shutil
|
|
9
|
+
import subprocess
|
|
10
|
+
from collections.abc import Callable
|
|
11
|
+
from enum import Enum, auto
|
|
12
|
+
from hashlib import md5
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
from urllib.parse import urljoin, urlparse
|
|
16
|
+
from zipfile import ZipFile
|
|
17
|
+
|
|
18
|
+
import m3u8
|
|
19
|
+
from hssp import Net
|
|
20
|
+
from hssp.utils import crypto
|
|
21
|
+
from loguru import logger
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_ffmpeg():
|
|
25
|
+
"""
|
|
26
|
+
根据平台不同获取不同的ffmpeg可执行文件
|
|
27
|
+
:return: FFmpeg 的可执行文件路径
|
|
28
|
+
"""
|
|
29
|
+
current_os = platform.system()
|
|
30
|
+
if current_os != "Windows":
|
|
31
|
+
return "ffmpeg"
|
|
32
|
+
|
|
33
|
+
res_path = Path(__file__).parent.parent.parent / "res"
|
|
34
|
+
ffmpeg_bin = res_path / "ffmpeg_win.exe"
|
|
35
|
+
|
|
36
|
+
if ffmpeg_bin.exists():
|
|
37
|
+
return str(ffmpeg_bin)
|
|
38
|
+
|
|
39
|
+
# ZIP 文件
|
|
40
|
+
ffmpeg_bin_zip = Path(ffmpeg_bin.parent) / f"{ffmpeg_bin.name}.zip"
|
|
41
|
+
if ffmpeg_bin_zip.exists():
|
|
42
|
+
# 解压缩到同一目录
|
|
43
|
+
with ZipFile(ffmpeg_bin_zip, "r") as zip_ref:
|
|
44
|
+
zip_ref.extractall(ffmpeg_bin.parent)
|
|
45
|
+
|
|
46
|
+
return ffmpeg_bin
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ContentType(Enum):
|
|
50
|
+
"""
|
|
51
|
+
获取URL数据的,类型枚举
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
Text = auto()
|
|
55
|
+
Json = auto()
|
|
56
|
+
Bytes = auto()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class M3u8Key:
|
|
60
|
+
"""
|
|
61
|
+
M3u8key
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def __init__(self, key: bytes, iv: str = None):
|
|
65
|
+
"""
|
|
66
|
+
:param key: 密钥
|
|
67
|
+
:param iv: 偏移
|
|
68
|
+
"""
|
|
69
|
+
self.key = key
|
|
70
|
+
self.iv = iv or key
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class M3u8Downloader:
|
|
74
|
+
"""
|
|
75
|
+
M3u8 异步下载器,并保留hls文件
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
retry_count: int = 0
|
|
79
|
+
retry_max_count: int = 50
|
|
80
|
+
ts_url_list: list = []
|
|
81
|
+
ts_path_list: list = []
|
|
82
|
+
ts_key: M3u8Key = None
|
|
83
|
+
m3u8_md5 = ""
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
m3u8_url: str,
|
|
88
|
+
save_path: str,
|
|
89
|
+
decrypt=False,
|
|
90
|
+
max_workers=None,
|
|
91
|
+
headers=None,
|
|
92
|
+
get_m3u8_func: Callable = None,
|
|
93
|
+
):
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
m3u8_url: m3u8 地址
|
|
98
|
+
save_path: 保存路径
|
|
99
|
+
decrypt: 如果ts被加密,是否解密ts
|
|
100
|
+
max_workers: 最大并发数
|
|
101
|
+
headers: 情求头
|
|
102
|
+
get_m3u8_func: 处理m3u8情求的回调函数。适用于m3u8地址不是真正的地址,
|
|
103
|
+
而是包含m3u8内容的情求,会把m3u8_url的响应传递给get_m3u8_func,要求返回真正的m3u8内容
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
sem = asyncio.Semaphore(max_workers) if max_workers else None
|
|
107
|
+
self.headers = headers
|
|
108
|
+
self.net = Net(sem=sem)
|
|
109
|
+
self.decrypt = decrypt
|
|
110
|
+
self.m3u8_url = urlparse(m3u8_url)
|
|
111
|
+
self.get_m3u8_func = get_m3u8_func
|
|
112
|
+
self.save_dir = Path(save_path) / "hls"
|
|
113
|
+
self.save_name = Path(save_path).name
|
|
114
|
+
self.key_path = self.save_dir / "key.key"
|
|
115
|
+
|
|
116
|
+
if not self.save_dir.exists():
|
|
117
|
+
self.save_dir.mkdir(parents=True)
|
|
118
|
+
|
|
119
|
+
logger.add(self.save_dir.parent / f"{self.save_name}.log")
|
|
120
|
+
self.logger = logger
|
|
121
|
+
|
|
122
|
+
async def run(self, merge=True, del_hls=False):
|
|
123
|
+
await self.start(merge, del_hls)
|
|
124
|
+
await self.net.close()
|
|
125
|
+
|
|
126
|
+
async def start(self, merge=True, del_hls=False):
|
|
127
|
+
"""
|
|
128
|
+
下载器启动函数
|
|
129
|
+
:param merge: ts下载完后是否合并,默认合并
|
|
130
|
+
:param del_hls: 是否删除hls系列文件,包括.m3u8文件、*.ts、.key文件
|
|
131
|
+
:return:
|
|
132
|
+
"""
|
|
133
|
+
mp4_path = self.save_dir.parent / f"{self.save_name}.mp4"
|
|
134
|
+
if Path(mp4_path).exists():
|
|
135
|
+
self.logger.info(f"{mp4_path}已存在")
|
|
136
|
+
if del_hls:
|
|
137
|
+
shutil.rmtree(str(self.save_dir))
|
|
138
|
+
return True
|
|
139
|
+
|
|
140
|
+
self.logger.info(
|
|
141
|
+
f"开始下载: 合并ts为mp4={merge}, "
|
|
142
|
+
f"删除hls信息={del_hls}, "
|
|
143
|
+
f"下载地址为:{self.m3u8_url.geturl()}. 保存路径为:{self.save_dir}"
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
await self._download()
|
|
147
|
+
self.logger.info("ts下载完成")
|
|
148
|
+
self.ts_path_list = [ts_path for ts_path in self.ts_path_list if ts_path]
|
|
149
|
+
count_1, count_2 = len(self.ts_url_list), len(self.ts_path_list)
|
|
150
|
+
self.logger.info(f"TS应下载数量为:{count_1}, 实际下载数量为:{count_2}")
|
|
151
|
+
if count_1 == 0 or count_2 == 0:
|
|
152
|
+
self.logger.error("ts数量为0,请检查!!!")
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
if count_2 != count_1:
|
|
156
|
+
self.logger.error(f"ts下载数量与实际数量不符合!!!应该下载数量为:{count_1}, 实际下载数量为:{count_2}")
|
|
157
|
+
self.logger.error(self.ts_url_list)
|
|
158
|
+
self.logger.error(self.ts_path_list)
|
|
159
|
+
if self.retry_count < self.retry_max_count:
|
|
160
|
+
self.retry_count += 1
|
|
161
|
+
self.logger.error(f"正在进行重试:{self.retry_count}/{self.retry_max_count}")
|
|
162
|
+
return self.start(merge, del_hls)
|
|
163
|
+
return False
|
|
164
|
+
|
|
165
|
+
if not merge:
|
|
166
|
+
return True
|
|
167
|
+
|
|
168
|
+
if self.merge():
|
|
169
|
+
self.logger.info("合并成功")
|
|
170
|
+
else:
|
|
171
|
+
self.logger.error(
|
|
172
|
+
f"mp4合并失败. ts应该下载数量为:{count_1}, 实际下载数量为:{count_2}. 保存路径为:{self.save_dir}"
|
|
173
|
+
)
|
|
174
|
+
return False
|
|
175
|
+
if del_hls:
|
|
176
|
+
shutil.rmtree(str(self.save_dir))
|
|
177
|
+
return True
|
|
178
|
+
|
|
179
|
+
async def _download(self):
|
|
180
|
+
"""
|
|
181
|
+
下载ts文件、m3u8文件、key文件
|
|
182
|
+
:return:
|
|
183
|
+
"""
|
|
184
|
+
self.ts_url_list = await self.get_ts_list(self.m3u8_url)
|
|
185
|
+
self.ts_path_list = [None] * len(self.ts_url_list)
|
|
186
|
+
await asyncio.gather(*[self._download_ts(url) for url in self.ts_url_list])
|
|
187
|
+
|
|
188
|
+
async def get_url_content(self, url: str, content_type: ContentType) -> bytes | str | Any:
|
|
189
|
+
"""
|
|
190
|
+
按照类型获取url内容
|
|
191
|
+
:param url: 请求地址
|
|
192
|
+
:param content_type: 内容类型
|
|
193
|
+
:return:
|
|
194
|
+
"""
|
|
195
|
+
data = None
|
|
196
|
+
try:
|
|
197
|
+
resp = await self.net.get(url, headers=self.headers)
|
|
198
|
+
if content_type == ContentType.Bytes:
|
|
199
|
+
data = resp.content
|
|
200
|
+
if content_type == ContentType.Text:
|
|
201
|
+
data = resp.text
|
|
202
|
+
if content_type == ContentType.Json:
|
|
203
|
+
data = resp.json
|
|
204
|
+
if resp.status_code != 200:
|
|
205
|
+
self.logger.error(f"请求{url}内容时返回码不正确,类型为:{content_type}, 返回码为:{resp.status_code}")
|
|
206
|
+
return None
|
|
207
|
+
except BaseException as exception:
|
|
208
|
+
self.logger.error(f"请求{url}内容时发生异常,类型为:{content_type}, 异常信息为:{exception}")
|
|
209
|
+
|
|
210
|
+
return data
|
|
211
|
+
|
|
212
|
+
async def get_ts_list(self, url) -> list[dict]:
|
|
213
|
+
"""
|
|
214
|
+
解析m3u8并保存至列表
|
|
215
|
+
:param url:
|
|
216
|
+
:return:
|
|
217
|
+
"""
|
|
218
|
+
resp = await self.net.get(url.geturl(), headers=self.headers)
|
|
219
|
+
m3u8_text = self.get_m3u8_func(resp.text) if self.get_m3u8_func else resp.text
|
|
220
|
+
m3u8_obj = m3u8.loads(m3u8_text)
|
|
221
|
+
prefix = f"{url.scheme}://{url.netloc}"
|
|
222
|
+
base_path = posixpath.normpath(url.path + "/..") + "/"
|
|
223
|
+
m3u8_obj.base_uri = urljoin(prefix, base_path)
|
|
224
|
+
|
|
225
|
+
# 解析多层m3u8, 默认选取比特率最高的
|
|
226
|
+
ts_url_list = []
|
|
227
|
+
if len(m3u8_obj.playlists) > 0:
|
|
228
|
+
bandwidth = 0
|
|
229
|
+
play_url = ""
|
|
230
|
+
self.logger.info("发现多个播放列表")
|
|
231
|
+
for playlist in m3u8_obj.playlists:
|
|
232
|
+
if int(playlist.stream_info.bandwidth) > bandwidth:
|
|
233
|
+
bandwidth = int(playlist.stream_info.bandwidth)
|
|
234
|
+
play_url = playlist.absolute_uri
|
|
235
|
+
self.logger.info(f"选择的播放地址:{play_url},比特率:{bandwidth}")
|
|
236
|
+
return await self.get_ts_list(urlparse(play_url))
|
|
237
|
+
|
|
238
|
+
# 遍历ts文件
|
|
239
|
+
for index, segments in enumerate(m3u8_obj.segments):
|
|
240
|
+
ts_uri = segments.uri if "http" in m3u8_obj.segments[index].uri else segments.absolute_uri
|
|
241
|
+
m3u8_obj.segments[index].uri = f"{index}.ts"
|
|
242
|
+
ts_url_list.append({"uri": ts_uri, "index": index})
|
|
243
|
+
|
|
244
|
+
# 保存解密key
|
|
245
|
+
if len(m3u8_obj.keys) > 0 and m3u8_obj.keys[0]:
|
|
246
|
+
resp = await self.net.get(m3u8_obj.keys[0].absolute_uri, headers=self.headers)
|
|
247
|
+
key_data = resp.content
|
|
248
|
+
self.save_file(key_data, self.key_path)
|
|
249
|
+
self.ts_key = M3u8Key(key=key_data, iv=m3u8_obj.keys[0].iv)
|
|
250
|
+
key = m3u8_obj.segments[0].key
|
|
251
|
+
key.uri = "key.key"
|
|
252
|
+
m3u8_obj.segments[0].key = key
|
|
253
|
+
|
|
254
|
+
# 导出m3u8文件
|
|
255
|
+
m3u8_text = m3u8_obj.dumps()
|
|
256
|
+
self.m3u8_md5 = md5(m3u8_text.encode("utf8"), usedforsecurity=False).hexdigest().lower()
|
|
257
|
+
self.save_file(m3u8_text, self.save_dir / f"{self.m3u8_md5}.m3u8")
|
|
258
|
+
self.logger.info("导出m3u8文件成功")
|
|
259
|
+
|
|
260
|
+
return ts_url_list
|
|
261
|
+
|
|
262
|
+
async def _download_ts(self, ts_item: dict):
|
|
263
|
+
"""
|
|
264
|
+
下载ts
|
|
265
|
+
:param ts_item: ts 数据
|
|
266
|
+
:return:
|
|
267
|
+
"""
|
|
268
|
+
index = ts_item["index"]
|
|
269
|
+
ts_uri = ts_item["uri"]
|
|
270
|
+
ts_path = self.save_dir / f"{index}.ts"
|
|
271
|
+
if Path(ts_path).exists():
|
|
272
|
+
self.ts_path_list[index] = str(ts_path)
|
|
273
|
+
return
|
|
274
|
+
resp = await self.net.get(ts_item["uri"])
|
|
275
|
+
ts_content = resp.content
|
|
276
|
+
if ts_content is None:
|
|
277
|
+
return
|
|
278
|
+
|
|
279
|
+
if self.ts_key and self.decrypt:
|
|
280
|
+
ts_content = crypto.decrypt_aes_256_cbc_pad7(ts_content, self.ts_key.key, self.ts_key.iv)
|
|
281
|
+
|
|
282
|
+
self.save_file(ts_content, ts_path)
|
|
283
|
+
self.logger.info(f"{ts_uri}下载成功")
|
|
284
|
+
self.ts_path_list[index] = str(ts_path)
|
|
285
|
+
|
|
286
|
+
def merge(self):
|
|
287
|
+
"""
|
|
288
|
+
合并ts文件为mp4文件
|
|
289
|
+
:return:
|
|
290
|
+
"""
|
|
291
|
+
self.logger.info("开始合并mp4")
|
|
292
|
+
if len(self.ts_path_list) != len(self.ts_url_list):
|
|
293
|
+
self.logger.error("数量不足拒绝合并!")
|
|
294
|
+
return False
|
|
295
|
+
|
|
296
|
+
# 整合后的ts文件路径
|
|
297
|
+
big_ts_path = self.save_dir.parent / f"{self.save_name}.ts"
|
|
298
|
+
if big_ts_path.exists():
|
|
299
|
+
big_ts_path.unlink()
|
|
300
|
+
|
|
301
|
+
# mp4路径
|
|
302
|
+
mp4_path = self.save_dir.parent / f"{self.save_name}.mp4"
|
|
303
|
+
|
|
304
|
+
# 把ts文件整合到一起
|
|
305
|
+
big_ts_file = big_ts_path.open("ab+")
|
|
306
|
+
for path in self.ts_path_list:
|
|
307
|
+
with open(path, "rb") as ts_file:
|
|
308
|
+
data = ts_file.read()
|
|
309
|
+
if self.ts_key:
|
|
310
|
+
data = crypto.decrypt_aes_256_cbc_pad7(data, self.ts_key.key, self.ts_key.iv)
|
|
311
|
+
big_ts_file.write(data)
|
|
312
|
+
big_ts_file.close()
|
|
313
|
+
self.logger.info("ts文件整合完毕")
|
|
314
|
+
|
|
315
|
+
# 把大的ts文件转换成mp4文件
|
|
316
|
+
ffmpeg_bin = get_ffmpeg()
|
|
317
|
+
command = (
|
|
318
|
+
f'{ffmpeg_bin} -i "{big_ts_path}" '
|
|
319
|
+
f'-c copy -map 0:v -map 0:a -bsf:a aac_adtstoasc -threads 32 "{mp4_path}" -y'
|
|
320
|
+
)
|
|
321
|
+
self.logger.info(f"ts整合成功,开始转为mp4。 command:{command}")
|
|
322
|
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
323
|
+
if result.returncode != 0:
|
|
324
|
+
logger.error(f"命令执行失败: {result.stderr or result.stdout}")
|
|
325
|
+
|
|
326
|
+
if Path(mp4_path).exists():
|
|
327
|
+
big_ts_path.unlink()
|
|
328
|
+
return Path(mp4_path).exists()
|
|
329
|
+
|
|
330
|
+
@staticmethod
|
|
331
|
+
def save_file(content: bytes | str, filepath):
|
|
332
|
+
"""
|
|
333
|
+
保存内容到文件
|
|
334
|
+
:param content: 内容
|
|
335
|
+
:param filepath: 文件路径
|
|
336
|
+
:return:
|
|
337
|
+
"""
|
|
338
|
+
mode = "wb" if isinstance(content, bytes) else "w"
|
|
339
|
+
with open(file=filepath, mode=mode) as file:
|
|
340
|
+
file.write(content)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: hs-m3u8
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: m3u8 下载器
|
|
5
|
+
Author-email: 昊色居士 <xhrtxh@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
11
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
12
|
+
Classifier: Operating System :: POSIX :: BSD
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: hssp>=0.4.4
|
|
21
|
+
Requires-Dist: m3u8>=6.0.0
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# hs-m3u8
|
|
25
|
+
|
|
26
|
+
m3u8 视频下载工具。支持大部分的m3u8视频下载。后续增加UI界面。
|
|
27
|
+
|
|
28
|
+
## 功能
|
|
29
|
+
|
|
30
|
+
- aes解密
|
|
31
|
+
- 自动选择高分辨m3u8
|
|
32
|
+
- 合并MP4
|
|
33
|
+
- 可选择保留ts文件
|
|
34
|
+
- 内置Windows平台ffmpeg可执行文件(由于Linux及Mac下权限问题,需自行安装ffmpeg文件)
|
|
35
|
+
|
|
36
|
+
## 使用
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
url = "https://surrit.com/6d3bb2b2-d707-4b79-adf0-89542cb1383c/playlist.m3u8"
|
|
40
|
+
name = "SDAB-129"
|
|
41
|
+
dl = M3u8Downloader(
|
|
42
|
+
url=url,
|
|
43
|
+
save_path=f"downloads/{name}",
|
|
44
|
+
max_workers=64
|
|
45
|
+
)
|
|
46
|
+
await dl.run(del_hls=False, merge=True)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- del_hls 为True时会删除ts、m3u8、key等文件,否则会经过处理后保留,以便直接使用
|
|
50
|
+
- merge 为True时会自动合并为mp4
|
|
51
|
+
|
|
52
|
+
## 安装
|
|
53
|
+
|
|
54
|
+
### rye 安装
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
rye sync
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### pip 安装
|
|
61
|
+
该`requirements.lock`文件是在Mac环境在生成的,不同系统环境下可能会遇到不同的效果,如果使用请使用`rye`安装
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install -r requirements.lock
|
|
65
|
+
```
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
hs_m3u8/__init__.py,sha256=fkbkVM4g2rTay79jUNRfYahwfdLP36VoaSm4wsbkKXk,65
|
|
2
|
+
hs_m3u8/main.py,sha256=tq-JtMzfEy1YTTpZFL9Oas5ZzrON3eigWxlKAduc-oU,11592
|
|
3
|
+
hs_m3u8-0.1.0a1.dist-info/METADATA,sha256=8UM-qjVIreTD9wU2fJwn56qUcIGlIIHYlv4w50J3mwE,1819
|
|
4
|
+
hs_m3u8-0.1.0a1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
5
|
+
hs_m3u8-0.1.0a1.dist-info/RECORD,,
|