-
Notifications
You must be signed in to change notification settings - Fork 2
/
y2qq_GUI.py
291 lines (261 loc) · 13.2 KB
/
y2qq_GUI.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
import threading
import time
import PySimpleGUI as sg
import textwrap
import y2qq
from m3u8_cache import server
# start a local server
server.start_server()
graphic_off = True
format_list = []
g_current_selected_format = 0
g_current_m3u8_url = ""
toggle_btn_off = b'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABmJLR0QA/wD/AP+gvaeTAAAED0lEQVRYCe1WTWwbRRR+M/vnv9hO7BjHpElMKSlpqBp6gRNHxAFVcKM3qgohQSqoqhQ45YAILUUVDRxAor2VAweohMSBG5ciodJUSVqa/iikaePEP4nj2Ovdnd1l3qqJksZGXscVPaylt7Oe/d6bb9/svO8BeD8vA14GvAx4GXiiM0DqsXv3xBcJU5IO+RXpLQvs5yzTijBmhurh3cyL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8v75eafnSgC6Luo2+B3fGKskilj5muV6pNhk2Qqg5v7lZ51nBZhNBjGrbxfI1+La5t2JCzfD8RF1HTBGJXyDzs1MblONulEqPDVYXgwDIfNx91IUVbAbY837GMur+/k/XZ75UWmJ77ou5mfM1/0x7vP1ls9XQdF2z9uNsPzosXPNFA5m0/EX72TBSiqsWzN8z/GZB08pWq9VeEZ+0bjKb7RTD2i1P4u6r+bwypo5tZUumEcDAmuC3W8ezIqSGfE6g/sTd1W5p5bKjaWubrmWd29Fu9TD0GlYlmTx+8tTJoZeqYe2BZC1/JEU+wQR5TVEUPptJy3Fs+Vkzgf8lemqHumP1AnYoMZSwsVEz6o26i/G9Lgitb+ZmLu/YZtshfn5FZDPBCcJFQRQ+8ih9DctOFvdLIKHH6uUQnq9yhFu0bec7znZ+xpAGmuqef5/wd8hAyEDIQMjAETHwP7nQl2WnYk4yAAAAAElFTkSuQmCC'
sg.theme("DarkAmber")
# 设置控件属性
update_button = sg.Button("更新版本", key="check_update", size=10)
auto_live_check = sg.Button("挂机模式", key="-isLiveNow-", size=14)
ff_text = sg.Text("设置ffmpeg路径")
ff_input = sg.Input("", key="ff", size=4)
port_text = sg.Text("输入代理端口")
port_input = sg.InputText(key="port", size=11)
port_button = sg.Button(
"设置代理",
)
save_button = sg.Button("保存配置", key="save_yaml", size=14)
read_button = sg.Button("使用配置", key="read_yaml", size=14)
switch_button = sg.Button('', image_data=toggle_btn_off, key='-TOGGLE-GRAPHIC-',
button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0)
switch_text = sg.Text('本地m3u8中转: ')
url_text = sg.Text("输入youtube链接")
url_input = sg.InputText(key="url", size=25)
m3u8_button = sg.Button("获取直播信息", size=14)
key_text = sg.Text("输入直播密钥")
key_input = sg.InputText(key="key", size=20)
live_server_text = sg.Text("输入直播服务器")
live_server_input = sg.Input(
key="live_server", size=20, default_text="rtmp://6721.livepush.myqcloud.com/live/"
)
output_Ml = sg.Multiline(
key="Output", disabled=True, size=(50, 17), autoscroll=True, reroute_cprint=True
)
start_button = sg.Button("开始直播", key='-start_button-', size=14)
stop_button = sg.Button("停止推流", visible=False, size=14)
format_list_text = sg.Text("选择分辨率")
format_list_selector = sg.Combo(
format_list, key="-SELECTOR-", readonly=True, enable_events=True, size=20
)
tilte_text = sg.Text("", key="-title-")
main_tab = [
[url_text, url_input],
[m3u8_button, switch_text, switch_button],
[format_list_text, format_list_selector],
[start_button, stop_button],
[auto_live_check],
[tilte_text],
]
setting_tab = [
[update_button],
[ff_text, ff_input, sg.FileBrowse("选择ffmpeg.exe")],
[port_text, port_input, port_button],
[live_server_text, live_server_input],
[key_text, key_input],
[save_button, read_button],
]
# 左侧布局
layout = [
[
sg.TabGroup(
[[sg.Tab("主页", main_tab), sg.Tab("设置", setting_tab)]],
key="-group1-",
tab_location="right",
)
],
[output_Ml],
]
# ---temp function. Maybe move to outer file for better constuct
def btn_read_yaml_config(window: sg.Window):
# 读取保存的配置并更新到控件
try:
dic = y2qq.read_yaml("config.yaml")
tmp_ff_path = dic.get("ff", "")
if tmp_ff_path != "":
window["ff"].update(tmp_ff_path)
tmp_port = dic.get("port", "")
if tmp_port != "":
window["port"].update(tmp_port)
tmp_live_server = dic.get("live_server", "")
if tmp_live_server != "":
window["live_server"].update(tmp_live_server)
if dic.get("last_ytb_link"):
window["url"].update(dic.get("last_ytb_link"))
if dic.get("last_key"):
window["key"].update(dic.get("last_key"))
y2qq.set_proxy(dic["port"])
except Exception as e:
pass
def btn_save_config_yaml(values, should_pop_up=True):
try:
# 保存配置字典到当前文件夹
dic = {
"ff": values["ff"],
"port": values["port"],
"last_ytb_link": values["url"],
"last_key": values["key"],
"live_server": values["live_server"],
}
y2qq.write_yaml(dic)
if should_pop_up:
sg.Popup("保存配置成功")
except:
sg.Popup("写入配置失败")
def call_window_event_value_with_delay(
window: sg.Window, key, value=None, delay: int = 0.1
):
import time
thread = threading.Thread(
target=window.write_event_value, args=(key, value), daemon=True
)
time.sleep(delay)
thread.start()
def windows_init(window: sg.Window):
btn_read_yaml_config(window)
call_window_event_value_with_delay(window, "获取直播信息")
# ---windows init and event loop
try:
window = sg.Window("QQ频道转播", layout, finalize=True)
windows_init(window)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "设置代理":
try:
tmp_port = int(values["port"])
y2qq.set_proxy(values["port"])
except:
sg.Popup("请输入端口数字")
elif event == '-TOGGLE-GRAPHIC-':
graphic_off = not graphic_off
window['-TOGGLE-GRAPHIC-'].update(
image_data=toggle_btn_off if graphic_off else toggle_btn_on)
elif event == "获取直播信息":
# 开启新进程获取直播信息
window.perform_long_operation(
lambda: y2qq.get_format(values["url"]), "-format-"
)
elif event == "-format-":
# 获取清晰度参数format
try:
# 每次更新时需要先清空,否则按多次时无限添加
format_list = []
formats_raw = values[event]
formats = formats_raw["formats"]
format_list = [f"{f['height']}p" for f in formats]
window["-SELECTOR-"].update(values=format_list)
call_window_event_value_with_delay(
window, "-SELECTOR-", format_list[-1]
)
title = textwrap.fill(formats_raw["title"][:-17], 35)
window["-title-"].update(title)
except:
pass
elif event == "-SELECTOR-":
# 通过列表选择器的输入提取对应的m3u8链接
g_current_selected_format = values["-SELECTOR-"]
index = format_list.index(values["-SELECTOR-"])
# 选择对应Index,用于自动触发时
window["-SELECTOR-"].update(set_to_index=[index])
g_current_m3u8_url = formats[index]["url"]
sg.cprint(f"当前选择:{g_current_selected_format}")
elif event == "-start_button-":
# 判断是否开启本地转发
if graphic_off == False:
try:
btn_save_config_yaml(values, should_pop_up=False)
# 开启新进程 用于持续打印推流情况
window.perform_long_operation(
lambda: y2qq.restream(
g_current_m3u8_url,
formats_raw["id"],
g_current_selected_format,
values["ff"],
values["live_server"],
values["key"],
),
"-restream-",
)
window["停止推流"].update(visible=True)
except Exception as e:
sg.Popup("推流失败,检查密钥和ffmpeg是否配置正确")
else:
try:
btn_save_config_yaml(values, should_pop_up=False)
# 开启新进程 用于持续打印推流情况
window.perform_long_operation(
lambda: y2qq.restream_direct(
g_current_m3u8_url,
values["ff"],
values["key"],
),
"-restream-",
)
window["停止推流"].update(visible=True)
except Exception as e:
sg.Popup("推流失败,检查密钥和ffmpeg是否配置正确")
elif event == "-start_button-":
try:
window.perform_long_operation(
lambda: y2qq.restream_direct(g_current_m3u8_url, values['ff'], values['key']), '-restream_direct-')
window["停止推流"].update(visible=True)
except:
sg.Popup('推流失败,检查m3u8、密钥和ffmpeg是否配置正确')
elif event == "-restream-":
pass
elif event == "-restream_direct-":
pass
elif event == "-isLiveNow-":
window.perform_long_operation(
lambda: y2qq.check_live(values["url"]), "-check-"
)
elif event == "-check-":
window.write_event_value("获取直播信息", "** DONE **")
is_auto_live_OK = False
tmp_retry_times = 0
while True:
if g_current_m3u8_url:
window.write_event_value("获取直播信息", "** DONE **")
call_window_event_value_with_delay(
window, "开始直播", g_current_m3u8_url, delay=1
)
is_auto_live_OK = True
break
else:
if tmp_retry_times > 10:
is_auto_live_OK = False
break
tmp_retry_times += 1
time.sleep(1)
if is_auto_live_OK == False:
sg.popup("自动开播失败, 请检测网络")
elif event == "停止推流":
y2qq.stop_restream()
sg.cprint("推流已经停止")
elif event == "save_yaml":
btn_save_config_yaml(values)
elif event == "read_yaml":
btn_read_yaml_config(window)
elif event == "check_update":
y2qq.check_update(window)
elif event == "-Missing_Updater-":
sg.popup("找不到updater.exe")
else:
sg.Popup(f"未处理未知事件:【{event}】")
except Exception as e:
import traceback
err_str = traceback.format_exc()
sg.Popup(f"未处理异常:【{err_str}】")
finally:
server.shutdown_server()
window.close()