forked from CharlesPikachu/videodl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.py
299 lines (280 loc) · 9.99 KB
/
Demo.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
# 代码仅供学习交流,不得用于商业/非法使用
# 作者:Charles
# 公众号:Charles的皮卡丘
# 视频下载器-Demo版
# 目前支持的平台:
# 网易云课堂: wangyiyun.wangyiyun()
# 音悦台: yinyuetai.yinyuetai()
# B站: bilibili.bilibili()
# 知乎: zhihu.zhihu()
# 斗鱼: douyu.douyu()
# Ted演讲: ted.ted()
# CNTV: cntv.cntv()
# 战旗: zhanqi.zhanqi()
import os
import threading
from platforms import *
from utils.utils import *
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image, ImageTk
# 下载器类
class Download_Thread(threading.Thread):
def __init__(self, *args, **kwargs):
super(Download_Thread, self).__init__(*args, **kwargs)
self.__pause = threading.Event()
self.__pause.clear()
self.__running = threading.Event()
self.__running.set()
self.flag = False
# 对应关系:
# 网易云课堂 -> '1'
# 音悦台 -> '2'
# B站 -> '3'
# 知乎 -> '4'
# 斗鱼 -> '5'
# Ted演讲 -> '6'
# CNTV -> '7'
# 战旗 -> '8'
self.engine = None
self.url = None
self.savepath = './videos'
def run(self):
while self.__running.isSet():
self.__pause.wait()
self.flag = True
if self.engine == '1':
self.show_start_info()
try:
res = wangyiyun.wangyiyun().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '2':
self.show_start_info()
try:
res = yinyuetai.yinyuetai().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '3':
self.show_start_info()
try:
res = bilibili.bilibili().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '4':
self.show_start_info()
try:
res = zhihu.zhihu().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '5':
self.show_start_info()
try:
res = douyu.douyu().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '6':
self.show_start_info()
try:
res = ted.ted().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '7':
self.show_start_info()
try:
res = cntv.cntv().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
elif self.engine == '8':
self.show_start_info()
try:
res = zhanqi.zhanqi().get(self.url, savepath=self.savepath, app='demo')
if res != 200:
raise RuntimeError('url request error...')
except:
self.show_parse_error()
self.show_end_info(savepath=self.savepath)
else:
title = '解析失败'
msg = '平台选项参数解析失败!'
messagebox.showerror(title, msg)
self.pause()
def pause(self):
self.__pause.clear()
def resume(self):
self.__pause.set()
def stop(self):
self.__running.clear()
def show_start_info(self):
title = '开始下载'
msg = '搜索平台: {}\n已开始下载{},请耐心等待。'.format(self.engine, self.url)
messagebox.showinfo(title, msg)
def show_end_info(self, savepath='./videos'):
title = '下载成功'
msg = '{}下载成功。'.format(self.url)
messagebox.showinfo(title, msg)
def show_parse_error(self):
title = '解析失败'
msg = '视频链接解析失败!'
messagebox.showerror(title, msg)
t_download = Download_Thread()
# 视频转换类
class Transfer_Thread(threading.Thread):
def __init__(self, *args, **kwargs):
super(Transfer_Thread, self).__init__(*args, **kwargs)
self.__pause = threading.Event()
self.__pause.clear()
self.__running = threading.Event()
self.__running.set()
self.flag = False
self.origin_file = None
self.target_format = None
self.savepath = 'results'
def run(self):
while self.__running.isSet():
self.__pause.wait()
self.flag = True
title = '开始转换'
msg = '已开始转换视频,请耐心等待。'
messagebox.showinfo(title, msg)
try:
result = transfer(origin_file=self.origin_file, target_format=self.target_format, savepath=self.savepath)
if result is False:
raise RuntimeError('origin_file unsupported...')
except:
title = '转换失败'
msg = '视频转换失败!'
messagebox.showerror(title, msg)
self.pause()
def pause(self):
self.__pause.clear()
def resume(self):
self.__pause.set()
def stop(self):
self.__running.clear()
t_transfer = Transfer_Thread()
# 下载器
def downloader(options, op_engine_var, en_videourl_var):
engine = str(options.index(str(op_engine_var.get())) + 1)
t_download.engine = engine
t_download.url = str(en_videourl_var.get())
if t_download.flag is False:
t_download.start()
t_download.resume()
# 转换器
def converter(en_originvideo_var, op_tarformat_var):
t_transfer.origin_file = str(en_originvideo_var.get())
t_transfer.target_format = str(op_tarformat_var.get())
if t_transfer.flag is False:
t_transfer.start()
t_transfer.resume()
# 选择视频
def ChoiceVideo(en_originvideo):
filepath = filedialog.askopenfilename()
en_originvideo.delete(0, END)
en_originvideo.insert(INSERT, filepath)
# 关于作者
def ShowAuthor():
title = '关于作者'
msg = '作者: Charles\n公众号: Charles的皮卡丘\nGithub: https://github.com/CharlesPikachu/Video-Downloader'
messagebox.showinfo(title, msg)
# 退出程序
def stopDemo(root):
t_download.stop()
t_transfer.stop()
root.destroy()
root.quit()
# 主界面
def Demo(options):
assert len(options) > 0
# 初始化
root = Tk()
root.title('视频下载器V1.3——公众号:Charles的皮卡丘')
root.resizable(False, False)
root.geometry('600x375+400+120')
image_path = './bgimgs/bg1_demo.jpg'
bgimg = Image.open(image_path)
bgimg = ImageTk.PhotoImage(bgimg)
lb_bgimg = Label(root, image=bgimg)
lb_bgimg.grid()
# Menu
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label='退出', command=lambda: stopDemo(root), font=('楷体', 10))
menubar.add_cascade(label='文件', menu=filemenu)
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label='关于作者', command=ShowAuthor, font=('楷体', 10))
menubar.add_cascade(label='更多', menu=filemenu)
root.config(menu=menubar)
# Label组件(标题)
lb_title1 = Label(root, text='视频下载', font=('楷体', 15), bg='white')
lb_title1.place(relx=0.5, rely=0.05, anchor=CENTER)
lb_title2 = Label(root, text='视频转换', font=('楷体', 15), bg='white')
lb_title2.place(relx=0.5, rely=0.55, anchor=CENTER)
# 视频下载部分:
# Label+Entry组件(视频地址)
lb_videourl = Label(root, text='视频地址:', font=('楷体', 10), bg='white')
lb_videourl.place(relx=0.1, rely=0.15, anchor=CENTER)
en_videourl_var = StringVar()
en_videourl = Entry(root, textvariable=en_videourl_var, width=55, fg='gray', relief=GROOVE, bd=3)
en_videourl.insert(0, 'http://study.163.com/course/courseMain.htm?courseId=1003842018')
en_videourl.place(relx=0.49, rely=0.15, anchor=CENTER)
# Label+OptionMenu组件(搜索平台)
lb_engine = Label(root, text='搜索平台:', font=('楷体', 10), bg='white')
lb_engine.place(relx=0.1, rely=0.25, anchor=CENTER)
op_engine_var = StringVar()
op_engine_var.set(options[0])
op_engine = OptionMenu(root, op_engine_var, *options)
op_engine.place(relx=0.26, rely=0.25, anchor=CENTER)
# Button组件(下载与退出)
bt_download = Button(root, text='下载视频', bd=2, width=15, height=2, command=lambda: downloader(options, op_engine_var, en_videourl_var), font=('楷体', 10))
bt_download.place(relx=0.26, rely=0.38, anchor=CENTER)
bt_quit = Button(root, text='退出程序', bd=2, width=15, height=2, command=lambda: stopDemo(root), font=('楷体', 10))
bt_quit.place(relx=0.52, rely=0.38, anchor=CENTER)
# 视频转换部分:
# Label+Entry+Button组件(视频路径)
lb_originvideo = Label(root, text='视频路径:', font=('楷体', 10), bg='white')
lb_originvideo.place(relx=0.1, rely=0.65, anchor=CENTER)
en_originvideo_var = StringVar()
en_originvideo = Entry(root, textvariable=en_originvideo_var, width=55, fg='gray', relief=GROOVE, bd=3)
en_originvideo.insert(0, '请输入/选择视频路径')
en_originvideo.place(relx=0.49, rely=0.65, anchor=CENTER)
bt_choice = Button(root, text='打开', bd=1, width=5, height=1, command=lambda: ChoiceVideo(en_originvideo), font=('楷体', 10))
bt_choice.place(relx=0.86, rely=0.65, anchor=CENTER)
# Label+OptionMenu组件(目标格式)
lb_tarformat = Label(root, text='目标格式:', font=('楷体', 10), bg='white')
lb_tarformat.place(relx=0.1, rely=0.75, anchor=CENTER)
options_format = ['mp4', 'flv', 'avi']
op_tarformat_var = StringVar()
op_tarformat_var.set(options_format[0])
op_tarformat = OptionMenu(root, op_tarformat_var, *options_format)
op_tarformat.place(relx=0.22, rely=0.75, anchor=CENTER)
# Button组件(视频转换)
bt_transfer = Button(root, text='开始转换', bd=2, width=15, height=2, command=lambda: converter(en_originvideo_var, op_tarformat_var), font=('楷体', 10))
bt_transfer.place(relx=0.52, rely=0.75, anchor=CENTER)
root.mainloop()
if __name__ == '__main__':
options = ["1.网易云课堂", "2.音悦台", "3.B站", "4.知乎", "5.斗鱼", "6.Ted演讲", "7.CNTV", "8.战旗"]
Demo(options)