#python #tkinter #youtube-dl
#python #tkinter #youtube-dl
Вопрос:
Я создаю загрузчик Youtube youtube-dl
, и когда я указываю путь, он говорит, что у меня нет разрешения, хотя я это делаю.
Вот сценарий:
import tkinter as tk
from tkinter import Tk
from tkinter import messagebox
from tkinter.constants import DISABLED
from tkinter import filedialog
import youtube_dl
import ctypes, sys
window = tk.Tk()
optionswindow = Tk()
f = False
t = True
def closeoptions():
optionswindow.withdraw()
def options():
optionswindow.deiconify()
optionswindow.geometry("360x640")
optionswindow.resizable(t, t)
global canvas2
canvas2 = tk.Canvas(optionswindow, width = 360, height = 640)
canvas2.pack()
canvas2.create_window(360, 640)
optionswindow.title("Options...")
global pathnotify
pathnotify = tk.Label(optionswindow, text="Where Shall We Save The File?")
pathnotify.place(x=25, y=5)
closebutton = tk.Button(optionswindow, text="Close", width=10, command=closeoptions)
def BrowseLocal():
Browsed = t
global TheEntry
TheEntry = filedialog.askdirectory(title="Where Shall We Save The File?")
if Browsed == t:
getinfo()
def getinfo():
print(TheEntry)
BrowseButton = tk.Button(optionswindow, text="Browse...", width=40, command=BrowseLocal)
BrowseButton.place(x=25, y=25)
closebutton.place(x=5, y=610)
optionswindow.protocol("WM_DELETE_WINDOW", closeoptions)
def click():
messagebox.showinfo("We are now downloading this video","We are now downloading the video, please
wait. If you have not specified the path you would like to save the file in, the file will save
in your user folder")
options = {'outtmpl': TheEntry}
x1 = linkbox.get()
youtube_dl.YoutubeDL().extract_info(x1, download=False)
with youtube_dl.YoutubeDL(options) as download:
download.download([x1])
optionswindow.withdraw()
window.title("Youtube Downloader")
window.minsize(250,50)
window.resizable(False, False)
canvas1 = tk.Canvas(window, width = 275, height = 75)
canvas1.pack()
linkbox = tk.Entry (window)
canvas1.create_window(100, 25, window=linkbox)
window.protocol("WM_DELETE_WINDOW", exit)
optionbutton = tk.Button(window, text="Options...", width=40, command=options)
optionbutton.place(x=0, y=50)
downloadbutton = tk.Button(window, compound=tk.LEFT, width=10, height=1,
text="Download", bg='white', command=click)
downloadbutton.place(x=170, y=12)
window.mainloop()
Я заставляю скрипт сохранять видео в свои документы, но затем он говорит следующее
ERROR: unable to open for writing: [Errno 13] Permission denied: 'C:\Users\Naeem\Documents'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dldownloaderhttp.py", line 260, in download
ctx.tmpfilename, ctx.open_mode)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlutils.py", line 2054, in sanitize_open
stream = open(encodeFilename(filename), open_mode)
PermissionError: [Errno 13] Permission denied: 'C:\Users\Naeem\Documents'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 1702, in __call__
return self.func(*args)
File "C:UsersNaeemDocumentsCoding ProjectDownloaderUiFilesUI.py", line 51, in click
download.download([x1])
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 2029, in download
url, force_generic_extractor=self.params.get('force_generic_extractor', False))
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 796, in extract_info
return self.__extract_info(url, ie, download, extra_info, process)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 803, in wrapper
return func(self, *args, **kwargs)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 835, in __extract_info
return self.process_ie_result(ie_result, download, extra_info)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 869, in process_ie_result
return self.process_video_result(ie_result, download=download)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 1654, in process_video_result
self.process_info(new_info)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 1936, in process_info
success = dl(filename, info_dict)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 1875, in dl
return fd.download(name, info)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dldownloadercommon.py", line 366, in download
return self.real_download(filename, info_dict)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dldownloaderhttp.py", line 352, in real_download
return download()
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dldownloaderhttp.py", line 265, in download
self.report_error('unable to open for writing: %s' % str(err))
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dldownloadercommon.py", line 165, in report_error
self.ydl.report_error(*args, **kargs)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 625, in report_error
self.trouble(error_message, tb)
File "C:UsersNaeemAppDataLocalProgramsPythonPython37libsite-packagesyoutube_dlYoutubeDL.py", line 595, in trouble
raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: 'C:\Users\Naeem\Documents'
Редактировать
Я продолжаю использовать askdirectory()
и изменять некоторые параметры на youtube-dl, и теперь это работает! Спасибо @acw1668! По-видимому, мне пришлось использовать forcefilname и другие команды, чтобы убедиться, что он не перезаписывает весь каталог.
Комментарии:
1. У вас это уже открыто в каком-то другом окне?
2. Пожалуйста, форматируйте свои ошибки так, как у вас есть свой код
3. Я думаю, вам следует использовать
asksaveasfilename()
вместоaskdirectory()
.4. @AbhishekRai Нет, у меня она не открыта. Но это не проблема, это говорит об ошибке разрешения?
5. Если вы хотите использовать
askdirectory()
для указания каталога для хранения загруженного видео, используйтеoptions = {"outtmpl": f"{TheEntry}/%(title)s", "strictfilenames": True}
insideclick()
. Выходным именем файла видео будет его заголовок внутри выбранного каталога.