hello,大家好啊,失蹤人口回歸了 [捂臉]!本次使用tkinter撰寫一篇 抖音無(wú)水印視頻下載,目的很純粹,就是為了設(shè)置 微信狀態(tài)視頻。本篇博文中,我會(huì)寫下我的代碼撰寫思路以及想寫設(shè)計(jì)流程,代碼放在了第四節(jié),工具打包好放在了 藍(lán)奏云,慢慢看,后面有鏈接。
本次要用到以下依賴庫(kù):re json os random tkinter threading requests pillow 其中后兩個(gè)需要安裝后使用
設(shè)計(jì)流程分為總體設(shè)計(jì)和詳細(xì)設(shè)計(jì),這里我會(huì)使用viso畫出幾個(gè)流程圖,用以展示我的思路,詳細(xì)設(shè)計(jì)部分列舉了兩個(gè)函數(shù)實(shí)現(xiàn)的具體流程。
#--*coding:utf-8*--
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
import json
import threading
import requests
from PIL import Image,ImageTk
'''
抖音無(wú)水印視頻下載工具
難點(diǎn):1.視頻接口
2.進(jìn)度條
'''
def clean_progressbar():
# 清空進(jìn)度條
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知變量,可更改
n = 600 / x # 465是矩形填充滿的次數(shù)
for t in range(x):
n = n + 600 / x
# 以矩形的長(zhǎng)度作為變量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
def download():
#先清空進(jìn)度條,再下載
clean_progressbar()
real_link=t1.get('0.0',END).split('**')[-1]
headers2 = {
'sec-fetch-dest': 'document',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Mobile Safari/537.36'
}
r2 = requests.get(real_link, headers=headers2, stream=True)
try:
os.mkdir(video_path)
except:
pass
file =video_path+ video_title + '.mp4'
#下載與進(jìn)度條
chunk_size = 1024 # 每一塊的大小,每次下載塊的大小
file_size = int(r2.headers['Content-Length']) # 提取出來(lái)的文件大小為string格式,使用int()強(qiáng)制轉(zhuǎn)化
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
raise_data = 600 / (file_size / chunk_size) # 增量大小,600為進(jìn)度條的長(zhǎng)度
_size = 0 # 已經(jīng)下載文件的大小
with open(file, "wb") as f:
n=0
for data in r2.iter_content(chunk_size): # inter_content:用于邊下載邊存硬盤,每次下載chunk_size大小的塊
f.write(data)
n = n + raise_data
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
#下載完以后清空
t1.delete('0.0',END)
t1.insert(END,'{title}.mp4\n下載完成!'.format(title=video_title))
#解析出無(wú)水印視頻下載地址以及視頻標(biāo)題
def parse(share_link):
# 先處理短鏈接獲取item_ids
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-dest': 'document',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
}
# 使用allow_redirects=False禁止重定向,拿到重定向的url
r1 = requests.get(share_link, allow_redirects=False, headers=headers)
item_ids = re.findall(r'video/(\d+)/', r1.text)
interface_url='https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={}'.format(''.join(item_ids))
r=requests.get(interface_url,headers=headers)
_json=json.loads(r.text)
try:
item_list=_json.get('item_list')[0]
video_title=item_list.get('desc')#視頻名稱
watermark_video_link=item_list.get('video').get('play_addr').get('url_list')[0]#視頻帶水印地址
real_video_link=watermark_video_link.replace('playwm','play')#視頻無(wú)水印地址
return video_title,real_video_link
except TypeError:
messagebox.showerror('提示','請(qǐng)檢查輸入地址!')
t1.delete('0.0',END)
return '輸入地址有誤','請(qǐng)檢查!'
#調(diào)用jiexi()將視頻標(biāo)題和下載鏈接顯示在Text Widget中
def pre_parse():
share_link=e1.get()
if len(share_link)!=0:
global video_title, real_video_link
try:
video_title, real_video_link=parse(share_link)
except TypeError:
t1.insert(END,'輸入地址有誤,請(qǐng)檢查!')
t1.delete('0.0',END)
#去除標(biāo)題中的表情以及符號(hào),只保留漢字
try:
t1.insert(END, video_title + '\n' +'*'*30+ real_video_link)
b2.config(state='normal')
except TclError:
filted_title=re.findall(r'[\u4E00-\u9FA5\s]+',video_title)
t1.insert(END,''.join(filted_title)+'\n'+'*'*30+real_video_link)
b2.config(state='normal')
else:
messagebox.showerror('錯(cuò)誤','請(qǐng)輸入地址!')
t1.delete('0.0',END)
t1.insert(END,'請(qǐng)?jiān)谏厦孑斎氲刂罚?)
#打開文件夾函數(shù)
def open_dir():
try:
os.mkdir(video_path)
except:
pass
abs_path=os.path.abspath(video_path)
os.startfile(abs_path)
#退出窗口函數(shù)
def quit_window():
window.destroy()
#函數(shù)打包進(jìn)線程
def thread_it(func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)#先守護(hù)主線程
t.start()#再啟動(dòng)
if __name__ == '__main__':
video_path='./video/'
window=Tk()
window.title('Downloader-v1.0')
width=295
height=380
screenWidth = window.winfo_screenwidth() # 獲取顯示區(qū)域的寬度
screenHeight = window.winfo_screenheight() # 獲取顯示區(qū)域的高度
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
window.geometry("%dx%d+%d+%d" % (width, height, left, top))
window.resizable(0,0)
window.iconbitmap('./rely/my_favicon.ico')
photo=Image.open('./rely/dy_logo.png')
photo=photo.resize((200,50))
image=ImageTk.PhotoImage(photo)
l0=ttk.Label(window,imag=image,justify='center')
l0.pack()
f1=ttk.Labelframe(window,text='視頻鏈接地址:')
f1.place(x=40,y=55)
e1=ttk.Entry(f1,width=30)
e1.pack()
f2=ttk.Labelframe(window,text='信息:')
f2.place(x=40,y=102)
t1=Text(f2,height=6,width=30)
t1.pack()
t1.insert(END,'{_xing}\n將抖音分享鏈接地址粘貼在上面輸入框中,本程序會(huì)自動(dòng)解析出視頻的下載地址并且顯示在這里\n{_xing}'.format(_xing='*'*29))
b1=ttk.Button(window,text='解析',command=lambda :thread_it(pre_parse))
b1.place(x=40,y=265)
b2=ttk.Button(window,text='下載',state='disable',command=lambda :thread_it(download))
b2.place(x=170,y=265)
b3=ttk.Button(window,text='打開文件夾',command=open_dir)
b3.place(x=40,y=298)
b4=ttk.Button(window,text='退出',command=quit_window)
b4.place(x=170,y=298)
f3=ttk.Labelframe(window)
f3.place(x=40,y=329)
l1=ttk.Label(f3,text=' 敬告:本軟件僅供學(xué)習(xí)交流使用!',width=30,justify='center',foreground='red')
l1.pack(fill=X)
f4=ttk.LabelFrame(window,text='下載進(jìn)度:')
f4.place(x=40,y=210)
canvas = Canvas(f4, width=210, height=20,bg="white")
canvas.pack(side='left')
l2_var=StringVar()
l2=ttk.Label(f4,textvariable=l2_var,text='未下載')
l2.pack(side='left',anchor=S)
window.mainloop()
'''
test_url: https://v.douyin.com/JcxTMj2/
'''
本次使用python的tkinter撰寫了一個(gè)抖音無(wú)水印視頻爬取GUI工具,因?yàn)橹虚g寫過(guò)幾個(gè)GUI界面了對(duì)tkinter的widget有些熟悉了,線程用起來(lái)更加熟練。他山之石,可以攻玉,本次思路、代碼的撰寫參考了:
以上就是python gui開發(fā)——制作抖音無(wú)水印視頻下載工具(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于python 抖音視頻下載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!