您好,登錄后才能下訂單哦!
這篇文章給大家介紹在python項目中利用tkinter與you-get如何實現下載視頻,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
運行效果:
完整代碼
from tkinter import * import tkinter.messagebox import base64 from tkinter import filedialog from tkinter.scrolledtext import ScrolledText import subprocess as sub import threading import os top = Tk() top.title("歡迎關注gudu12306知乎") path2=os.path.dirname(os.path.abspath(__file__)) # print(path2) # print(os.environ["Path"]) os.environ["PATH"] += os.pathsep + path2 # print(os.environ["Path"]) #獲取屏幕尺寸以計算布局參數,使窗口居屏幕中央,其中width和height為界面寬和高 width=700 height=700 screenwidth = top.winfo_screenwidth() screenheight = top.winfo_screenheight() alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2) top.geometry(alignstr) #阻止窗口調整大小 top.resizable(0,0) img=b'AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAlJSX/KSkp/y0tLf8wMDD/NjY2/0RERP+Dg4P/Pz8//x4eHv8hISH/IiIi/x0dHf8rKyv/Ly8v/4SEhP+jo6P/HBwc/yEhIf8mJib/KCgo/yEhIf9gYWH/PT4+/x4eHv8gICD/RERE/0xMTP8rKyv/ICAg/zExMf+QkJD/qKio/xoaGv8cHBz/HBwc/x4eHv8gICD/LS0t/x8fH/8fHx//LS0t/3l5ef9qamr/SkpK/yQkJP8oKSj/kZGQ/6ysrP8bGxv/HBwc/x0dHf8cHBz/Hx8f/yAgIP8hISH/NTY2/4OFhf+Xl5f/ampq/2JiYv8zNDL/Jycn/5OTk/+wsLD/Gxsb/xwcHP8dHR3/HR0d/x4eHv8hISH/RUVF/6Ghof/BwcH/tLS0/21ubP9FRkX/TU1N/yoqKv+VlZX/s7Oz/xwcHP8dHR3/HR0d/x0dHf8fHx//Pj4+/66urv/CwsL/ycnJ/7Gxsf89PT3/Nzc3/1tbW/89PT3/eXl5/7e3t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root=Frame(top) frame_left=Frame(frame_root) frame_left.pack(side=LEFT) # frame_right.pack(side=RIGHT,anchor=N) frame_root.pack() #github源地址 tip0_0= Label(frame_left, text='github源地址:https://github.com/soimort/you-get',font = ('楷體',15)) tip0_0.pack(padx=10,anchor=W) tip0_1= Label(frame_left, text='僅用于學習研究使用,用于非法用途概不負責',font = ('楷體',15)) tip0_1.pack(padx=10,anchor=W) #輸入視頻鏈接 tip1= Label(frame_left, text='請輸入視頻鏈接: ',font = ('楷體',25)) tip1.pack(padx=10,anchor=W) #視頻鏈接輸入框 input_url= Entry(frame_left,bg='#F7F3EC') input_url.pack(ipadx=159,ipady=8,padx=20,anchor=W) #請選擇保存位置: tip2=Label(frame_left, text='請選擇保存位置(必填!): ',font = ('楷體',25)) tip2.pack(padx=10,anchor=W) #保存地址輸入框 input_save_address= Entry(frame_left,bg='#F7F3EC') input_save_address.pack(ipadx=159,ipady=8,padx=20,anchor=W) #加載會員cookies: tip3=Label(frame_left, text='加載會員cookies(下載會員視頻選填!!!):',bg="red",font = ('楷體',18)) tip3.pack(padx=10,pady=5,anchor=W) tip4=Label(frame_left, text='cookies文件為火狐瀏覽器的cookies.sqlite文件',bg="red",font = ('楷體',18)) tip4.pack(padx=10,pady=2,anchor=W) #會員cookies輸入框 input_cookies_address= Entry(frame_left,bg='#F7F3EC') input_cookies_address.pack(ipadx=159,ipady=8,padx=20,anchor=W) #瀏覽本地文件夾,選擇保存位置 def browse_folder(): #瀏覽選擇本地文件夾 save_address = filedialog.askdirectory() #把獲得路徑,插入保存地址輸入框(即插入input_save_address輸入框) input_save_address.insert(0,save_address) #瀏覽本地文件夾,選擇保存位置 def browse_cookies(): #瀏覽選擇本地文件夾 cookies_address = filedialog.askopenfilename() #把獲得路徑,插入保存地址輸入框(即插入input_save_address輸入框) input_cookies_address.insert(0,cookies_address) #下載函數 def download(): tkinter.messagebox.showinfo(title='Hi', message='已經開始下載,耐性等待請勿重復點擊') #從輸入框獲取視頻鏈接 url=input_url.get() # 從輸入框獲取保存地址 if input_cookies_address.get()=='': cookies_address='' else: cookies_address="-c "+input_cookies_address.get() save_address=input_save_address.get() cmd = f'you {cookies_address} -o {save_address} {url}' print(cmd) input_url.delete(0,END) input_save_address.delete(0,END) #將cmd結果重定向到tkinterGUI,即將命令行的結果顯示ScrolledText(滾動文本框)控件里 p = sub.Popen(cmd,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE,shell=True) for line in iter(p.stdout.readline, b''): stext.insert(END,line.decode('UTF-8')) stext.yview_moveto(1) if not sub.Popen.poll(p) is None: if line == "": break p.stdout.close() # while p.poll() is None: # output=p.stdout.readline().decode('UTF-8') # stext.insert(END,output) # stext.yview_moveto(1) # if p.poll()!=0: # errors=p.stderr.read().decode('UTF-8') # stext.insert(END,errors) # stext.yview_moveto(1) #為避免在下載時tkinter界面卡死,創建線程函數 def thread_it(func, *args): # 創建 t = threading.Thread(target=func, args=args) # 守護 !!! t.setDaemon(True) # 啟動 t.start() # “瀏覽文件夾”按鈕 browse_folder_button = Button(top, text='瀏覽',font = ('楷體',15),bg="green",command=lambda :thread_it(browse_folder)) browse_folder_button.place(relx=0.81,rely=0.24,anchor="nw") #cookies文件按鈕 browse_folder_cookies = Button(top, text='瀏覽',font = ('楷體',15),bg="green",command=lambda :thread_it(browse_cookies)) browse_folder_cookies.place(relx=0.81,rely=0.40,anchor="nw") # “下載”按鈕 download_button = Button(frame_left, text='下載',font = ('楷體',15),command=lambda :thread_it(download)) download_button.pack( padx=20,pady=6,anchor=W) # ScrolledText組件(滾動文本框) stext = ScrolledText(frame_left, width=60, height=23, background='#F7F3EC') stext.pack(padx=20,anchor=W) top.mainloop()
關于在python項目中利用tkinter與you-get如何實現下載視頻就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。