Skip to content

Commit

Permalink
Adapt to showroom interface update
Browse files Browse the repository at this point in the history
  • Loading branch information
vacabun committed Mar 29, 2024
1 parent f22816f commit 2d7a5c1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions showroom-recorder.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"emoji",
"pytz",
"requests",
"websocket_client",
"ffmpeg-python",
"webdav4",
"biliup"
"biliup",
"m3u8"
]
}
31 changes: 28 additions & 3 deletions src/showroom_recorder/processor/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .uploader import UploaderQueue, UploaderWebDav, UploaderBili
from ..utils.config import get_bili_cookie, get_biliup_list
import re
import m3u8

fake_headers = {
'Accept': '*/*',
Expand Down Expand Up @@ -98,6 +99,7 @@ def get_stream_url_by_roomid(room_id):
try:
response = requests.get(url=api_endpoint, headers=fake_headers).text
response = json.loads(response)
# print(response)
stream_url = response['streaming_url_list'][0]['url']
for streaming_url in response['streaming_url_list']:
if streaming_url['type'] == 'hls_all':
Expand All @@ -107,6 +109,21 @@ def get_stream_url_by_roomid(room_id):
return stream_url


def get_max_bandwidth_stream(m3u8_url):
playlist = m3u8.load(m3u8_url)
stream_dict = {}
for stream in playlist.playlists:
bandwidth = stream.stream_info.bandwidth
stream_url = stream.uri
if not stream_url.startswith("http"):
base_url = m3u8_url.rsplit("/", 1)[0]
stream_url = f"{base_url}/{stream_url}"
stream_dict[bandwidth] = stream_url
max_bandwidth = max(stream_dict.keys())
best_stream_url = stream_dict[max_bandwidth]
return stream_dict, max_bandwidth, best_stream_url


class Recorder:
def __init__(self, room_url_key, live_info, uploader_queue, config):
self.room_url_key = room_url_key
Expand Down Expand Up @@ -156,6 +173,9 @@ def download(self, output_dir='.'):
stream_url = get_stream_url_by_roomid(self.room_id)
except Exception as e:
raise Exception('get stream url error: ' + e)
__, __, stream_url = get_max_bandwidth_stream(stream_url)
# logging.info('{room_url_key}: stream url is {stream_url}.'.format(
# room_url_key=self.room_url_key, stream_url=stream_url))

self.time_str = get_time_now().strftime('%Y%m%d_%H%M%S')

Expand All @@ -172,14 +192,17 @@ def download(self, output_dir='.'):
try:
self.ffmpeg_proc = (
ffmpeg
.input(stream_url)
.input(stream_url, **{'rw_timeout': '10000000'})
.output(self.output, **kwargs_dict)
.run()
)
self.ffmpeg_proc.communicate()
except Exception as e:
try:
logging.info('{room_url_key}: video recording error, try to stop ffmpeg process.{e}'.format(
room_url_key=self.room_url_key, e=str(e)))
self.ffmpeg_proc.stdin.write('q'.encode('utf-8'))
self.ffmpeg_proc.stdin.flush()
except Exception:
pass
self.__download_finish()
Expand All @@ -196,7 +219,8 @@ def __download_finish(self):
room_url_key=self.room_url_key,
room_name=self.room_name,
time_str=self.time_str,
login_cookie=get_bili_cookie('bili_cookie.json'),
login_cookie=get_bili_cookie(
'bili_cookie.json'),
lines=self.config['biliup_lines'])
self.uploader_queue.put(uploader_bili)

Expand All @@ -206,10 +230,11 @@ def __download_finish(self):
self.config['webdav_url'],
self.config['webdav_username'],
self.config['webdav_password'])
if(self.config['webdav_delete_source_file']):
if (self.config['webdav_delete_source_file']):
uploader_webdav.enable_delete_source_file()
self.uploader_queue.put(uploader_webdav)


class RecroderManager:
def __init__(self, room_url_key_list, config):
self.room_url_key_list = room_url_key_list
Expand Down
2 changes: 1 addition & 1 deletion src/showroom_recorder/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python3

__version__ = '0.6.7'
__version__ = '0.6.8'
30 changes: 30 additions & 0 deletions test/test_stream_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import m3u8

# M3U8文件的URL
m3u8_url = "https://hls-css.live.showroom-live.com/live/40acb124ff82e95b4882495ae83838be7a6b3631ed960cf32b1e6eaf2712d6d5_default.m3u8"

# 使用m3u8库加载M3U8文件
playlist = m3u8.load(m3u8_url)

# 初始化一个字典来存储带宽和相应的视频流URL
stream_dict = {}

# 遍历播放列表中的所有流
for stream in playlist.playlists:
# 获取带宽
bandwidth = stream.stream_info.bandwidth
# 获取视频流的URL
# 需要检查URL是否完整
stream_url = stream.uri
if not stream_url.startswith("http"):
# 如果不是完整的URL,则尝试拼接
base_url = m3u8_url.rsplit("/", 1)[0]
stream_url = f"{base_url}/{stream_url}"

# 将带宽和视频流URL添加到字典中
stream_dict[bandwidth] = stream_url

# 打印结果
print("不同带宽的视频流地址:")
for bandwidth, url in stream_dict.items():
print(f"带宽: {bandwidth}, URL: {url}")

0 comments on commit 2d7a5c1

Please sign in to comment.