Skip to content

Commit

Permalink
Add bilibili live record
Browse files Browse the repository at this point in the history
  • Loading branch information
ihmily committed Aug 14, 2023
1 parent 8e8b20a commit ebfad74
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 19 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [x] 虎牙
- [x] 斗鱼
- [x] YY
- [x] B站
- [ ] 更多平台正在更新中

</div>
Expand Down Expand Up @@ -47,12 +48,10 @@
- 注意事项③ 如果电脑开启了`全局或者规则代理`,可不用添加proxy_addr参数值但仍需在config.ini配置文件中设置开启代理
- 注意事项④ 可以在URL_config.ini中的链接开头加上#,此时将不会录制该条链接对应的直播
- 注意事项⑤ 测试时有可能会出现在IDE如Pycharm中运行代码进行直播录制,录制出来的视频却无法正常播放的现象,如果遇到这个问题 最好在命令控制台DOS界面运行代码,录制出来的视频即可正常播放。
- 可使用 `pyinstaller -F或-D` 将代码打包成exe可执行文件 ,前提是已经安装`pyinstaller`
- 可使用 `pyinstaller -F或-D` 将代码打包成exe可执行文件 ,前提是已经安装了`pyinstaller`

&emsp;

Tiktok目前只支持PC网页端地址(没下app),其他平台 app端直播间分享地址和网页端长地址都能正常进行录制(抖音尽量用长链接,避免因短链接转换失效导致不能正常录制)。

直播间链接示例:

```
Expand All @@ -75,15 +74,34 @@ https://www.douyu.com/topic/wzDBLS6?rid=4921614&dyshid=
YY:
https://www.yy.com/22490906/22490906
B站:
https://live.bilibili.com/320
```

Tiktok目前只支持PC网页端地址(没下载app),其他平台 app端直播间分享地址和网页端长地址都能正常进行录制(抖音尽量用长链接,避免因短链接转换失效导致不能正常录制)。

</div>

