服务器之家

服务器之家 > 正文

python基于tkinter制作m3u8视频下载工具

时间:2021-10-19 09:38     来源/作者:raxar81

这是我为了学习tkinterpython 写的一个下载m3u8视频的小程序,程序使用了多线程下载,下载后自动合并成一个视频文件,方便播放。

目前的众多视频都是m3u8的播放类型,只要知道视频的m3u8地址,就可以完美下载整个视频。

m3u8地址获取

打开浏览器,点开你要获取地址的视频

 python基于tkinter制作m3u8视频下载工具

重要的来了,右键>>审查元素或者按f12也可以

根据开发或测试的实际环境选择相应的设备,选择iphone6 plus

python基于tkinter制作m3u8视频下载工具

选择好了以后,刷新页面,点击漏斗,选择media,一定刷新之后再点击,没出来的话切换几下选项卡,就能出来了

python基于tkinter制作m3u8视频下载工具

点击播放视频,在下边就可以看到地址了

python基于tkinter制作m3u8视频下载工具

程序代码

?
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
# -*- coding: utf-8 -*-
import sys
 
sys.path.append("c:\\python36-32\\lib\\site-packages")
import tkinter
import re
import urllib3
import threadpool
import threading
import os
import shutil
import time
import glob
from tkinter.ttk import *
from pil import image, imagetk
import pyperclip
from tkinter.filedialog import askdirectory
 
def get_image(filename,width,height):
    im = image.open(filename).resize((width,height))
    return imagetk.photoimage(im)
 
 
def get_resource_path(relative_path):
    if hasattr(sys, '_meipass'):
        return os.path.join(sys._meipass, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)
 
 
def getrealtask(link):
    global key
    rooturl1 = ''
    rooturl2 = ''
    pattern3 = re.compile(r'^.*[\/]', re.m)
    result11 = pattern3.findall(link)
    if result11:
        rooturl1 = result11[0]
    pattern4 = re.compile(r'^http[s]?:\/\/[^\/]*', re.m)
    result114 = pattern4.findall(link)
    if result114:
        rooturl2 = result114[0]
    res = http.request('get', link)
    content = str(res.data, 'utf8')
    list = content.split('\n')
    reallist = []
    for one in list:
        if one.endswith('"key.key"'):
            keyurl = rooturl1 + "key.key"
            res = http.request('get', keyurl)
            key = str(res.data, 'utf8')
        if one.endswith('.ts') or one.endswith('.image'):
            if re.match(r'http', one, re.m | re.i):
                reallist.append(one)
            elif re.match(r'\/', one, re.m | re.i):
                reallist.append(rooturl2 + one)
            else:
                reallist.append(rooturl1 + one)
        if one.endswith('.m3u8'):
            if re.match(r'\/', one, re.m | re.i):
                reallist = getrealtask(rooturl2 + one)
            else:
                reallist = getrealtask(rooturl1 + one)
            break
    return reallist
 
def download_ts(result):
    url = result['url']
    name = result['name']
    num = result['num']
    rootpath = result['root']
    m3u8name = result['m3u8name']
    t= str(result['total'])
    if num % 10000 == 0:
        print(str(num)+' / '+t)
    basepath = os.path.join(rootpath,m3u8name)
    fullpath = os.path.join(basepath,name)
    isexist = os.path.exists(fullpath)
    if not isexist:
        http = urllib3.poolmanager(timeout=10.0)
        while(1):
            try:
                f = http.request('get', url)
                break
            except:
                print("url erro: " + url)
                time.sleep(2)
        d = f.data
        with open(fullpath, "wb") as code:
            code.write(d)
        print("save: " + url)
def clock2(num,path):
    global window
    global key
    v3 = tkinter.stringvar();
    v4 = tkinter.stringvar();
    l3 = tkinter.label(window, text='', textvariable=v3, font=('arial', 10))
    l4 = tkinter.label(window, text='', textvariable=v4, font=('arial', 10))
    l3.place(x=10, y=130, anchor='nw')
    l4.place(x=10, y=160, anchor='nw')
    v3.set("下载中。。。")
    while(1):
        path_file_number = len(glob.glob(path+'/*.ts'))
        mp4_file_number = len(glob.glob(path + '/*.mp4'))
        numberstr = str(path_file_number) + '/'+str(num)
        v4.set(numberstr)
        if mp4_file_number==1:
            v3.set("下载完成!")
            key = ''
            break
 
def clock1():
    global v
    global v2
    global rootpath
    m3u8name = v2.get()
    url = v.get()
    print(url)
    urls = getrealtask(url)
    total = len(urls)
    i = 0
    tasks = []
    tsnames = []
    for one in urls:
        task = {}
        task['root'] = rootpath
        task['m3u8name'] = m3u8name
        task['url'] = one
        task['num'] = i
        task['total'] = total
        task['name'] = str(i) + '.ts'
        tsnames.append(str(i) + '.ts')
        i = i + 1
        tasks.append(task)
    print('tasks: ' + str(len(tasks)))
    targetpath = os.path.join(rootpath, m3u8name)
    if not os.path.exists(targetpath):
        os.makedirs(targetpath)
    timer2 = threading.thread(target=clock2,args=(len(tasks),targetpath))
    timer2.daemon = true
    timer2.start()
    requests = threadpool.makerequests(download_ts, tasks)
    [task_pool.putrequest(req) for req in requests]
    task_pool.wait()
    mp4targetfile = os.path.join(targetpath, m3u8name + '.mp4')
    with open(mp4targetfile, 'wb') as f:
        for ts in tsnames:
            tstargetfile = os.path.join(targetpath, ts)
            with open(tstargetfile, 'rb') as mergefile:
                shutil.copyfileobj(mergefile, f)
            print(tstargetfile + ' merged.')
        for tts in tsnames:
            tstargetfile = os.path.join(targetpath, tts)
            os.remove(tstargetfile)
    print(total)
 
