-
Notifications
You must be signed in to change notification settings - Fork 66
/
live_recorder.py
553 lines (504 loc) · 21.8 KB
/
live_recorder.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import asyncio
import json
import os
import re
import time
import uuid
from http.cookies import SimpleCookie
from pathlib import Path
from typing import Dict, Tuple, Union
from urllib.parse import parse_qs
import anyio
import ffmpeg
import httpx
import jsengine
import streamlink
from httpx_socks import AsyncProxyTransport
from jsonpath_ng.ext import parse
from loguru import logger
from streamlink.options import Options
from streamlink.stream import StreamIO, HTTPStream, HLSStream
from streamlink_cli.main import open_stream
from streamlink_cli.output import FileOutput
from streamlink_cli.streamrunner import StreamRunner
recording: Dict[str, Tuple[StreamIO, FileOutput]] = {}
class LiveRecoder:
def __init__(self, config: dict, user: dict):
self.id = user['id']
platform = user['platform']
name = user.get('name', self.id)
self.flag = f'[{platform}][{name}]'
self.interval = user.get('interval', 10)
self.crypto_js_url = user.get('crypto_js_url', '')
self.headers = user.get('headers', {'User-Agent': 'Chrome'})
self.cookies = user.get('cookies')
self.format = user.get('format')
self.proxy = user.get('proxy', config.get('proxy'))
self.output = user.get('output', config.get('output', 'output'))
if not self.crypto_js_url:
self.crypto_js_url = 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js'
self.get_cookies()
self.client = self.get_client()
async def start(self):
self.ssl = True
self.mState = 0
while True:
try:
logger.info(f'{self.flag}正在检测直播状态')
logger.info(f'预配置刷新间隔:{self.interval}s')
try:
await self.run()
except Exception as run_error:
logger.error(f"{self.flag}直播检测内部错误\n{repr(run_error)}")
state = self.mState
timeI = self.interval
if state == '1':
timeI = 2
logger.info(f'->直播状态:{state} 实际刷新间隔:{timeI}s')
await asyncio.sleep(timeI)
except ConnectionError as error:
if '直播检测请求协议错误' not in str(error):
logger.error(error)
await self.client.aclose()
self.client = self.get_client()
except Exception as error:
logger.exception(f'{self.flag}直播检测错误\n{repr(error)}')
async def run(self):
pass
async def request(self, method, url, **kwargs):
try:
response = await self.client.request(method, url, **kwargs)
return response
except httpx.ProtocolError as error:
raise ConnectionError(f'{self.flag}直播检测请求协议错误\n{error}')
except httpx.HTTPStatusError as error:
raise ConnectionError(
f'{self.flag}直播检测请求状态码错误\n{error}\n{response.text}')
except anyio.EndOfStream as error:
raise ConnectionError(f'{self.flag}直播检测代理错误\n{error}')
except httpx.HTTPError as error:
logger.error(f'网络异常 重试...')
raise ConnectionError(f'{self.flag}直播检测请求错误\n{repr(error)}')
def get_client(self):
client_kwargs = {
'http2': True,
'timeout': self.interval,
'limits': httpx.Limits(max_keepalive_connections=100, keepalive_expiry=self.interval * 2),
'headers': self.headers,
'cookies': self.cookies
}
# 检查是否有设置代理
if self.proxy:
if 'socks' in self.proxy:
client_kwargs['transport'] = AsyncProxyTransport.from_url(self.proxy)
else:
client_kwargs['proxies'] = self.proxy
return httpx.AsyncClient(**client_kwargs)
def get_cookies(self):
if self.cookies:
cookies = SimpleCookie()
cookies.load(self.cookies)
self.cookies = {k: v.value for k, v in cookies.items()}
def get_filename(self, title, format):
live_time = time.strftime('%Y.%m.%d %H.%M.%S')
# 文件名特殊字符转换为全角字符
char_dict = {
'"': '"',
'*': '*',
':': ':',
'<': '<',
'>': '>',
'?': '?',
'/': '/',
'\\': '\',
'|': '|'
}
for half, full in char_dict.items():
title = title.replace(half, full)
filename = f'[{live_time}]{self.flag}{title[:50]}.{format}'
return filename
def get_streamlink(self):
session = streamlink.session.Streamlink({
'stream-segment-timeout': 60,
'hls-segment-queue-threshold': 10
})
ssl = self.ssl
logger.info(f'是否验证SSL:{ssl}')
session.set_option('http-ssl-verify', ssl)
# 添加streamlink的http相关选项
if proxy := self.proxy:
# 代理为socks5时,streamlink的代理参数需要改为socks5h,防止部分直播源获取失败
if 'socks' in proxy:
proxy = proxy.replace('://', 'h://')
session.set_option('http-proxy', proxy)
if self.headers:
session.set_option('http-headers', self.headers)
if self.cookies:
session.set_option('http-cookies', self.cookies)
return session
def run_record(self, stream: Union[StreamIO, HTTPStream], url, title, format):
# 获取输出文件名
filename = self.get_filename(title, format)
if stream:
logger.info(f'{self.flag}开始录制:{filename}')
# 调用streamlink录制直播
result = self.stream_writer(stream, url, filename)
# 录制成功、format配置存在且不等于直播平台默认格式时运行ffmpeg封装
if result and self.format and self.format != format:
self.run_ffmpeg(filename, format)
recording.pop(url, None)
logger.info(f'{self.flag}停止录制:{filename}')
else:
logger.error(f'{self.flag}无可用直播源:{filename}')
def stream_writer(self, stream, url, filename):
logger.info(f'{self.flag}获取到直播流链接:{filename}\n{stream.url}')
output = FileOutput(Path(f'{self.output}/{filename}'))
try:
stream_fd, prebuffer = open_stream(stream)
output.open()
recording[url] = (stream_fd, output)
logger.info(f'{self.flag}正在录制:{filename}')
StreamRunner(stream_fd, output, show_progress=True).run(prebuffer)
return True
except Exception as error:
if 'timeout' in str(error):
logger.warning(f'{self.flag}直播录制超时,请检查主播是否正常开播或网络连接是否正常:{filename}\n{error}')
elif re.search(f'SSL: CERTIFICATE_VERIFY_FAILED', str(error)):
logger.warning(f'{self.flag}SSL错误,将取消SSL验证:{filename}\n{error}')
self.ssl = False
elif re.search(f'(Unable to open URL|No data returned from stream)', str(error)):
logger.warning(f'{self.flag}直播流打开错误,请检查主播是否正常开播:{filename}\n{error}')
else:
logger.exception(f'{self.flag}直播录制错误:{filename}\n{error}')
finally:
output.close()
def run_ffmpeg(self, filename, format):
logger.info(f'{self.flag}开始ffmpeg封装:{filename}')
new_filename = filename.replace(f'.{format}', f'.{self.format}')
ffmpeg.input(f'{self.output}/{filename}').output(
f'{self.output}/{new_filename}',
codec='copy',
map_metadata='-1',
movflags='faststart'
).global_args('-hide_banner').run()
os.remove(f'{self.output}/{filename}')
class Bilibili(LiveRecoder):
async def run(self):
url = f'https://live.bilibili.com/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url='https://api.live.bilibili.com/room/v1/Room/get_info',
params={'room_id': self.id}
)).json()
if response['data']['live_status'] == 1:
title = response['data']['title']
stream = self.get_streamlink().streams(url).get('best') # HTTPStream[flv]
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')
class Douyu(LiveRecoder):
async def run(self):
url = f'https://www.douyu.com/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=f'https://open.douyucdn.cn/api/RoomApi/room/{self.id}',
)).json()
state = response['data']['room_status']
self.mState = state
logger.info(
f'直播状态[1已开播,2未开播]:{state} 上一次开播时间:{response["data"]["start_time"]}')
if state == '1':
liveUrl = await self.get_live()
if liveUrl != '':
title = response['data']['room_name']
stream = HTTPStream(
self.get_streamlink(),
liveUrl
) # HTTPStream[flv]
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')
else:
self.ssl = True
async def get_js(self):
response = (await self.request(
method='POST',
url=f'https://www.douyu.com/swf_api/homeH5Enc?rids={self.id}'
)).json()
js_enc = response['data'][f'room{self.id}']
getUrl = self.crypto_js_url
crypto_js = (await self.request(
method='GET',
url= getUrl
)).text
return jsengine.JSEngine(js_enc + crypto_js)
async def get_live(self):
did = uuid.uuid4().hex
tt = str(int(time.time()))
params = {
'cdn': 'tct-h5',
'did': did,
'tt': tt,
'rate': 0
}
js = await self.get_js()
query = js.call('ub98484234', self.id, did, tt)
params.update({k: v[0] for k, v in parse_qs(query).items()})
response = (await self.request(
method='POST',
url=f'https://www.douyu.com/lapi/live/getH5Play/{self.id}',
params=params
)).json()
if response['data'] == '' and response['msg'] != '':
logger.info(f'直播状态:{response["error"]} {response["msg"]}')
return ''
return f"{response['data']['rtmp_url']}/{response['data']['rtmp_live']}"
class Huya(LiveRecoder):
async def run(self):
url = f'https://www.huya.com/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=url
)).text
if '"isOn":true' in response:
title = re.search('"introduction":"(.*?)"', response).group(1)
stream = self.get_streamlink().streams(url).get('best') # HTTPStream[flv]
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')
class Douyin(LiveRecoder):
async def run(self):
url = f'https://live.douyin.com/{self.id}'
if url not in recording:
if not self.client.cookies:
await self.client.get(url='https://live.douyin.com/') # 获取ttwid
response = (await self.request(
method='GET',
url='https://live.douyin.com/webcast/room/web/enter/',
params={
'aid': 6383,
'device_platform': 'web',
'browser_language': 'zh-CN',
'browser_platform': 'Win32',
'browser_name': 'Chrome',
'browser_version': '100.0.0.0',
'web_rid': self.id
},
)).json()
if data := response['data']['data']:
data = data[0]
if data['status'] == 2:
title = data['title']
live_url = ''
stream_data = json.loads(data['stream_url']['live_core_sdk_data']['pull_data']['stream_data'])
for quality_code in ('origin', 'uhd', 'hd', 'sd', 'md', 'ld'):
if quality_data := stream_data['data'].get(quality_code):
live_url = quality_data['main']['flv']
break
stream = HTTPStream(
self.get_streamlink(),
live_url
) # HTTPStream[flv]
await asyncio.to_thread(self.run_record, stream, url, title, 'flv')
class Youtube(LiveRecoder):
async def run(self):
response = (await self.request(
method='POST',
url='https://www.youtube.com/youtubei/v1/browse',
params={
'key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8',
'prettyPrint': False
},
json={
'context': {
'client': {
'hl': 'zh-CN',
'clientName': 'MWEB',
'clientVersion': '2.20230101.00.00',
'timeZone': 'Asia/Shanghai'
}
},
'browseId': self.id,
'params': 'EgdzdHJlYW1z8gYECgJ6AA%3D%3D'
}
)).json()
jsonpath = parse('$..videoWithContextRenderer').find(response)
for match in jsonpath:
video = match.value
if '"style": "LIVE"' in json.dumps(video):
url = f"https://www.youtube.com/watch?v={video['videoId']}"
title = video['headline']['runs'][0]['text']
if url not in recording:
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
# FIXME:多开直播间中断
asyncio.create_task(asyncio.to_thread(self.run_record, stream, url, title, 'ts'))
class Twitch(LiveRecoder):
async def run(self):
url = f'https://www.twitch.tv/{self.id}'
if url not in recording:
response = (await self.request(
method='POST',
url='https://gql.twitch.tv/gql',
headers={'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko'},
json=[{
'operationName': 'StreamMetadata',
'variables': {'channelLogin': self.id},
'extensions': {
'persistedQuery': {
'version': 1,
'sha256Hash': 'a647c2a13599e5991e175155f798ca7f1ecddde73f7f341f39009c14dbf59962'
}
}
}]
)).json()
if response[0]['data']['user']['stream']:
title = response[0]['data']['user']['lastBroadcast']['title']
options = Options()
options.set('disable-ads', True)
stream = self.get_streamlink().streams(url, options).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Niconico(LiveRecoder):
async def run(self):
url = f'https://live.nicovideo.jp/watch/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=url
)).text
if '"content_status":"ON_AIR"' in response:
title = json.loads(
re.search(r'<script type="application/ld\+json">(.*?)</script>', response).group(1)
)['name']
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Twitcasting(LiveRecoder):
async def run(self):
url = f'https://twitcasting.tv/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url='https://twitcasting.tv/streamserver.php',
params={
'target': self.id,
'mode': 'client'
}
)).json()
if response:
response = (await self.request(
method='GET',
url=url
)).text
title = re.search('<meta name="twitter:title" content="(.*?)">', response).group(1)
stream = self.get_streamlink().streams(url).get('best') # Stream[mp4]
await asyncio.to_thread(self.run_record, stream, url, title, 'mp4')
class Afreeca(LiveRecoder):
async def run(self):
url = f'https://play.afreecatv.com/{self.id}'
if url not in recording:
response = (await self.request(
method='POST',
url='https://live.afreecatv.com/afreeca/player_live_api.php',
data={'bid': self.id}
)).json()
if response['CHANNEL']['RESULT'] != 0:
title = response['CHANNEL']['TITLE']
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Pandalive(LiveRecoder):
async def run(self):
url = f'https://www.pandalive.co.kr/live/play/{self.id}'
if url not in recording:
response = (await self.request(
method='POST',
url='https://api.pandalive.co.kr/v1/live/play',
headers={
'x-device-info': '{"t":"webMobile","v":"1.0","ui":0}'
},
data={
'action': 'watch',
'userId': self.id
}
)).json()
if response['result']:
title = response['media']['title']
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Bigolive(LiveRecoder):
async def run(self):
url = f'https://www.bigo.tv/cn/{self.id}'
if url not in recording:
response = (await self.request(
method='POST',
url='https://ta.bigo.tv/official_website/studio/getInternalStudioInfo',
params={'siteId': self.id}
)).json()
if response['data']['alive']:
title = response['data']['roomTopic']
stream = HLSStream(
session=self.get_streamlink(),
url=response['data']['hls_src']
) # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Pixivsketch(LiveRecoder):
async def run(self):
url = f'https://sketch.pixiv.net/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=url
)).text
next_data = json.loads(re.search(r'<script id="__NEXT_DATA__".*?>(.*?)</script>', response)[1])
initial_state = json.loads(next_data['props']['pageProps']['initialState'])
if lives := initial_state['live']['lives']:
live = list(lives.values())[0]
title = live['name']
streams = HLSStream.parse_variant_playlist(
session=self.get_streamlink(),
url=live['owner']['hls_movie']
)
stream = list(streams.values())[0] # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
class Chaturbate(LiveRecoder):
async def run(self):
url = f'https://chaturbate.com/{self.id}'
if url not in recording:
response = (await self.request(
method='POST',
url='https://chaturbate.com/get_edge_hls_url_ajax/',
headers={
'X-Requested-With': 'XMLHttpRequest'
},
data={
'room_slug': self.id
}
)).json()
if response['room_status'] == 'public':
title = self.id
streams = HLSStream.parse_variant_playlist(
session=self.get_streamlink(),
url=response['url']
)
stream = list(streams.values())[2]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')
async def run():
with open('config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
try:
tasks = []
for item in config['user']:
platform_class = globals()[item['platform']]
coro = platform_class(config, item).start()
tasks.append(asyncio.create_task(coro))
await asyncio.wait(tasks)
except (asyncio.CancelledError, KeyboardInterrupt, SystemExit):
logger.warning('用户中断录制,正在关闭直播流')
for stream_fd, output in recording.copy().values():
stream_fd.close()
output.close()
if __name__ == '__main__':
logger.add(
sink='logs/log_{time:YYYY-MM-DD}.log',
rotation='00:00',
retention='3 days',
level='INFO',
encoding='utf-8',
format='[{time:YYYY-MM-DD HH:mm:ss}][{level}][{name}][{function}:{line}]{message}'
)
asyncio.run(run())