-
Notifications
You must be signed in to change notification settings - Fork 0
/
YT_Downloader.py
50 lines (38 loc) · 1.62 KB
/
YT_Downloader.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
import pytube
from tkinter import Tk, filedialog, simpledialog, messagebox
from datetime import datetime
def sanitize_filename(filename):
"""Remove or replace characters in filename that are invalid for file system."""
invalid_chars = ['<', '>', ':', '"', '/', '\\', '|', '?', '*']
for char in invalid_chars:
filename = filename.replace(char, "_")
return filename
def download_video(url, download_path):
try:
video = pytube.YouTube(url)
stream = video.streams.filter(progressive=True, file_extension="mp4").first()
# Generate unique filename using sanitized video title and current timestamp
sanitized_title = sanitize_filename(video.title)
filename = f"{sanitized_title}_{datetime.now().strftime('%Y%m%d%H%M%S')}.mp4"
filepath = os.path.join(download_path, filename)
stream.download(output_path=download_path, filename=filename)
return True
except Exception as e:
print(f"An error occurred: {e}")
return False
def main():
root = Tk()
root.withdraw() # Hide the main window
url = simpledialog.askstring("Input", "Paste the YouTube video URL:")
if not url:
return
download_path = filedialog.askdirectory(title="Select Download Directory")
if not download_path:
return
success = download_video(url, download_path)
if success:
messagebox.showinfo("Success", f"Video downloaded successfully to {download_path}")
else:
messagebox.showerror("Error", "Failed to download the video. Please check the URL or your connection.")
if __name__ == "__main__":
main()