def hit_me():
    global on_hit
    timer = threading.thread(target=clock1)
    timer.daemon = true
    timer.start()
    return
 
def choose_dir():
    global v5
    global rootpath
    rootpath = askdirectory()
    v5.set('文件夹: '+rootpath+'/')
    return
 
def about():
    window = tkinter.toplevel()
    window.geometry('600x100')# note toplevel, not tk.
    msg = 'rax m3u8下载器 v1.4\n写这个程序主要是为了学习tk,顺便满足下自己看视频的需求。\n家里的移动网络看在线视频还是有些卡顿的。 '
    label = tkinter.label(window, text=msg,font=('arial', 15))
    label.grid()
def update():
    window = tkinter.toplevel()
    window.geometry('250x200')
    msg = 'rax m3u8下载器 v1.5\n可以选择保存的目录了\nrax m3u8下载器 v1.4\n增加了菜单栏'
    label = tkinter.label(window, text=msg,font=('arial', 13))
    label.place(x=30, y=30, anchor='nw')
def donate():
    window = tkinter.toplevel()
    window.geometry('500x400')
    msg = '软件免费使用\n欢迎喜欢此软件的各位大佬打赏,谢谢。'
    label = tkinter.label(window, text=msg, font=('arial', 20))
 
 
    i1 = tkinter.photoimage(file=get_resource_path("images\\wx.png"))
    i2 = tkinter.photoimage(file=get_resource_path("images\\zfb.png"))
    imagelabel = tkinter.label(window, text='aaa', image=i1, font=('arial', 10))
    imagelabel2 = tkinter.label(window, text='vvv', image=i2, font=('arial', 10))
    imagelabel.place(x=10, y=145, anchor='nw')
    imagelabel2.place(x=230, y=145, anchor='nw')
    label.place(x=40, y=50, anchor='nw')
    window.mainloop()
def clear():
    global v
    v.set("")
def paste():
    global v
    v.set(pyperclip.paste())
 
key = ''
on_hit = false
rootpath = "d:\\"
#最高50线程
task_pool = threadpool.threadpool(50)
http = urllib3.poolmanager(timeout=5.0)
urllib3.disable_warnings()
 
#主窗口初始化
window = tkinter.tk()
window.style = style()
window.style.theme_use("clam")
window.title("rax m3u8视频下载器")
window.geometry('500x300')
window.resizable(0,0)
 
 
#飞机背景图
canvas_root = tkinter.canvas(window,width=500,height=300)
im_root = get_image(get_resource_path('images\\feiji.jpeg'),500,300)
canvas_root.create_image(250,240,image=im_root)
canvas_root.pack()
 
#各控件初始状态
l1 = tkinter.label(window, text='m3u8地址:', font=('arial', 10))
l1.place(x=10, y=0, anchor='nw')
 
#   地址栏
v = tkinter.stringvar();
e2 = tkinter.entry(window, show=none, textvariable = v,font=('arial', 10),width=40)
v.set('')
e2.place(x=10, y=30, anchor='nw')
 
#   视频名称
l6 = tkinter.label(window, text = ' 视频文件名称:', font=('arial', 10))
l6.place(x=0, y=90, anchor='nw')
 
#   视频名称栏
v2 = tkinter.stringvar();
e3 = tkinter.entry(window, show=none, textvariable = v2,font=('arial', 10),width=15)
v2.set('')
e3.place(x=105, y=90, anchor='nw')
 
#   保存位置
v5 = tkinter.stringvar();
l2 = tkinter.label(window, textvariable = v5, font=('arial', 10))
v5.set('文件夹: d:/')
l2.place(x=10, y=60, anchor='nw')
 
#   下载按钮
b = tkinter.button(window, text='下载', font=('arial', 10), width=10, command=hit_me)
b.place(x=400, y=100, anchor='nw')
 
#   选择路径按钮
pathselectbutton = tkinter.button(window, text='选择路径', font=('arial', 10), width=10, command=choose_dir)
pathselectbutton.place(x=400, y=60, anchor='nw')
 
#   清空按钮
b2 = tkinter.button(window, text='清空', font=('arial', 10), width=10, command=clear)
b2.place(x=300, y=25, anchor='nw')
 
#   粘贴地址按钮
b3 = tkinter.button(window, text='粘贴地址', font=('arial', 10), width=10, command=paste)
b3.place(x=400, y=25, anchor='nw')
 
#   求捐赠按钮
l5 = tkinter.label(window, text='软件免费使用,欢迎各位喜欢此软件的大佬打赏,谢谢。\nqq讨论群:519565890', font=('arial', 10))
l5.place(x=100, y=160, anchor='nw')
 
window.option_add('*tearoff', false)
 
#菜单栏
menubar = tkinter.menu(window)
window['menu'] = menubar
help_menu = tkinter.menu(menubar)
menubar.add_command(label='捐助作者',command=lambda: donate())
menubar.add_cascade(menu=help_menu, label='帮助')
 
#帮助 下拉菜单
help_menu.add_command(label='更新内容',command=lambda: update())
help_menu.add_command(label='关于',command=lambda: about())
 
# 进入消息循环
window.mainloop()

项目地址

https://github.com/raxar81/rax_m3u8_downloader

以上就是python基于tkinter制作m3u8视频下载工具的详细内容,更多关于python m3u8视频下载的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/raxar81/rax_m3u8_downloader

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部