测试API([源码](https://github.com/ihmily/DouyinLiveRecorder/tree/main/api)):https://hmily.vip/api/jx/live/?url=
解析接口:

```HTTP
GET https://hmily.vip/api/jx/live/?url=
```

请求示例:

```HTTP
GET https://hmily.vip/api/jx/live/?url=https://live.douyin.com/573716250978
```

若需要将抖音直播间短链接转换为长链接,使用以下接口:

```HTTP
GET https://hmily.vip/api/jx/live/convert.php?url=https://v.douyin.com/iQLgKSj/
```

请求示例:https://hmily.vip/api/jx/live/?url=https://live.douyin.com/573716250978

抖音地址转换:https://hmily.vip/api/jx/live/convert.php?url=https://v.douyin.com/iQLgKSj/

&emsp;

Expand All @@ -95,9 +113,12 @@ https://www.yy.com/22490906/22490906

## ⏳提交日志

- 20230814
- 新增B站直播录制

- 20230812
- 新增YY直播录制

- 20230808
- 修复主播重新开播无法再次录制的问题

Expand Down Expand Up @@ -125,6 +146,6 @@ https://www.yy.com/22490906/22490906

&emsp;

## 有问题可以提issue ,后续我会在这里不断更新其他直播平台的录制 欢迎给个Star
## 有问题可以提issue ,后续我会在这里不断更新其他直播平台的录制 欢迎Star

####
62 changes: 54 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Author: Hmily
Github: https://github.com/ihmily
Date: 2023-07-17 23:52:05
Update: 2023-08-12 02:42:52
Update: 2023-08-14 17:13:00
Copyright (c) 2023 by Hmily, All Rights Reserved.
Function: Record live stream video.
"""
Expand All @@ -25,8 +25,8 @@
from web_rid import *

# 版本号
version = "v1.0.3"
platforms = "抖音|Tiktok|快手|虎牙|斗鱼|YY"
version = "v1.0.4"
platforms = "抖音|Tiktok|快手|虎牙|斗鱼|YY|B站"

# --------------------------log日志-------------------------------------
# 创建一个logger
Expand Down Expand Up @@ -92,7 +92,7 @@ def display_info():
print(f"是否开启代理录制: {'是' if use_proxy else '否'}", end=" | ")
if Splitvideobysize:
print(f"TS录制分段开启,录制分段大小为 {Splitsize} M", end=" | ")
print(f"是否生成时间文件: {'是' if create_time_file else '否'}", end = " | ")
print(f"是否生成时间文件: {'是' if create_time_file else '否'}", end=" | ")
print("录制视频质量为: " + str(video_quality), end=" | ")
print("录制视频格式为: " + str(video_save_type), end=" | ")
print("目前瞬时错误数为: " + str(warning_count), end=" | ")
Expand Down Expand Up @@ -422,6 +422,50 @@ def get_yy_stream_url(json_data):
return data


def get_bilibili_stream_url(json_data):
# TODO: 获取B站直播源地址
data = []

anchor_name = json_data['roomInfoRes']['data']['anchor_info']['base_info']['uname']
playurl_info = json_data['roomInitRes']['data']['playurl_info']
if not playurl_info:
data = [anchor_name, False, '', '']
else:
def get_url(m, n):
format_list = ['.flv', '.m3u8']
# 字典中的键就是qn,其中qn=30000为杜比 20000为4K 10000为原画 400蓝光 250超清 150高清,qn=0是默认画质
quality_list = {'10000': 'bluray', '400': '4000', '250': '2500', '150': '1500'}

stream_data = playurl_info['playurl']['stream'][m]['format'][0]['codec'][0]
accept_qn_list = stream_data['accept_qn']
while len(accept_qn_list) < 4:
accept_qn_list.append(accept_qn_list[0])
base_url = stream_data['base_url']
host = stream_data['url_info'][0]['host']
extra = stream_data['url_info'][0]['extra']
format = format_list[m]
qn = str(accept_qn_list[n])
quality = quality_list[qn]
base_url = re.sub(r'(\d+)' + f'(?={format}\?)', quality, base_url)
extra = re.sub('&qn=0', f'&qn={qn}', extra)
url = host + base_url + extra
return url

if video_quality == "原画" or video_quality == "蓝光":
flv_url = get_url(0, 0)
m3u8_url = get_url(1, 0)
elif video_quality == "超清":
flv_url = get_url(0, 1)
m3u8_url = get_url(1, 1)
elif video_quality == "高清":
flv_url = get_url(0, 2)
m3u8_url = get_url(1, 2)
elif video_quality == "标清":
flv_url = get_url(0, 3)
m3u8_url = get_url(1, 3)
data = [anchor_name, True, m3u8_url, flv_url] # B站使用m3u8链接进行录制
return data


def start_record(line, count_variable=-1):
global warning_count
Expand Down Expand Up @@ -489,6 +533,10 @@ def start_record(line, count_variable=-1):
json_data = get_yy_stream_data(record_url)
port_info = get_yy_stream_url(json_data)

elif record_url.find("https://live.bilibili.com/") > -1:
with semaphore:
json_data = get_bilibili_stream_data(record_url)
port_info = get_bilibili_stream_url(json_data)

# print("端口信息:" + str(port_info))
# port_info=['主播名','状态码','m3u8地址','flv地址']
Expand Down Expand Up @@ -1098,7 +1146,8 @@ def tranform_int_to_time(seconds):
'live.kuaishou.com',
'www.huya.com',
'www.douyu.com',
'www.yy.com'
'www.yy.com',
'live.bilibili.com'
]
if url_host in host_list:
new_line = (url, split_line[1])
Expand Down Expand Up @@ -1148,6 +1197,3 @@ def tranform_int_to_time(seconds):

# 总体循环3s
time.sleep(3)



24 changes: 21 additions & 3 deletions spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Author: Hmily
Github:https://github.com/ihmily
Date: 2023-07-15 23:15:00
Update: 2023-08-12 02:30:52
Update: 2023-08-14 01:42:23
Copyright (c) 2023 by Hmily, All Rights Reserved.
Function: Get live stream data.
"""
Expand Down Expand Up @@ -200,6 +200,21 @@ def get_yy_stream_data(url):
return json_data


def get_bilibili_stream_data(url):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Referer':'https://live.bilibili.com/?spm_id_from=333.1296.0.0',
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request, timeout=10)
html_str = response.read().decode('utf-8')
json_str=re.search('<script>window.__NEPTUNE_IS_MY_WAIFU__=(.*?)</script><script>',html_str,re.S).group(1)
json_data=json.loads(json_str)
return json_data



if __name__ == '__main__':
# 抖音直播间页面的cookie
Cookie = 'ttwid=1%7CB1qls3GdnZhUov9o2NxOMxxYS2ff6OSvEWbv0ytbES4%7C1680522049%7C280d802d6d478e3e78d0c807f7c487e7ffec0ae4e5fdd6a0fe74c3c6af149511; my_rd=1; passport_csrf_token=3ab34460fa656183fccfb904b16ff742; passport_csrf_token_default=3ab34460fa656183fccfb904b16ff742; d_ticket=9f562383ac0547d0b561904513229d76c9c21; n_mh=hvnJEQ4Q5eiH74-84kTFUyv4VK8xtSrpRZG1AhCeFNI; store-region=cn-fj; store-region-src=uid; LOGIN_STATUS=1; __security_server_data_status=1; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%7D; pwa2=%223%7C0%7C3%7C0%22; download_guide=%223%2F20230729%2F0%22; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.6%7D; strategyABtestKey=%221690824679.923%22; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1536%2C%5C%22screen_height%5C%22%3A864%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A150%7D%22; VIDEO_FILTER_MEMO_SELECT=%7B%22expireTime%22%3A1691443863751%2C%22type%22%3Anull%7D; home_can_add_dy_2_desktop=%221%22; __live_version__=%221.1.1.2169%22; device_web_cpu_core=8; device_web_memory_size=8; xgplayer_user_id=346045893336; csrf_session_id=2e00356b5cd8544d17a0e66484946f28; odin_tt=724eb4dd23bc6ffaed9a1571ac4c757ef597768a70c75fef695b95845b7ffcd8b1524278c2ac31c2587996d058e03414595f0a4e856c53bd0d5e5f56dc6d82e24004dc77773e6b83ced6f80f1bb70627; __ac_nonce=064caded4009deafd8b89; __ac_signature=_02B4Z6wo00f01HLUuwwAAIDBh6tRkVLvBQBy9L-AAHiHf7; ttcid=2e9619ebbb8449eaa3d5a42d8ce88ec835; webcast_leading_last_show_time=1691016922379; webcast_leading_total_show_times=1; webcast_local_quality=sd; live_can_add_dy_2_desktop=%221%22; msToken=1JDHnVPw_9yTvzIrwb7cQj8dCMNOoesXbA_IooV8cezcOdpe4pzusZE7NB7tZn9TBXPr0ylxmv-KMs5rqbNUBHP4P7VBFUu0ZAht_BEylqrLpzgt3y5ne_38hXDOX8o=; msToken=jV_yeN1IQKUd9PlNtpL7k5vthGKcHo0dEh_QPUQhr8G3cuYv-Jbb4NnIxGDmhVOkZOCSihNpA2kvYtHiTW25XNNX_yrsv5FN8O6zm3qmCIXcEe0LywLn7oBO2gITEeg=; tt_scid=mYfqpfbDjqXrIGJuQ7q-DlQJfUSG51qG.KUdzztuGP83OjuVLXnQHjsz-BRHRJu4e986'
Expand All @@ -209,12 +224,15 @@ def get_yy_stream_data(url):
# url = 'https://www.huya.com/116' # 虎牙直播
# url = 'https://www.douyu.com/topic/wzDBLS6?rid=4921614&dyshid=' # 斗鱼直播
# url = 'https://www.douyu.com/3637778?dyshid='
url = 'https://www.yy.com/22490906/22490906'
# url = 'https://www.yy.com/22490906/22490906' # YY直播
url = 'https://live.bilibili.com/21593109' # b站直播


# print(get_douyin_stream_data(url,Cookie))
# print(get_tiktok_stream_data(url,''))
# print(get_kuaishou_stream_data(url))
# print(get_huya_stream_data(url))
# print(get_douyu_info_data(url))
# print(get_douyu_stream_data("4921614",rate='-1'))
print(get_yy_stream_data(url))
# print(get_yy_stream_data(url))
print(get_bilibili_stream_data(url))

0 comments on commit ebfad74

Please sign in to comment.